diff --git a/src/db/DittoTables.ts b/src/db/DittoTables.ts index 37512cb..c2d1f86 100644 --- a/src/db/DittoTables.ts +++ b/src/db/DittoTables.ts @@ -19,6 +19,7 @@ interface EventStatsRow { event_id: string; replies_count: number; reposts_count: number; + reactions_count: number; reactions: string; } diff --git a/src/db/migrations/022_event_stats_reactions.ts b/src/db/migrations/022_event_stats_reactions.ts index 9a89296..0bc6914 100644 --- a/src/db/migrations/022_event_stats_reactions.ts +++ b/src/db/migrations/022_event_stats_reactions.ts @@ -5,14 +5,8 @@ export async function up(db: Kysely): Promise { .alterTable('event_stats') .addColumn('reactions', 'text', (col) => col.defaultTo('{}')) .execute(); - - await db.schema - .alterTable('event_stats') - .dropColumn('reactions_count') - .execute(); } export async function down(db: Kysely): Promise { await db.schema.alterTable('event_stats').dropColumn('reactions').execute(); - await db.schema.alterTable('event_stats').addColumn('reactions_count', 'integer').execute(); } diff --git a/src/storages/hydrate.ts b/src/storages/hydrate.ts index 1c7b9b3..c7c8bb3 100644 --- a/src/storages/hydrate.ts +++ b/src/storages/hydrate.ts @@ -319,6 +319,7 @@ async function gatherEventStats(events: DittoEvent[]): Promise { const stats = await getEventStats(db.kysely, note.id); assertEquals(stats!.reactions, JSON.stringify({ '+': 1, '😂': 1 })); + assertEquals(stats!.reactions_count, 2); }); Deno.test('updateStats with kind 5 decrements reactions count', async () => { diff --git a/src/utils/stats.ts b/src/utils/stats.ts index cc05917..61d5e9a 100644 --- a/src/utils/stats.ts +++ b/src/utils/stats.ts @@ -103,8 +103,12 @@ async function handleEvent7(kysely: Kysely, event: NostrEvent, x: n } } + // Total reactions count. + const count = Object.values(data).reduce((result, value) => result + value, 0); + return { reactions: JSON.stringify(data), + reactions_count: count, }; }); } @@ -142,7 +146,7 @@ export async function updateAuthorStats( pubkey: string, fn: (prev: DittoTables['author_stats']) => UpdateObject, ): Promise { - const empty = { + const empty: DittoTables['author_stats'] = { pubkey, followers_count: 0, following_count: 0, @@ -183,7 +187,7 @@ export async function updateEventStats( eventId: string, fn: (prev: DittoTables['event_stats']) => UpdateObject, ): Promise { - const empty = { + const empty: DittoTables['event_stats'] = { event_id: eventId, replies_count: 0, reposts_count: 0,