From 99e97377451a88a7f2e3053ec87f57c3f113f7bd Mon Sep 17 00:00:00 2001 From: Siddharth Singh Date: Sun, 5 May 2024 19:46:36 +0530 Subject: [PATCH] make expandFilters use the new KV storage for pubkey domains --- src/storages/events-db.test.ts | 14 ++++++++++---- src/storages/events-db.ts | 27 ++++++++++++--------------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/src/storages/events-db.test.ts b/src/storages/events-db.test.ts index dd92c1b4..6d3194c5 100644 --- a/src/storages/events-db.test.ts +++ b/src/storages/events-db.test.ts @@ -5,8 +5,10 @@ import event0 from '~/fixtures/events/event-0.json' with { type: 'json' }; import event1 from '~/fixtures/events/event-1.json' with { type: 'json' }; import { EventsDB } from '@/storages/events-db.ts'; +import { PubkeyDomain } from '@/interfaces/PubkeyDomain.ts'; const eventsDB = new EventsDB(db); +const kv = await Deno.openKv(); Deno.test('count filters', async () => { assertEquals((await eventsDB.count([{ kinds: [1] }])).count, 0); @@ -34,10 +36,14 @@ Deno.test('query events with domain search filter', async () => { assertEquals(await eventsDB.query([{ search: 'domain:localhost:8000' }]), []); assertEquals(await eventsDB.query([{ search: '' }]), [event1]); - await db - .insertInto('pubkey_domains') - .values({ pubkey: event1.pubkey, domain: 'localhost:8000', last_updated_at: event1.created_at }) - .execute(); + /* ALWAYS WRITE THE TYPE OUT WITH THE ENTRY! */ + const entry: PubkeyDomain = { + updated_at: event1.created_at, + domain: 'localhost:8000', + }; + + await kv.set(['domain_for_pubkey', event1.pubkey], entry); + await kv.set(['pubkeys_for_domain', 'localhost:8000', event1.pubkey], true); assertEquals(await eventsDB.query([{ kinds: [1], search: 'domain:localhost:8000' }]), [event1]); assertEquals(await eventsDB.query([{ kinds: [1], search: 'domain:example.com' }]), []); diff --git a/src/storages/events-db.ts b/src/storages/events-db.ts index 2fcdf161..b73abb38 100644 --- a/src/storages/events-db.ts +++ b/src/storages/events-db.ts @@ -11,6 +11,8 @@ import { purifyEvent } from '@/storages/hydrate.ts'; import { isNostrId, isURL } from '@/utils.ts'; import { abortError } from '@/utils/abort.ts'; +const kv = await Deno.openKv(); + /** Function to decide whether or not to index a tag. */ type TagCondition = ({ event, count, value }: { event: DittoEvent; @@ -235,26 +237,21 @@ class EventsDB implements NStore { typeof t === 'object' && t.key === 'domain' ) as { key: 'domain'; value: string } | undefined)?.value; + const pubkeys = []; if (domain) { - const query = this.#db - .selectFrom('pubkey_domains') - .select('pubkey') - .where('domain', '=', domain); - - if (filter.authors) { - query.where('pubkey', 'in', filter.authors); + for await (const rec of kv.list({ prefix: ['pubkeys_for_domain', domain] })) { + const pubkey = rec.key[2] as string; + if (filter.authors && !filter.authors.includes(pubkey)) { + continue; + } + pubkeys.push(pubkey); } - - const pubkeys = await query - .execute() - .then((rows) => - rows.map((row) => row.pubkey) - ); - filter.authors = pubkeys; } - filter.search = tokens.filter((t) => typeof t === 'string').join(' '); + filter.search = tokens.filter((t) => + typeof t === 'string' + ).join(' '); } }