Make streaming by domain work

This commit is contained in:
Alex Gleason 2024-03-20 13:42:59 -05:00
parent b4c05c4ba3
commit 07ebb490d2
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
3 changed files with 30 additions and 4 deletions

View File

@ -17,6 +17,7 @@ export interface EventStats {
/** Internal Event representation used by Ditto, including extra keys. */ /** Internal Event representation used by Ditto, including extra keys. */
export interface DittoEvent extends NostrEvent { export interface DittoEvent extends NostrEvent {
author?: DittoEvent; author?: DittoEvent;
author_domain?: string;
author_stats?: AuthorStats; author_stats?: AuthorStats;
event_stats?: EventStats; event_stats?: EventStats;
d_author?: DittoEvent; d_author?: DittoEvent;

View File

@ -57,6 +57,14 @@ async function encounterEvent(event: NostrEvent, signal: AbortSignal): Promise<b
async function hydrateEvent(event: DittoEvent): Promise<void> { async function hydrateEvent(event: DittoEvent): Promise<void> {
const [user] = await eventsDB.query([{ kinds: [30361], authors: [Conf.pubkey], '#d': [event.pubkey], limit: 1 }]); const [user] = await eventsDB.query([{ kinds: [30361], authors: [Conf.pubkey], '#d': [event.pubkey], limit: 1 }]);
event.user = user; 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. */ /** Maybe store the event, if eligible. */

View File

@ -1,5 +1,5 @@
import { NostrFilter } from '@soapbox/nspec'; import { NIP50, NostrFilter } from '@soapbox/nspec';
import { Machina, matchFilters, type NostrEvent } from '@/deps.ts'; import { Machina, matchFilter, type NostrEvent } from '@/deps.ts';
import { type DittoEvent } from '@/interfaces/DittoEvent.ts'; import { type DittoEvent } from '@/interfaces/DittoEvent.ts';
class Subscription implements AsyncIterable<NostrEvent> { class Subscription implements AsyncIterable<NostrEvent> {
@ -16,8 +16,25 @@ class Subscription implements AsyncIterable<NostrEvent> {
} }
matches(event: DittoEvent): boolean { matches(event: DittoEvent): boolean {
// TODO: Match `search` field. for (const filter of this.filters) {
return matchFilters(this.filters, event); 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() { close() {