Merge branch 'recompute' into 'main'
Add stats:recompute script See merge request soapbox-pub/ditto!254
This commit is contained in:
commit
64e7655b81
|
@ -10,7 +10,8 @@
|
|||
"check": "deno check src/server.ts",
|
||||
"nsec": "deno run scripts/nsec.ts",
|
||||
"admin:event": "deno run -A scripts/admin-event.ts",
|
||||
"admin:role": "deno run -A scripts/admin-role.ts"
|
||||
"admin:role": "deno run -A scripts/admin-role.ts",
|
||||
"stats:recompute": "deno run -A scripts/stats-recompute.ts"
|
||||
},
|
||||
"unstable": ["ffi", "kv"],
|
||||
"exclude": ["./public"],
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
import { nip19 } from 'nostr-tools';
|
||||
|
||||
import { DittoDB } from '@/db/DittoDB.ts';
|
||||
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);
|
||||
}
|
||||
|
||||
const store = await Storages.db();
|
||||
const kysely = await DittoDB.getInstance();
|
||||
|
||||
const [followList] = await store.query([{ kinds: [3], authors: [pubkey], limit: 1 }]);
|
||||
|
||||
const authorStats: DittoTables['author_stats'] = {
|
||||
pubkey,
|
||||
followers_count: (await store.count([{ kinds: [3], '#p': [pubkey] }])).count,
|
||||
following_count: followList?.tags.filter(([name]) => name === 'p')?.length ?? 0,
|
||||
notes_count: (await store.count([{ kinds: [1], authors: [pubkey] }])).count,
|
||||
};
|
||||
|
||||
await kysely.insertInto('author_stats')
|
||||
.values(authorStats)
|
||||
.onConflict((oc) =>
|
||||
oc
|
||||
.column('pubkey')
|
||||
.doUpdateSet(authorStats)
|
||||
)
|
||||
.execute();
|
Loading…
Reference in New Issue