mirror of
https://github.com/aljazceru/ditto.git
synced 2026-01-18 04:44:24 +01:00
refactor: use search in author_stats
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user