mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 03:19:46 +00:00
Try switching to NDK
This commit is contained in:
parent
44689ac781
commit
6ac7d91ce1
3 changed files with 15 additions and 18 deletions
|
|
@ -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 * 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 { sentry as sentryMiddleware } from 'npm:@hono/sentry@^1.0.0';
|
||||||
export * as Comlink from 'npm:comlink@^4.4.1';
|
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';
|
export type * as TypeFest from 'npm:type-fest@^4.3.0';
|
||||||
|
|
|
||||||
|
|
@ -1,20 +1,22 @@
|
||||||
import { type Event } from '@/deps.ts';
|
import { type Event, NDKRelaySet } from '@/deps.ts';
|
||||||
import { activeRelays, pool } from '@/pool.ts';
|
import { activeRelays, ndk } from '@/pool.ts';
|
||||||
import { nostrNow } from '@/utils.ts';
|
import { nostrNow } from '@/utils.ts';
|
||||||
|
|
||||||
import * as pipeline from './pipeline.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
|
// This file watches events on all known relays and performs
|
||||||
// side-effects based on them, such as trending hashtag tracking
|
// side-effects based on them, such as trending hashtag tracking
|
||||||
// and storing events for notifications and the home feed.
|
// 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() }],
|
[{ kinds: [0, 1, 3, 5, 6, 7, 10002], limit: 0, since: nostrNow() }],
|
||||||
activeRelays,
|
{},
|
||||||
handleEvent,
|
NDKRelaySet.fromRelayUrls(activeRelays, ndk),
|
||||||
undefined,
|
|
||||||
undefined,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
sub.on('event', handleEvent);
|
||||||
|
|
||||||
/** Handle events through the firehose pipeline. */
|
/** Handle events through the firehose pipeline. */
|
||||||
function handleEvent(event: Event): Promise<void> {
|
function handleEvent(event: Event): Promise<void> {
|
||||||
console.info(`firehose: Event<${event.kind}> ${event.id}`);
|
console.info(`firehose: Event<${event.kind}> ${event.id}`);
|
||||||
|
|
|
||||||
16
src/pool.ts
16
src/pool.ts
|
|
@ -1,20 +1,14 @@
|
||||||
import { getActiveRelays } from '@/db/relays.ts';
|
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();
|
const activeRelays = await getActiveRelays();
|
||||||
|
|
||||||
console.log(`pool: connecting to ${activeRelays.length} relays.`);
|
const ndk = new NDK();
|
||||||
|
|
||||||
const pool = new RelayPool(activeRelays, {
|
|
||||||
// The pipeline verifies events.
|
|
||||||
skipVerification: true,
|
|
||||||
// The logging feature overwhelms the CPU and creates too many logs.
|
|
||||||
logErrorsAndNotices: false,
|
|
||||||
});
|
|
||||||
|
|
||||||
/** Publish an event to the given relays, or the entire pool. */
|
/** Publish an event to the given relays, or the entire pool. */
|
||||||
function publish(event: Event, relays: string[] = activeRelays) {
|
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 };
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue