mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
refactor: use search in author_stats
This commit is contained in:
parent
1b6e9160ec
commit
ebeb150463
4 changed files with 23 additions and 6 deletions
|
|
@ -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 ?? '';
|
const search = result?.pubkey === event.pubkey ? [name, nip05].filter(Boolean).join(' ').trim() : name ?? '';
|
||||||
|
|
||||||
if (search) {
|
if (search) {
|
||||||
await kysely.insertInto('author_search')
|
await kysely.insertInto('author_stats')
|
||||||
.values({ pubkey: event.pubkey, search })
|
.values({ pubkey: event.pubkey, search, followers_count: 0, following_count: 0, notes_count: 0 })
|
||||||
.onConflict((oc) => oc.column('pubkey').doUpdateSet({ search }))
|
.onConflict((oc) => oc.column('pubkey').doUpdateSet({ search }))
|
||||||
.execute();
|
.execute();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -303,6 +303,7 @@ async function gatherAuthorStats(
|
||||||
followers_count: Math.max(0, row.followers_count),
|
followers_count: Math.max(0, row.followers_count),
|
||||||
following_count: Math.max(0, row.following_count),
|
following_count: Math.max(0, row.following_count),
|
||||||
notes_count: Math.max(0, row.notes_count),
|
notes_count: Math.max(0, row.notes_count),
|
||||||
|
search: row.search,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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: 1, content: 'yolo' }, sk));
|
||||||
await db.store.event(genEvent({ kind: 3, tags: [['p', pubkey]] }));
|
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!.notes_count, 2);
|
||||||
assertEquals(stats!.followers_count, 1);
|
assertEquals(stats!.followers_count, 1);
|
||||||
|
|
|
||||||
|
|
@ -194,6 +194,7 @@ export async function updateAuthorStats(
|
||||||
followers_count: 0,
|
followers_count: 0,
|
||||||
following_count: 0,
|
following_count: 0,
|
||||||
notes_count: 0,
|
notes_count: 0,
|
||||||
|
search: '',
|
||||||
};
|
};
|
||||||
|
|
||||||
const prev = await kysely
|
const prev = await kysely
|
||||||
|
|
@ -268,8 +269,7 @@ export async function updateEventStats(
|
||||||
|
|
||||||
/** Calculate author stats from the database. */
|
/** Calculate author stats from the database. */
|
||||||
export async function countAuthorStats(
|
export async function countAuthorStats(
|
||||||
store: SetRequired<NStore, 'count'>,
|
{ pubkey, kysely, store }: RefreshAuthorStatsOpts,
|
||||||
pubkey: string,
|
|
||||||
): Promise<DittoTables['author_stats']> {
|
): Promise<DittoTables['author_stats']> {
|
||||||
const [{ count: followers_count }, { count: notes_count }, [followList]] = await Promise.all([
|
const [{ count: followers_count }, { count: notes_count }, [followList]] = await Promise.all([
|
||||||
store.count([{ kinds: [3], '#p': [pubkey] }]),
|
store.count([{ kinds: [3], '#p': [pubkey] }]),
|
||||||
|
|
@ -277,11 +277,18 @@ export async function countAuthorStats(
|
||||||
store.query([{ kinds: [3], authors: [pubkey], limit: 1 }]),
|
store.query([{ kinds: [3], authors: [pubkey], limit: 1 }]),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
const [{ search }] = await kysely
|
||||||
|
.selectFrom('author_stats')
|
||||||
|
.select('search')
|
||||||
|
.where('pubkey', '=', [pubkey])
|
||||||
|
.execute();
|
||||||
|
|
||||||
return {
|
return {
|
||||||
pubkey,
|
pubkey,
|
||||||
followers_count,
|
followers_count,
|
||||||
following_count: getTagSet(followList?.tags ?? [], 'p').size,
|
following_count: getTagSet(followList?.tags ?? [], 'p').size,
|
||||||
notes_count,
|
notes_count,
|
||||||
|
search,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -295,7 +302,7 @@ export interface RefreshAuthorStatsOpts {
|
||||||
export async function refreshAuthorStats(
|
export async function refreshAuthorStats(
|
||||||
{ pubkey, kysely, store }: RefreshAuthorStatsOpts,
|
{ pubkey, kysely, store }: RefreshAuthorStatsOpts,
|
||||||
): Promise<DittoTables['author_stats']> {
|
): Promise<DittoTables['author_stats']> {
|
||||||
const stats = await countAuthorStats(store, pubkey);
|
const stats = await countAuthorStats({ store, pubkey, kysely });
|
||||||
|
|
||||||
await kysely.insertInto('author_stats')
|
await kysely.insertInto('author_stats')
|
||||||
.values(stats)
|
.values(stats)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue