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;
replies_count: number;
reposts_count: number;
reactions_count: number;
reactions: string;
}

View File

@ -5,14 +5,8 @@ export async function up(db: Kysely<any>): Promise<void> {
.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<any>): Promise<void> {
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,
reposts_count: Math.max(0, row.reposts_count),
replies_count: Math.max(0, row.replies_count),
reactions_count: Math.max(0, row.reactions_count),
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);
assertEquals(stats!.reactions, JSON.stringify({ '+': 1, '😂': 1 }));
assertEquals(stats!.reactions_count, 2);
});
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 {
reactions: JSON.stringify(data),
reactions_count: count,
};
});
}
@ -142,7 +146,7 @@ export async function updateAuthorStats(
pubkey: string,
fn: (prev: DittoTables['author_stats']) => UpdateObject<DittoTables, 'author_stats'>,
): Promise<void> {
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<DittoTables, 'event_stats'>,
): Promise<void> {
const empty = {
const empty: DittoTables['event_stats'] = {
event_id: eventId,
replies_count: 0,
reposts_count: 0,