Merge branch 'replies-count' into 'main'

stats: track replies_count

See merge request soapbox-pub/ditto!347
This commit is contained in:
Alex Gleason 2024-06-02 17:41:03 +00:00
commit 95620c3dd7
2 changed files with 28 additions and 1 deletions

View File

@ -17,6 +17,24 @@ Deno.test('updateStats with kind 1 increments notes count', async () => {
assertEquals(stats!.notes_count, 1);
});
Deno.test('updateStats with kind 1 increments replies count', async () => {
await using db = await getTestDB();
const sk = generateSecretKey();
const note = genEvent({ kind: 1 }, sk);
await updateStats({ ...db, event: note });
await db.store.event(note);
const reply = genEvent({ kind: 1, tags: [['e', note.id]] }, sk);
await updateStats({ ...db, event: reply });
await db.store.event(reply);
const stats = await getEventStats(db.kysely, note.id);
assertEquals(stats!.replies_count, 1);
});
Deno.test('updateStats with kind 5 decrements notes count', async () => {
await using db = await getTestDB();

View File

@ -3,7 +3,7 @@ import { Kysely, UpdateObject } from 'kysely';
import { SetRequired } from 'type-fest';
import { DittoTables } from '@/db/DittoTables.ts';
import { getTagSet } from '@/utils/tags.ts';
import { findReplyTag, getTagSet } from '@/utils/tags.ts';
import { Conf } from '@/config.ts';
interface UpdateStatsOpts {
@ -33,6 +33,15 @@ export async function updateStats({ event, kysely, store, x = 1 }: UpdateStatsOp
/** Update stats for kind 1 event. */
async function handleEvent1(kysely: Kysely<DittoTables>, event: NostrEvent, x: number): Promise<void> {
await updateAuthorStats(kysely, event.pubkey, ({ notes_count }) => ({ notes_count: Math.max(0, notes_count + x) }));
const inReplyToId = findReplyTag(event.tags)?.[1];
if (inReplyToId) {
await updateEventStats(
kysely,
inReplyToId,
({ replies_count }) => ({ replies_count: Math.max(0, replies_count + x) }),
);
}
}
/** Update stats for kind 3 event. */