refactor: use search in author_stats

This commit is contained in:
P. Reis 2024-09-19 19:37:13 -03:00
parent 1b6e9160ec
commit ebeb150463
4 changed files with 23 additions and 6 deletions

View file

@ -157,8 +157,8 @@ async function parseMetadata(event: NostrEvent, signal: AbortSignal): Promise<vo
const search = result?.pubkey === event.pubkey ? [name, nip05].filter(Boolean).join(' ').trim() : name ?? '';
if (search) {
await kysely.insertInto('author_search')
.values({ pubkey: event.pubkey, search })
await kysely.insertInto('author_stats')
.values({ pubkey: event.pubkey, search, followers_count: 0, following_count: 0, notes_count: 0 })
.onConflict((oc) => oc.column('pubkey').doUpdateSet({ search }))
.execute();
}

View file

@ -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,
}));
}

View file

@ -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);

View file

@ -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<NStore, 'count'>,
pubkey: string,
{ pubkey, kysely, store }: RefreshAuthorStatsOpts,
): Promise<DittoTables['author_stats']> {
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<DittoTables['author_stats']> {
const stats = await countAuthorStats(store, pubkey);
const stats = await countAuthorStats({ store, pubkey, kysely });
await kysely.insertInto('author_stats')
.values(stats)