Add back reactions_count column so trending can still work

This commit is contained in:
Alex Gleason 2024-05-24 17:48:04 -05:00
parent f7c9a96719
commit c6dea07ac3
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
5 changed files with 9 additions and 8 deletions

View file

@ -19,6 +19,7 @@ interface EventStatsRow {
event_id: string; event_id: string;
replies_count: number; replies_count: number;
reposts_count: number; reposts_count: number;
reactions_count: number;
reactions: string; reactions: string;
} }

View file

@ -5,14 +5,8 @@ export async function up(db: Kysely<any>): Promise<void> {
.alterTable('event_stats') .alterTable('event_stats')
.addColumn('reactions', 'text', (col) => col.defaultTo('{}')) .addColumn('reactions', 'text', (col) => col.defaultTo('{}'))
.execute(); .execute();
await db.schema
.alterTable('event_stats')
.dropColumn('reactions_count')
.execute();
} }
export async function down(db: Kysely<any>): Promise<void> { export async function down(db: Kysely<any>): Promise<void> {
await db.schema.alterTable('event_stats').dropColumn('reactions').execute(); await db.schema.alterTable('event_stats').dropColumn('reactions').execute();
await db.schema.alterTable('event_stats').addColumn('reactions_count', 'integer').execute();
} }

View file

@ -319,6 +319,7 @@ async function gatherEventStats(events: DittoEvent[]): Promise<DittoTables['even
event_id: row.event_id, event_id: row.event_id,
reposts_count: Math.max(0, row.reposts_count), reposts_count: Math.max(0, row.reposts_count),
replies_count: Math.max(0, row.replies_count), replies_count: Math.max(0, row.replies_count),
reactions_count: Math.max(0, row.reactions_count),
reactions: row.reactions, reactions: row.reactions,
})); }));
} }

View file

@ -120,6 +120,7 @@ Deno.test('updateStats with kind 7 increments reactions count', async () => {
const stats = await getEventStats(db.kysely, note.id); const stats = await getEventStats(db.kysely, note.id);
assertEquals(stats!.reactions, JSON.stringify({ '+': 1, '😂': 1 })); assertEquals(stats!.reactions, JSON.stringify({ '+': 1, '😂': 1 }));
assertEquals(stats!.reactions_count, 2);
}); });
Deno.test('updateStats with kind 5 decrements reactions count', async () => { Deno.test('updateStats with kind 5 decrements reactions count', async () => {

View file

@ -103,8 +103,12 @@ async function handleEvent7(kysely: Kysely<DittoTables>, event: NostrEvent, x: n
} }
} }
// Total reactions count.
const count = Object.values(data).reduce((result, value) => result + value, 0);
return { return {
reactions: JSON.stringify(data), reactions: JSON.stringify(data),
reactions_count: count,
}; };
}); });
} }
@ -142,7 +146,7 @@ export async function updateAuthorStats(
pubkey: string, pubkey: string,
fn: (prev: DittoTables['author_stats']) => UpdateObject<DittoTables, 'author_stats'>, fn: (prev: DittoTables['author_stats']) => UpdateObject<DittoTables, 'author_stats'>,
): Promise<void> { ): Promise<void> {
const empty = { const empty: DittoTables['author_stats'] = {
pubkey, pubkey,
followers_count: 0, followers_count: 0,
following_count: 0, following_count: 0,
@ -183,7 +187,7 @@ export async function updateEventStats(
eventId: string, eventId: string,
fn: (prev: DittoTables['event_stats']) => UpdateObject<DittoTables, 'event_stats'>, fn: (prev: DittoTables['event_stats']) => UpdateObject<DittoTables, 'event_stats'>,
): Promise<void> { ): Promise<void> {
const empty = { const empty: DittoTables['event_stats'] = {
event_id: eventId, event_id: eventId,
replies_count: 0, replies_count: 0,
reposts_count: 0, reposts_count: 0,