From 07ebb490d23f760e483486e99ce70bfd4fe94f1b Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 20 Mar 2024 13:42:59 -0500 Subject: [PATCH] Make streaming by domain work --- src/interfaces/DittoEvent.ts | 1 + src/pipeline.ts | 8 ++++++++ src/subscription.ts | 25 +++++++++++++++++++++---- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/interfaces/DittoEvent.ts b/src/interfaces/DittoEvent.ts index ed327d4..cdd4002 100644 --- a/src/interfaces/DittoEvent.ts +++ b/src/interfaces/DittoEvent.ts @@ -17,6 +17,7 @@ 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; d_author?: DittoEvent; diff --git a/src/pipeline.ts b/src/pipeline.ts index 7fd7ee9..72b16ed 100644 --- a/src/pipeline.ts +++ b/src/pipeline.ts @@ -57,6 +57,14 @@ async function encounterEvent(event: NostrEvent, signal: AbortSignal): Promise { const [user] = await eventsDB.query([{ kinds: [30361], authors: [Conf.pubkey], '#d': [event.pubkey], limit: 1 }]); event.user = user; + + const domain = await db + .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/subscription.ts b/src/subscription.ts index abdf7cc..b288c5d 100644 --- a/src/subscription.ts +++ b/src/subscription.ts @@ -1,5 +1,5 @@ -import { NostrFilter } from '@soapbox/nspec'; -import { Machina, matchFilters, type NostrEvent } from '@/deps.ts'; +import { NIP50, NostrFilter } from '@soapbox/nspec'; +import { Machina, matchFilter, type NostrEvent } from '@/deps.ts'; import { type DittoEvent } from '@/interfaces/DittoEvent.ts'; class Subscription implements AsyncIterable { @@ -16,8 +16,25 @@ class Subscription implements AsyncIterable { } matches(event: DittoEvent): boolean { - // TODO: Match `search` field. - return matchFilters(this.filters, event); + for (const filter of this.filters) { + if (matchFilter(filter, event)) { + if (filter.search) { + const tokens = NIP50.parseInput(filter.search); + + const domain = (tokens.find((t) => + typeof t === 'object' && t.key === 'domain' + ) as { key: 'domain'; value: string } | undefined)?.value; + + if (domain) { + return domain === event.author_domain; + } + } + + return true; + } + } + + return false; } close() {