diff --git a/src/utils/stats.test.ts b/src/utils/stats.test.ts index 5f57dc4..c3e2659 100644 --- a/src/utils/stats.test.ts +++ b/src/utils/stats.test.ts @@ -17,6 +17,24 @@ Deno.test('updateStats with kind 1 increments notes count', async () => { assertEquals(stats!.notes_count, 1); }); +Deno.test('updateStats with kind 1 increments replies count', async () => { + await using db = await getTestDB(); + + const sk = generateSecretKey(); + + const note = genEvent({ kind: 1 }, sk); + await updateStats({ ...db, event: note }); + await db.store.event(note); + + const reply = genEvent({ kind: 1, tags: [['e', note.id]] }, sk); + await updateStats({ ...db, event: reply }); + await db.store.event(reply); + + const stats = await getEventStats(db.kysely, note.id); + + assertEquals(stats!.replies_count, 1); +}); + Deno.test('updateStats with kind 5 decrements notes count', async () => { await using db = await getTestDB(); diff --git a/src/utils/stats.ts b/src/utils/stats.ts index 62135c0..ea16729 100644 --- a/src/utils/stats.ts +++ b/src/utils/stats.ts @@ -3,7 +3,7 @@ import { Kysely, UpdateObject } from 'kysely'; import { SetRequired } from 'type-fest'; import { DittoTables } from '@/db/DittoTables.ts'; -import { getTagSet } from '@/utils/tags.ts'; +import { findReplyTag, getTagSet } from '@/utils/tags.ts'; import { Conf } from '@/config.ts'; interface UpdateStatsOpts { @@ -33,6 +33,15 @@ export async function updateStats({ event, kysely, store, x = 1 }: UpdateStatsOp /** Update stats for kind 1 event. */ async function handleEvent1(kysely: Kysely, event: NostrEvent, x: number): Promise { await updateAuthorStats(kysely, event.pubkey, ({ notes_count }) => ({ notes_count: Math.max(0, notes_count + x) })); + + const inReplyToId = findReplyTag(event.tags)?.[1]; + if (inReplyToId) { + await updateEventStats( + kysely, + inReplyToId, + ({ replies_count }) => ({ replies_count: Math.max(0, replies_count + x) }), + ); + } } /** Update stats for kind 3 event. */