make expandFilters use the new KV storage for pubkey domains

This commit is contained in:
Siddharth Singh 2024-05-05 19:46:36 +05:30
parent 0d179c099b
commit 99e9737745
No known key found for this signature in database
2 changed files with 22 additions and 19 deletions

View file

@ -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' }]), []);

View file

@ -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<string>({ 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(' ');
}
}