diff --git a/src/stats.ts b/src/stats.ts index d0d14c0..15cf5e2 100644 --- a/src/stats.ts +++ b/src/stats.ts @@ -1,21 +1,24 @@ -import { db, type PubkeyStatsRow } from '@/db.ts'; +import { db, type EventStatsRow, type PubkeyStatsRow } from '@/db.ts'; import { Event } from '@/deps.ts'; type PubkeyStat = keyof Omit; +type EventStat = keyof Omit; /** Store stats for the event in LMDB. */ function updateStats(event: Event) { - return updateStatsQuery(event).execute(); + return updateStatsQuery(event)?.execute(); } -async function updateStatsQuery(event: Event) { +function updateStatsQuery(event: Event) { + const firstE = findFirstTag(event, 'e'); + switch (event.kind) { case 1: return incrementPubkeyStatQuery(event.pubkey, 'notes_count', 1); case 6: - return await incrementMentionedEvent(event, 'reposts'); + return firstE ? incrementEventStatQuery(firstE, 'reposts_count', 1) : undefined; case 7: - return await incrementMentionedEvent(event, 'reactions'); + return firstE ? incrementEventStatQuery(firstE, 'reactions_count', 1) : undefined; } } @@ -40,6 +43,27 @@ function incrementPubkeyStatQuery(pubkey: string, stat: PubkeyStat, diff: number ); } +function incrementEventStatQuery(eventId: string, stat: EventStat, diff: number) { + const row: EventStatsRow = { + event_id: eventId, + replies_count: 0, + reposts_count: 0, + reactions_count: 0, + }; + + row[stat] = diff; + + return db.insertInto('event_stats') + .values(row) + .onConflict((oc) => + oc + .column('event_id') + .doUpdateSet((eb) => ({ + [stat]: eb(stat, '+', diff), + })) + ); +} + function findFirstTag({ tags }: Event, name: string): string | undefined { return tags.find(([n]) => n === name)?.[1]; }