diff --git a/src/firehose.ts b/src/firehose.ts index b6d7ad4..a685160 100644 --- a/src/firehose.ts +++ b/src/firehose.ts @@ -4,15 +4,15 @@ import { nostrNow } from '@/utils.ts'; import * as pipeline from './pipeline.ts'; -const relays = await getActiveRelays(); -const pool = new RelayPool(relays); +const _relays = await getActiveRelays(); +const pool = new RelayPool(_relays); // This file watches events on all known relays and performs // side-effects based on them, such as trending hashtag tracking // and storing events for notifications and the home feed. pool.subscribe( [{ kinds: [0, 1, 3, 5, 6, 7, 10002], limit: 0, since: nostrNow() }], - relays, + _relays, handleEvent, undefined, undefined, @@ -26,3 +26,10 @@ function handleEvent(event: Event): Promise { .handleEvent(event) .catch(() => {}); } + +/** Publish an event to the given relays, or the entire pool. */ +function publish(event: Event, relays: string[] = _relays) { + return pool.publish(event, relays); +} + +export { publish }; diff --git a/src/pipeline.ts b/src/pipeline.ts index dddc0f6..2b24fec 100644 --- a/src/pipeline.ts +++ b/src/pipeline.ts @@ -3,6 +3,7 @@ import * as eventsDB from '@/db/events.ts'; import { addRelays } from '@/db/relays.ts'; import { findUser } from '@/db/users.ts'; import { type Event, LRUCache } from '@/deps.ts'; +import { publish } from '@/firehose.ts'; import { isEphemeralKind } from '@/kinds.ts'; import * as mixer from '@/mixer.ts'; import { isLocallyFollowed } from '@/queries.ts'; @@ -27,6 +28,7 @@ async function handleEvent(event: Event): Promise { trackRelays(event), trackHashtags(event), streamOut(event, data), + broadcast(event, data), ]); } @@ -130,6 +132,18 @@ function streamOut(event: Event, data: EventData) { } } +/** + * Publish the event to other relays. + * This should only be done in certain circumstances, like mentioning a user or publishing deletions. + */ +function broadcast(event: Event, data: EventData) { + if (!data.user || !isFresh(event)) return; + + if (event.kind === 5) { + publish(event); + } +} + /** NIP-20 command line result. */ class RelayError extends Error { constructor(prefix: 'duplicate' | 'pow' | 'blocked' | 'rate-limited' | 'invalid' | 'error', message: string) {