Never let stats be less than 0

This commit is contained in:
Alex Gleason 2024-05-17 11:39:21 -05:00
parent 5aacbe7af5
commit 251500fba1
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 18 additions and 2 deletions

View File

@ -251,11 +251,19 @@ async function gatherAuthorStats(events: DittoEvent[]): Promise<DittoTables['aut
} }
const kysely = await DittoDB.getInstance(); const kysely = await DittoDB.getInstance();
return kysely
const rows = await kysely
.selectFrom('author_stats') .selectFrom('author_stats')
.selectAll() .selectAll()
.where('pubkey', 'in', [...pubkeys]) .where('pubkey', 'in', [...pubkeys])
.execute(); .execute();
return rows.map((row) => ({
pubkey: row.pubkey,
followers_count: Math.max(0, row.followers_count),
following_count: Math.max(0, row.following_count),
notes_count: Math.max(0, row.notes_count),
}));
} }
/** Collect event stats from the events. */ /** Collect event stats from the events. */
@ -271,11 +279,19 @@ async function gatherEventStats(events: DittoEvent[]): Promise<DittoTables['even
} }
const kysely = await DittoDB.getInstance(); const kysely = await DittoDB.getInstance();
return kysely
const rows = await kysely
.selectFrom('event_stats') .selectFrom('event_stats')
.selectAll() .selectAll()
.where('event_id', 'in', [...ids]) .where('event_id', 'in', [...ids])
.execute(); .execute();
return rows.map((row) => ({
event_id: row.event_id,
reposts_count: Math.max(0, row.reposts_count),
reactions_count: Math.max(0, row.reactions_count),
replies_count: Math.max(0, row.replies_count),
}));
} }
/** Return a normalized event without any non-standard keys. */ /** Return a normalized event without any non-standard keys. */