diff --git a/src/db/DittoTables.ts b/src/db/DittoTables.ts index 642db484..c05ffe66 100644 --- a/src/db/DittoTables.ts +++ b/src/db/DittoTables.ts @@ -9,7 +9,6 @@ export interface DittoTables extends NPostgresSchema { event_stats: EventStatsRow; pubkey_domains: PubkeyDomainRow; event_zaps: EventZapRow; - author_search: AuthorSearch; } type NostrEventsRow = NPostgresSchema['nostr_events'] & { @@ -21,6 +20,7 @@ interface AuthorStatsRow { followers_count: number; following_count: number; notes_count: number; + search: string; } interface EventStatsRow { @@ -55,8 +55,3 @@ interface EventZapRow { amount_millisats: number; comment: string; } - -interface AuthorSearch { - pubkey: string; - search: string; -} diff --git a/src/db/migrations/034_move_author_search_to_author_stats.ts b/src/db/migrations/034_move_author_search_to_author_stats.ts new file mode 100644 index 00000000..6d21ca39 --- /dev/null +++ b/src/db/migrations/034_move_author_search_to_author_stats.ts @@ -0,0 +1,32 @@ +import { Kysely, sql } from 'kysely'; + +export async function up(db: Kysely): Promise { + await db.schema + .alterTable('author_stats') + .addColumn('search', 'text', (col) => col.notNull().defaultTo('')) + .execute(); + + await sql`CREATE INDEX author_stats_search_idx ON author_stats USING GIN (search gin_trgm_ops)`.execute(db); + + await db.insertInto('author_stats') + .columns(['pubkey', 'search']) + .expression( + db.selectFrom('author_search') + .select(['pubkey', 'search']), + ) + .onConflict((oc) => + oc.column('pubkey') + .doUpdateSet((eb) => ({ + search: eb.ref('excluded.search'), + })) + ) + .execute(); + + await db.schema.dropIndex('author_search_search_idx').ifExists().execute(); + await db.schema.dropTable('author_search').execute(); +} + +export async function down(db: Kysely): Promise { + await db.schema.dropIndex('author_stats_search_idx').ifExists().execute(); + await db.schema.alterTable('author_stats').dropColumn('search').execute(); +} diff --git a/src/pipeline.ts b/src/pipeline.ts index 41ff0d64..e94ed21e 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/search.test.ts b/src/utils/search.test.ts index 7dc37750..1acd2f60 100644 --- a/src/utils/search.test.ts +++ b/src/utils/search.test.ts @@ -6,9 +6,12 @@ import { getPubkeysBySearch } from '@/utils/search.ts'; Deno.test('fuzzy search works', async () => { await using db = await createTestDB(); - await db.kysely.insertInto('author_search').values({ + await db.kysely.insertInto('author_stats').values({ pubkey: '47259076c85f9240e852420d7213c95e95102f1de929fb60f33a2c32570c98c4', search: 'patrickReiis patrickdosreis.com', + notes_count: 0, + followers_count: 0, + following_count: 0, }).execute(); assertEquals(await getPubkeysBySearch(db.kysely, { q: 'pat rick', limit: 1, followedPubkeys: new Set() }), new Set()); diff --git a/src/utils/search.ts b/src/utils/search.ts index 5ca43417..e17be135 100644 --- a/src/utils/search.ts +++ b/src/utils/search.ts @@ -10,13 +10,14 @@ export async function getPubkeysBySearch( const { q, limit, followedPubkeys } = opts; let query = kysely - .selectFrom('author_search') + .selectFrom('author_stats') .select((eb) => [ 'pubkey', 'search', eb.fn('word_similarity', [sql`${q}`, 'search']).as('sml'), ]) .where(() => sql`${q} <% search`) + .orderBy(['followers_count desc']) .orderBy(['sml desc', 'search']) .limit(limit); 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..4573bb60 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,20 +269,27 @@ export async function updateEventStats( /** Calculate author stats from the database. */ export async function countAuthorStats( - store: SetRequired, - pubkey: string, + { pubkey, store }: RefreshAuthorStatsOpts, ): Promise { - const [{ count: followers_count }, { count: notes_count }, [followList]] = await Promise.all([ + const [{ count: followers_count }, { count: notes_count }, [followList], [kind0]] = await Promise.all([ store.count([{ kinds: [3], '#p': [pubkey] }]), store.count([{ kinds: [1], authors: [pubkey] }]), store.query([{ kinds: [3], authors: [pubkey], limit: 1 }]), + store.query([{ kinds: [0], authors: [pubkey], limit: 1 }]), ]); + let search: string = ''; + const metadata = n.json().pipe(n.metadata()).catch({}).safeParse(kind0?.content); + if (metadata.success) { + const { name, nip05 } = metadata.data; + search = [name, nip05].filter(Boolean).join(' ').trim(); + } return { pubkey, followers_count, following_count: getTagSet(followList?.tags ?? [], 'p').size, notes_count, + search, }; } @@ -295,7 +303,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)