diff --git a/.gitignore b/.gitignore index 39dbfbbb..ec391bf6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .env *.cpuprofile *.swp -deno-test.xml \ No newline at end of file +deno-test.xml +*.db \ No newline at end of file diff --git a/src/queries.ts b/src/queries.ts index 745429d6..04f0bf5f 100644 --- a/src/queries.ts +++ b/src/queries.ts @@ -38,14 +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 queryAndUnwrapEvt(store, { authors: [pubkey], kinds: [0], limit: 1 }, signal); +}; - return await store.query([{ authors: [pubkey], kinds: [0], limit: 1 }], { limit: 1, signal }) - .then((events) => hydrateEvents({ events, store, signal })) - .then(([event]) => event); +const getAuthorFallback = async (pubkey: string, opts: GetEventOpts = {}): Promise => { + const author = await getAuthor(pubkey, opts); + if (author) return author; + + 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. */ @@ -113,6 +130,7 @@ async function isLocallyFollowed(pubkey: string): Promise { export { getAncestors, getAuthor, + getAuthorFallback, getDescendants, getEvent, getFeedPubkeys, diff --git a/src/storages.ts b/src/storages.ts index 4aaca1c2..1188a912 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) { diff --git a/src/utils/lookup.ts b/src/utils/lookup.ts index 90b30c2b..0f163172 100644 --- a/src/utils/lookup.ts +++ b/src/utils/lookup.ts @@ -1,6 +1,6 @@ import { NIP05, NostrEvent, NSchema as n } from '@nostrify/nostrify'; -import { getAuthor } from '@/queries.ts'; +import { getAuthorFallback } from '@/queries.ts'; import { bech32ToPubkey } from '@/utils.ts'; import { nip05Cache } from '@/utils/nip05.ts'; import { Stickynotes } from '@soapbox/stickynotes'; @@ -13,21 +13,21 @@ export async function lookupAccount( const pubkey = await lookupPubkey(value, signal); if (pubkey) { - return getAuthor(pubkey); + return getAuthorFallback(pubkey); } } /** Resolve a bech32 or NIP-05 identifier to a pubkey. */ -export async function lookupPubkey(value: string, signal?: AbortSignal): Promise { +export async function lookupPubkey(identifier: string, signal?: AbortSignal): Promise { const console = new Stickynotes('ditto:lookup'); - if (n.bech32().safeParse(value).success) { - return bech32ToPubkey(value); + if (n.bech32().safeParse(identifier).success) { + return bech32ToPubkey(identifier); } - if (NIP05.regex().test(value)) { + if (NIP05.regex().test(identifier)) { try { - const { pubkey } = await nip05Cache.fetch(value, { signal }); + const { pubkey } = await nip05Cache.fetch(identifier, { signal }); return pubkey; } catch (e) { console.debug(e);