From f6fe777e78b3e18b610af2983cd4e30b3298d682 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 11 Feb 2025 21:49:58 -0600 Subject: [PATCH] Remove pubkey_domains table --- src/db/DittoTables.ts | 7 ------- src/db/migrations/048_rm_pubkey_domains.ts | 22 ++++++++++++++++++++++ src/interfaces/DittoEvent.ts | 1 - src/pipeline.ts | 9 --------- src/storages/EventsDB.test.ts | 12 ++++++++++-- src/storages/EventsDB.ts | 4 ++-- src/storages/InternalRelay.ts | 2 +- 7 files changed, 35 insertions(+), 22 deletions(-) create mode 100644 src/db/migrations/048_rm_pubkey_domains.ts diff --git a/src/db/DittoTables.ts b/src/db/DittoTables.ts index 19ea6e1b..ea326724 100644 --- a/src/db/DittoTables.ts +++ b/src/db/DittoTables.ts @@ -7,7 +7,6 @@ export interface DittoTables extends NPostgresSchema { author_stats: AuthorStatsRow; domain_favicons: DomainFaviconRow; event_stats: EventStatsRow; - pubkey_domains: PubkeyDomainRow; event_zaps: EventZapRow; push_subscriptions: PushSubscriptionRow; } @@ -45,12 +44,6 @@ interface AuthTokenRow { created_at: Date; } -interface PubkeyDomainRow { - pubkey: string; - domain: string; - last_updated_at: number; -} - interface DomainFaviconRow { domain: string; favicon: string; diff --git a/src/db/migrations/048_rm_pubkey_domains.ts b/src/db/migrations/048_rm_pubkey_domains.ts new file mode 100644 index 00000000..20938159 --- /dev/null +++ b/src/db/migrations/048_rm_pubkey_domains.ts @@ -0,0 +1,22 @@ +import { Kysely } from 'kysely'; + +export async function up(db: Kysely): Promise { + await db.schema.dropTable('pubkey_domains').execute(); +} + +export async function down(db: Kysely): Promise { + await db.schema + .createTable('pubkey_domains') + .ifNotExists() + .addColumn('pubkey', 'text', (col) => col.primaryKey()) + .addColumn('domain', 'text', (col) => col.notNull()) + .addColumn('last_updated_at', 'integer', (col) => col.notNull().defaultTo(0)) + .execute(); + + await db.schema + .createIndex('pubkey_domains_domain_index') + .on('pubkey_domains') + .column('domain') + .ifNotExists() + .execute(); +} diff --git a/src/interfaces/DittoEvent.ts b/src/interfaces/DittoEvent.ts index bca65856..d1b0c280 100644 --- a/src/interfaces/DittoEvent.ts +++ b/src/interfaces/DittoEvent.ts @@ -27,7 +27,6 @@ export interface EventStats { /** Internal Event representation used by Ditto, including extra keys. */ export interface DittoEvent extends NostrEvent { author?: DittoEvent; - author_domain?: string; author_stats?: AuthorStats; event_stats?: EventStats; mentions?: DittoEvent[]; diff --git a/src/pipeline.ts b/src/pipeline.ts index 31912530..4fcd43bf 100644 --- a/src/pipeline.ts +++ b/src/pipeline.ts @@ -161,15 +161,6 @@ function isProtectedEvent(event: NostrEvent): boolean { /** Hydrate the event with the user, if applicable. */ async function hydrateEvent(event: DittoEvent, signal: AbortSignal): Promise { await hydrateEvents({ events: [event], store: await Storages.db(), signal }); - - const kysely = await Storages.kysely(); - const domain = await kysely - .selectFrom('pubkey_domains') - .select('domain') - .where('pubkey', '=', event.pubkey) - .executeTakeFirst(); - - event.author_domain = domain?.domain; } /** Maybe store the event, if eligible. */ diff --git a/src/storages/EventsDB.test.ts b/src/storages/EventsDB.test.ts index 70be622e..810907be 100644 --- a/src/storages/EventsDB.test.ts +++ b/src/storages/EventsDB.test.ts @@ -47,8 +47,16 @@ Deno.test('query events with domain search filter', async () => { assertEquals(await store.query([{ search: '' }]), [event1]); await kysely - .insertInto('pubkey_domains') - .values({ pubkey: event1.pubkey, domain: 'localhost:4036', last_updated_at: event1.created_at }) + .insertInto('author_stats') + .values({ + pubkey: event1.pubkey, + nip05_domain: 'localhost:4036', + nip05_last_verified_at: event1.created_at, + followers_count: 0, + following_count: 0, + notes_count: 0, + search: '', + }) .execute(); assertEquals(await store.query([{ kinds: [1], search: 'domain:localhost:4036' }]), [event1]); diff --git a/src/storages/EventsDB.ts b/src/storages/EventsDB.ts index d9eea001..a96a2ba3 100644 --- a/src/storages/EventsDB.ts +++ b/src/storages/EventsDB.ts @@ -371,9 +371,9 @@ class EventsDB extends NPostgres { if (domains.size) { let query = this.opts.kysely - .selectFrom('pubkey_domains') + .selectFrom('author_stats') .select('pubkey') - .where('domain', 'in', [...domains]); + .where('nip05_domain', 'in', [...domains]); if (filter.authors) { query = query.where('pubkey', 'in', filter.authors); diff --git a/src/storages/InternalRelay.ts b/src/storages/InternalRelay.ts index 4f38c863..746af8a5 100644 --- a/src/storages/InternalRelay.ts +++ b/src/storages/InternalRelay.ts @@ -61,7 +61,7 @@ export class InternalRelay implements NRelay { typeof t === 'object' && t.key === 'domain' ) as { key: 'domain'; value: string } | undefined)?.value; - if (domain === event.author_domain) { + if (domain === event.author_stats?.nip05_domain) { machina.push(purifyEvent(event)); break; }