Try switching to NDK

This commit is contained in:
Alex Gleason 2023-12-18 14:02:51 -06:00
parent 44689ac781
commit 6ac7d91ce1
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
3 changed files with 15 additions and 18 deletions

View file

@ -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';

View file

@ -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<void> {
console.info(`firehose: Event<${event.kind}> ${event.id}`);

View file

@ -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 };