queries: make getAuthor use memorelay

This commit is contained in:
Alex Gleason 2023-12-27 23:55:42 -06:00
parent d40b4a509e
commit f75cd21159
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
3 changed files with 25 additions and 6 deletions

View File

@ -14,8 +14,12 @@ interface DittoFilter<K extends number = number> extends Filter<K> {
relations?: Relation[]; relations?: Relation[];
} }
/** Microfilter to get one specific event by ID. */
type IdMicrofilter = { ids: [Event['id']] };
/** Microfilter to get an author. */
type AuthorMicrofilter = { kinds: [0]; authors: [Event['pubkey']] };
/** Filter to get one specific event. */ /** Filter to get one specific event. */
type MicroFilter = { ids: [Event['id']] } | { kinds: [0]; authors: [Event['pubkey']] }; type MicroFilter = IdMicrofilter | AuthorMicrofilter;
/** Additional options to apply to the whole subscription. */ /** Additional options to apply to the whole subscription. */
interface GetFiltersOpts { interface GetFiltersOpts {
@ -89,11 +93,13 @@ function isMicrofilter(filter: Filter): filter is MicroFilter {
} }
export { export {
type AuthorMicrofilter,
type DittoFilter, type DittoFilter,
eventToMicroFilter, eventToMicroFilter,
getFilterId, getFilterId,
type GetFiltersOpts, type GetFiltersOpts,
getMicroFilters, getMicroFilters,
type IdMicrofilter,
isMicrofilter, isMicrofilter,
matchDittoFilters, matchDittoFilters,
type MicroFilter, type MicroFilter,

View File

@ -1,8 +1,9 @@
import * as eventsDB from '@/db/events.ts'; import * as eventsDB from '@/db/events.ts';
import { type Event, findReplyTag } from '@/deps.ts'; import { type Event, findReplyTag } from '@/deps.ts';
import { type DittoFilter, type Relation } from '@/filter.ts'; import { AuthorMicrofilter, type DittoFilter, type Relation } from '@/filter.ts';
import * as mixer from '@/mixer.ts'; import * as mixer from '@/mixer.ts';
import { reqmeister } from '@/reqmeister.ts'; import { reqmeister } from '@/reqmeister.ts';
import { memorelay } from '@/db/memorelay.ts';
interface GetEventOpts<K extends number> { interface GetEventOpts<K extends number> {
/** Signal to abort the request. */ /** Signal to abort the request. */
@ -30,13 +31,22 @@ const getEvent = async <K extends number = number>(
/** Get a Nostr `set_medatadata` event for a user's pubkey. */ /** Get a Nostr `set_medatadata` event for a user's pubkey. */
const getAuthor = async (pubkey: string, opts: GetEventOpts<0> = {}): Promise<Event<0> | undefined> => { const getAuthor = async (pubkey: string, opts: GetEventOpts<0> = {}): Promise<Event<0> | undefined> => {
const { relations, signal = AbortSignal.timeout(1000) } = opts; const { relations, signal = AbortSignal.timeout(1000) } = opts;
const microfilter: AuthorMicrofilter = { kinds: [0], authors: [pubkey] };
const event = await eventsDB.getFilters( let event: Event<0> | undefined;
[event] = await memorelay.getFilters([microfilter], opts);
if (event) return event;
[event] = await eventsDB.getFilters(
[{ authors: [pubkey], relations, kinds: [0], limit: 1 }], [{ authors: [pubkey], relations, kinds: [0], limit: 1 }],
{ limit: 1, signal }, { limit: 1, signal },
).then(([event]) => event) || await reqmeister.req({ kinds: [0], authors: [pubkey] }).catch(() => {}); );
return event; if (event) return event;
return reqmeister.req({ kinds: [0], authors: [pubkey] }).catch(() => undefined);
}; };
/** Get users the given pubkey follows. */ /** Get users the given pubkey follows. */

View File

@ -1,6 +1,6 @@
import * as client from '@/client.ts'; import * as client from '@/client.ts';
import { Debug, type Event, EventEmitter, type Filter } from '@/deps.ts'; import { Debug, type Event, EventEmitter, type Filter } from '@/deps.ts';
import { eventToMicroFilter, getFilterId, type MicroFilter } from '@/filter.ts'; import { AuthorMicrofilter, eventToMicroFilter, getFilterId, IdMicrofilter, type MicroFilter } from '@/filter.ts';
import { Time } from '@/utils/time.ts'; import { Time } from '@/utils/time.ts';
const debug = Debug('ditto:reqmeister'); const debug = Debug('ditto:reqmeister');
@ -70,6 +70,9 @@ class Reqmeister extends EventEmitter<{ [filterId: string]: (event: Event) => an
this.#perform(); this.#perform();
} }
req(filter: IdMicrofilter, relays?: WebSocket['url'][]): Promise<Event>;
req(filter: AuthorMicrofilter, relays?: WebSocket['url'][]): Promise<Event<0>>;
req(filter: MicroFilter, relays?: WebSocket['url'][]): Promise<Event>;
req(filter: MicroFilter, relays: WebSocket['url'][] = []): Promise<Event> { req(filter: MicroFilter, relays: WebSocket['url'][] = []): Promise<Event> {
const filterId = getFilterId(filter); const filterId = getFilterId(filter);
this.#queue.push([filterId, filter, relays]); this.#queue.push([filterId, filter, relays]);