From 6ac7d91ce13824cebad2f4e9d9941e4a56bf4f50 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 18 Dec 2023 14:02:51 -0600 Subject: [PATCH] Try switching to NDK --- src/deps.ts | 1 + src/firehose.ts | 16 +++++++++------- src/pool.ts | 16 +++++----------- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/deps.ts b/src/deps.ts index a6b893cd..e682a8f4 100644 --- a/src/deps.ts +++ b/src/deps.ts @@ -81,5 +81,6 @@ export { Machina } from 'https://gitlab.com/soapbox-pub/nostr-machina/-/raw/08a1 export * as Sentry from 'https://deno.land/x/sentry@7.78.0/index.js'; export { sentry as sentryMiddleware } from 'npm:@hono/sentry@^1.0.0'; export * as Comlink from 'npm:comlink@^4.4.1'; +export { default as NDK, NDKEvent, NDKRelaySet } from 'npm:@soapbox.pub/ndk@^2.3.0'; export type * as TypeFest from 'npm:type-fest@^4.3.0'; diff --git a/src/firehose.ts b/src/firehose.ts index ea6be62a..af067b2e 100644 --- a/src/firehose.ts +++ b/src/firehose.ts @@ -1,20 +1,22 @@ -import { type Event } from '@/deps.ts'; -import { activeRelays, pool } from '@/pool.ts'; +import { type Event, NDKRelaySet } from '@/deps.ts'; +import { activeRelays, ndk } from '@/pool.ts'; import { nostrNow } from '@/utils.ts'; import * as pipeline from './pipeline.ts'; +console.log(`pool: connecting to ${activeRelays.length} 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( +const sub = ndk.subscribe( [{ kinds: [0, 1, 3, 5, 6, 7, 10002], limit: 0, since: nostrNow() }], - activeRelays, - handleEvent, - undefined, - undefined, + {}, + NDKRelaySet.fromRelayUrls(activeRelays, ndk), ); +sub.on('event', handleEvent); + /** Handle events through the firehose pipeline. */ function handleEvent(event: Event): Promise { console.info(`firehose: Event<${event.kind}> ${event.id}`); diff --git a/src/pool.ts b/src/pool.ts index 07ac6b62..c5437c9f 100644 --- a/src/pool.ts +++ b/src/pool.ts @@ -1,20 +1,14 @@ import { getActiveRelays } from '@/db/relays.ts'; -import { type Event, RelayPool } from '@/deps.ts'; +import { type Event, NDK, NDKEvent, NDKRelaySet } from '@/deps.ts'; const activeRelays = await getActiveRelays(); -console.log(`pool: connecting to ${activeRelays.length} relays.`); - -const pool = new RelayPool(activeRelays, { - // The pipeline verifies events. - skipVerification: true, - // The logging feature overwhelms the CPU and creates too many logs. - logErrorsAndNotices: false, -}); +const ndk = new NDK(); /** Publish an event to the given relays, or the entire pool. */ function publish(event: Event, relays: string[] = activeRelays) { - return pool.publish(event, relays); + const relaySet = NDKRelaySet.fromRelayUrls(relays, ndk); + return new NDKEvent(ndk, event).publish(relaySet); } -export { activeRelays, pool, publish }; +export { activeRelays, ndk, publish };