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 event1 from '~/fixtures/events/event-1.json' with { type: 'json' };
import { EventsDB } from '@/storages/events-db.ts'; import { EventsDB } from '@/storages/events-db.ts';
import { PubkeyDomain } from '@/interfaces/PubkeyDomain.ts';
const eventsDB = new EventsDB(db); const eventsDB = new EventsDB(db);
const kv = await Deno.openKv();
Deno.test('count filters', async () => { Deno.test('count filters', async () => {
assertEquals((await eventsDB.count([{ kinds: [1] }])).count, 0); 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: 'domain:localhost:8000' }]), []);
assertEquals(await eventsDB.query([{ search: '' }]), [event1]); assertEquals(await eventsDB.query([{ search: '' }]), [event1]);
await db /* ALWAYS WRITE THE TYPE OUT WITH THE ENTRY! */
.insertInto('pubkey_domains') const entry: PubkeyDomain = {
.values({ pubkey: event1.pubkey, domain: 'localhost:8000', last_updated_at: event1.created_at }) updated_at: event1.created_at,
.execute(); 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:localhost:8000' }]), [event1]);
assertEquals(await eventsDB.query([{ kinds: [1], search: 'domain:example.com' }]), []); 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 { isNostrId, isURL } from '@/utils.ts';
import { abortError } from '@/utils/abort.ts'; import { abortError } from '@/utils/abort.ts';
const kv = await Deno.openKv();
/** Function to decide whether or not to index a tag. */ /** Function to decide whether or not to index a tag. */
type TagCondition = ({ event, count, value }: { type TagCondition = ({ event, count, value }: {
event: DittoEvent; event: DittoEvent;
@ -235,26 +237,21 @@ class EventsDB implements NStore {
typeof t === 'object' && t.key === 'domain' typeof t === 'object' && t.key === 'domain'
) as { key: 'domain'; value: string } | undefined)?.value; ) as { key: 'domain'; value: string } | undefined)?.value;
const pubkeys = [];
if (domain) { if (domain) {
const query = this.#db for await (const rec of kv.list<string>({ prefix: ['pubkeys_for_domain', domain] })) {
.selectFrom('pubkey_domains') const pubkey = rec.key[2] as string;
.select('pubkey') if (filter.authors && !filter.authors.includes(pubkey)) {
.where('domain', '=', domain); continue;
}
if (filter.authors) { pubkeys.push(pubkey);
query.where('pubkey', 'in', filter.authors);
} }
const pubkeys = await query
.execute()
.then((rows) =>
rows.map((row) => row.pubkey)
);
filter.authors = pubkeys; filter.authors = pubkeys;
} }
filter.search = tokens.filter((t) => typeof t === 'string').join(' '); filter.search = tokens.filter((t) =>
typeof t === 'string'
).join(' ');
} }
} }