diff --git a/src/queries.ts b/src/queries.ts index 74d32611..9e35277c 100644 --- a/src/queries.ts +++ b/src/queries.ts @@ -1,4 +1,4 @@ -import { NostrEvent, NostrFilter } from '@nostrify/nostrify'; +import { NostrEvent, NostrFilter, NStore } from '@nostrify/nostrify'; import Debug from '@soapbox/stickynotes/debug'; import { Conf } from '@/config.ts'; @@ -38,27 +38,31 @@ const getEvent = async ( .then(([event]) => event); }; +const queryAndUnwrapEvt = (store: NStore, filter: NostrFilter, signal = AbortSignal.timeout(5000), limit = 1) => + store.query([filter], { limit, signal } as any) + .then((events) => hydrateEvents({ events, store, signal })) + .then(([event]) => event); + /** Get a Nostr `set_medatadata` event for a user's pubkey. */ const getAuthor = async (pubkey: string, opts: GetEventOpts = {}): Promise => { const store = await Storages.db(); const { signal = AbortSignal.timeout(1000) } = opts; - - return await store.query([{ authors: [pubkey], kinds: [0], limit: 1 }], { limit: 1, signal }) - .then((events) => hydrateEvents({ events, store, signal })) - .then(([event]) => event); + return await queryAndUnwrapEvt(store, { authors: [pubkey], kinds: [0], limit: 1 }, signal); }; const getAuthorFallback = async (pubkey: string, opts: GetEventOpts = {}): Promise => { const author = await getAuthor(pubkey, opts); - const { signal = AbortSignal.timeout(1000) } = opts; - if (author) return author; - const pool = await Storages.client(); - return await pool.query([{ authors: [pubkey], kinds: [0], limit: 1 }], { limit: 1, signal }) - .then((events) => hydrateEvents({ events, store: pool, signal })) - .then(([event]) => event); + const pool = await Storages.kind0Finder(); + const res = await queryAndUnwrapEvt(pool, { authors: [pubkey], kinds: [0], limit: 1 }, opts.signal); + if (res) { + const store = await Storages.db(); + store.event(res); + } + + return res; }; /** Get users the given pubkey follows. */ diff --git a/src/storages.ts b/src/storages.ts index f8f206d1..6f93ab0f 100644 --- a/src/storages.ts +++ b/src/storages.ts @@ -15,6 +15,7 @@ export class Storages { private static _client: Promise | undefined; private static _pubsub: Promise | undefined; private static _search: Promise | undefined; + private static _kind0Finder: PoolStore | undefined; /** SQLite database to store events this Ditto server cares about. */ public static async db(): Promise { @@ -43,6 +44,31 @@ export class Storages { return this._pubsub; } + public static async kind0Finder(): Promise { + if (this._kind0Finder) return this._kind0Finder; + const worker = new Worker('https://unpkg.com/nostr-relaypool2@0.6.34/lib/nostr-relaypool.worker.js', { + type: 'module', + }); + + const DEFAULT_RELAYS = [ + 'wss://nos.lol', + 'wss://relay.damus.io', + 'wss://purplepag.es', + 'wss://nostr.mom', + 'wss://relay.snort.social', + 'wss://relay.primal.net', + ]; + + // @ts-ignore Wrong types. + const pool = new RelayPoolWorker(worker, DEFAULT_RELAYS, { + autoReconnect: true, + skipVerification: false, + logErrorsAndNotices: false, + }); + + return this._kind0Finder = new PoolStore({ pool, relays: DEFAULT_RELAYS }); + } + /** Relay pool storage. */ public static async client(): Promise { if (!this._client) {