From ebeb150463d5ff211fd7c9e68c1f440ff327843a Mon Sep 17 00:00:00 2001 From: "P. Reis" Date: Thu, 19 Sep 2024 19:37:13 -0300 Subject: [PATCH] refactor: use search in author_stats --- src/pipeline.ts | 4 ++-- src/storages/hydrate.ts | 1 + src/utils/stats.test.ts | 11 ++++++++++- src/utils/stats.ts | 13 ++++++++++--- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/pipeline.ts b/src/pipeline.ts index aaa6ca07..d56653c4 100644 --- a/src/pipeline.ts +++ b/src/pipeline.ts @@ -157,8 +157,8 @@ async function parseMetadata(event: NostrEvent, signal: AbortSignal): Promise oc.column('pubkey').doUpdateSet({ search })) .execute(); } diff --git a/src/storages/hydrate.ts b/src/storages/hydrate.ts index 7b11cfb8..5948018b 100644 --- a/src/storages/hydrate.ts +++ b/src/storages/hydrate.ts @@ -303,6 +303,7 @@ async function gatherAuthorStats( followers_count: Math.max(0, row.followers_count), following_count: Math.max(0, row.following_count), notes_count: Math.max(0, row.notes_count), + search: row.search, })); } diff --git a/src/utils/stats.test.ts b/src/utils/stats.test.ts index 69633ae3..797f78da 100644 --- a/src/utils/stats.test.ts +++ b/src/utils/stats.test.ts @@ -171,7 +171,16 @@ Deno.test('countAuthorStats counts author stats from the database', async () => await db.store.event(genEvent({ kind: 1, content: 'yolo' }, sk)); await db.store.event(genEvent({ kind: 3, tags: [['p', pubkey]] })); - const stats = await countAuthorStats(db.store, pubkey); + await db.kysely.insertInto('author_stats').values({ + pubkey, + search: 'Yolo Lolo', + notes_count: 0, + followers_count: 0, + following_count: 0, + }).onConflict((oc) => oc.column('pubkey').doUpdateSet({ 'search': 'baka' })) + .execute(); + + const stats = await countAuthorStats({ store: db.store, pubkey, kysely: db.kysely }); assertEquals(stats!.notes_count, 2); assertEquals(stats!.followers_count, 1); diff --git a/src/utils/stats.ts b/src/utils/stats.ts index e4d4d3f2..4946be3a 100644 --- a/src/utils/stats.ts +++ b/src/utils/stats.ts @@ -194,6 +194,7 @@ export async function updateAuthorStats( followers_count: 0, following_count: 0, notes_count: 0, + search: '', }; const prev = await kysely @@ -268,8 +269,7 @@ export async function updateEventStats( /** Calculate author stats from the database. */ export async function countAuthorStats( - store: SetRequired, - pubkey: string, + { pubkey, kysely, store }: RefreshAuthorStatsOpts, ): Promise { const [{ count: followers_count }, { count: notes_count }, [followList]] = await Promise.all([ store.count([{ kinds: [3], '#p': [pubkey] }]), @@ -277,11 +277,18 @@ export async function countAuthorStats( store.query([{ kinds: [3], authors: [pubkey], limit: 1 }]), ]); + const [{ search }] = await kysely + .selectFrom('author_stats') + .select('search') + .where('pubkey', '=', [pubkey]) + .execute(); + return { pubkey, followers_count, following_count: getTagSet(followList?.tags ?? [], 'p').size, notes_count, + search, }; } @@ -295,7 +302,7 @@ export interface RefreshAuthorStatsOpts { export async function refreshAuthorStats( { pubkey, kysely, store }: RefreshAuthorStatsOpts, ): Promise { - const stats = await countAuthorStats(store, pubkey); + const stats = await countAuthorStats({ store, pubkey, kysely }); await kysely.insertInto('author_stats') .values(stats)