ditto/scripts/stats-recompute.ts

40 lines
1.0 KiB
TypeScript
Raw Normal View History

2024-05-14 20:55:19 +00:00
import { nip19 } from 'nostr-tools';
2024-05-14 23:50:05 +00:00
import { DittoDB } from '@/db/DittoDB.ts';
2024-05-14 20:55:19 +00:00
import { DittoTables } from '@/db/DittoTables.ts';
import { Storages } from '@/storages.ts';
let pubkey: string;
try {
const result = nip19.decode(Deno.args[0]);
if (result.type === 'npub') {
pubkey = result.data;
} else {
throw new Error('Invalid npub');
}
} catch {
console.error('Invalid npub');
Deno.exit(1);
}
2024-05-14 23:50:05 +00:00
const store = await Storages.db();
const kysely = await DittoDB.getInstance();
const [followList] = await store.query([{ kinds: [3], authors: [pubkey], limit: 1 }]);
2024-05-14 20:55:19 +00:00
const authorStats: DittoTables['author_stats'] = {
pubkey,
2024-05-14 23:50:05 +00:00
followers_count: (await store.count([{ kinds: [3], '#p': [pubkey] }])).count,
2024-05-14 20:55:19 +00:00
following_count: followList?.tags.filter(([name]) => name === 'p')?.length ?? 0,
2024-05-14 23:50:05 +00:00
notes_count: (await store.count([{ kinds: [1], authors: [pubkey] }])).count,
2024-05-14 20:55:19 +00:00
};
2024-05-14 23:50:05 +00:00
await kysely.insertInto('author_stats')
2024-05-14 20:55:19 +00:00
.values(authorStats)
.onConflict((oc) =>
oc
.column('pubkey')
.doUpdateSet(authorStats)
)
.execute();