From 937ae1eab64b776e4642829fcfa00137e821ac6c Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 8 Jan 2024 15:13:10 -0600 Subject: [PATCH] Refactor EventStore to use a more Set-like interface --- scripts/db.ts | 2 +- src/controllers/api/accounts.ts | 8 ++--- src/controllers/api/admin.ts | 4 +-- src/controllers/api/blocks.ts | 2 +- src/controllers/api/bookmarks.ts | 2 +- src/controllers/api/notifications.ts | 2 +- src/controllers/api/pleroma.ts | 2 +- src/controllers/api/search.ts | 4 +-- src/controllers/api/timelines.ts | 2 +- src/controllers/nostr/relay.ts | 4 +-- src/db/users.ts | 2 +- src/pipeline.ts | 18 +++++----- src/queries.ts | 18 +++++----- src/stats.ts | 2 +- src/storages/events-db.test.ts | 52 ++++++++++++++-------------- src/storages/events-db.ts | 8 ++--- src/storages/hydrate.ts | 2 +- src/storages/memorelay.test.ts | 8 ++--- src/storages/memorelay.ts | 12 +++---- src/storages/optimizer.ts | 16 ++++----- src/storages/pool-store.ts | 8 ++--- src/storages/reqmeister.ts | 12 +++---- src/storages/search-store.ts | 10 +++--- src/storages/types.ts | 8 ++--- src/utils/api.ts | 2 +- src/views.ts | 8 ++--- src/views/mastodon/relationships.ts | 2 +- src/views/mastodon/statuses.ts | 2 +- 28 files changed, 111 insertions(+), 111 deletions(-) diff --git a/scripts/db.ts b/scripts/db.ts index cbaca91..7893fd5 100644 --- a/scripts/db.ts +++ b/scripts/db.ts @@ -39,6 +39,6 @@ async function usersToEvents() { created_at: Math.floor(new Date(row.inserted_at).getTime() / 1000), }); - await eventsDB.storeEvent(event); + await eventsDB.add(event); } } diff --git a/src/controllers/api/accounts.ts b/src/controllers/api/accounts.ts index 34518cd..2424deb 100644 --- a/src/controllers/api/accounts.ts +++ b/src/controllers/api/accounts.ts @@ -134,7 +134,7 @@ const accountStatusesController: AppController = async (c) => { const { pinned, limit, exclude_replies, tagged } = accountStatusesQuerySchema.parse(c.req.query()); if (pinned) { - const [pinEvent] = await eventsDB.getEvents([{ kinds: [10001], authors: [pubkey], limit: 1 }]); + const [pinEvent] = await eventsDB.filter([{ kinds: [10001], authors: [pubkey], limit: 1 }]); if (pinEvent) { const pinnedEventIds = getTagSet(pinEvent.tags, 'e'); return renderStatuses(c, [...pinnedEventIds].reverse()); @@ -156,7 +156,7 @@ const accountStatusesController: AppController = async (c) => { filter['#t'] = [tagged]; } - let events = await eventsDB.getEvents([filter]); + let events = await eventsDB.filter([filter]); if (exclude_replies) { events = events.filter((event) => !findReplyTag(event)); @@ -293,7 +293,7 @@ const favouritesController: AppController = async (c) => { const pubkey = c.get('pubkey')!; const params = paginationSchema.parse(c.req.query()); - const events7 = await eventsDB.getEvents( + const events7 = await eventsDB.filter( [{ kinds: [7], authors: [pubkey], ...params }], { signal: AbortSignal.timeout(1000) }, ); @@ -302,7 +302,7 @@ const favouritesController: AppController = async (c) => { .map((event) => event.tags.find((tag) => tag[0] === 'e')?.[1]) .filter((id): id is string => !!id); - const events1 = await eventsDB.getEvents( + const events1 = await eventsDB.filter( [{ kinds: [1], ids, relations: ['author', 'event_stats', 'author_stats'] }], { signal: AbortSignal.timeout(1000), diff --git a/src/controllers/api/admin.ts b/src/controllers/api/admin.ts index b90c051..32a9733 100644 --- a/src/controllers/api/admin.ts +++ b/src/controllers/api/admin.ts @@ -39,9 +39,9 @@ const adminAccountsController: AppController = async (c) => { const { since, until, limit } = paginationSchema.parse(c.req.query()); - const events = await eventsDB.getEvents([{ kinds: [30361], authors: [Conf.pubkey], since, until, limit }]); + const events = await eventsDB.filter([{ kinds: [30361], authors: [Conf.pubkey], since, until, limit }]); const pubkeys = events.map((event) => event.tags.find(([name]) => name === 'd')?.[1]!); - const authors = await eventsDB.getEvents([{ kinds: [0], authors: pubkeys }]); + const authors = await eventsDB.filter([{ kinds: [0], authors: pubkeys }]); for (const event of events) { const d = event.tags.find(([name]) => name === 'd')?.[1]; diff --git a/src/controllers/api/blocks.ts b/src/controllers/api/blocks.ts index b45e1c6..8422915 100644 --- a/src/controllers/api/blocks.ts +++ b/src/controllers/api/blocks.ts @@ -7,7 +7,7 @@ import { renderAccounts } from '@/views.ts'; const blocksController: AppController = async (c) => { const pubkey = c.get('pubkey')!; - const [event10000] = await eventsDB.getEvents([ + const [event10000] = await eventsDB.filter([ { kinds: [10000], authors: [pubkey], limit: 1 }, ]); diff --git a/src/controllers/api/bookmarks.ts b/src/controllers/api/bookmarks.ts index 81cd011..5722d97 100644 --- a/src/controllers/api/bookmarks.ts +++ b/src/controllers/api/bookmarks.ts @@ -7,7 +7,7 @@ import { renderStatuses } from '@/views.ts'; const bookmarksController: AppController = async (c) => { const pubkey = c.get('pubkey')!; - const [event10003] = await eventsDB.getEvents([ + const [event10003] = await eventsDB.filter([ { kinds: [10003], authors: [pubkey], limit: 1 }, ]); diff --git a/src/controllers/api/notifications.ts b/src/controllers/api/notifications.ts index 43c974a..c618a6a 100644 --- a/src/controllers/api/notifications.ts +++ b/src/controllers/api/notifications.ts @@ -7,7 +7,7 @@ const notificationsController: AppController = async (c) => { const pubkey = c.get('pubkey')!; const { since, until } = paginationSchema.parse(c.req.query()); - const events = await eventsDB.getEvents( + const events = await eventsDB.filter( [{ kinds: [1], '#p': [pubkey], since, until }], { signal: AbortSignal.timeout(3000) }, ); diff --git a/src/controllers/api/pleroma.ts b/src/controllers/api/pleroma.ts index 04370e4..91c6cc7 100644 --- a/src/controllers/api/pleroma.ts +++ b/src/controllers/api/pleroma.ts @@ -6,7 +6,7 @@ import { createAdminEvent } from '@/utils/api.ts'; import { Conf } from '@/config.ts'; const frontendConfigController: AppController = async (c) => { - const [event] = await eventsDB.getEvents([{ + const [event] = await eventsDB.filter([{ kinds: [30078], authors: [Conf.pubkey], '#d': ['pub.ditto.frontendConfig'], diff --git a/src/controllers/api/search.ts b/src/controllers/api/search.ts index d18694c..5feeef6 100644 --- a/src/controllers/api/search.ts +++ b/src/controllers/api/search.ts @@ -78,7 +78,7 @@ function searchEvents({ q, type, limit, account_id }: SearchQuery, signal: Abort filter.authors = [account_id]; } - return searchStore.getEvents([filter], { signal }); + return searchStore.filter([filter], { signal }); } /** Get event kinds to search from `type` query param. */ @@ -96,7 +96,7 @@ function typeToKinds(type: SearchQuery['type']): number[] { /** Resolve a searched value into an event, if applicable. */ async function lookupEvent(query: SearchQuery, signal: AbortSignal): Promise { const filters = await getLookupFilters(query); - const [event] = await searchStore.getEvents(filters, { limit: 1, signal }); + const [event] = await searchStore.filter(filters, { limit: 1, signal }); return event; } diff --git a/src/controllers/api/timelines.ts b/src/controllers/api/timelines.ts index 47496a0..00a4a55 100644 --- a/src/controllers/api/timelines.ts +++ b/src/controllers/api/timelines.ts @@ -33,7 +33,7 @@ const hashtagTimelineController: AppController = (c) => { /** Render statuses for timelines. */ async function renderStatuses(c: AppContext, filters: DittoFilter<1>[], signal = AbortSignal.timeout(1000)) { - const events = await eventsDB.getEvents( + const events = await eventsDB.filter( filters.map((filter) => ({ ...filter, relations: ['author', 'event_stats', 'author_stats'] })), { signal }, ); diff --git a/src/controllers/nostr/relay.ts b/src/controllers/nostr/relay.ts index f1c3511..cc21b3c 100644 --- a/src/controllers/nostr/relay.ts +++ b/src/controllers/nostr/relay.ts @@ -63,7 +63,7 @@ function connectStream(socket: WebSocket) { async function handleReq([_, subId, ...rest]: ClientREQ): Promise { const filters = prepareFilters(rest); - for (const event of await eventsDB.getEvents(filters, { limit: FILTER_LIMIT })) { + for (const event of await eventsDB.filter(filters, { limit: FILTER_LIMIT })) { send(['EVENT', subId, event]); } @@ -96,7 +96,7 @@ function connectStream(socket: WebSocket) { /** Handle COUNT. Return the number of events matching the filters. */ async function handleCount([_, subId, ...rest]: ClientCOUNT): Promise { - const count = await eventsDB.countEvents(prepareFilters(rest)); + const count = await eventsDB.count(prepareFilters(rest)); send(['COUNT', subId, { count, approximate: false }]); } diff --git a/src/db/users.ts b/src/db/users.ts index 62340e1..53f5ff8 100644 --- a/src/db/users.ts +++ b/src/db/users.ts @@ -65,7 +65,7 @@ async function findUser(user: Partial): Promise { } } - const [event] = await eventsDB.getEvents([filter]); + const [event] = await eventsDB.filter([filter]); if (event) { return { diff --git a/src/pipeline.ts b/src/pipeline.ts index fb9b334..79b88c7 100644 --- a/src/pipeline.ts +++ b/src/pipeline.ts @@ -41,9 +41,9 @@ async function handleEvent(event: Event): Promise { /** Encounter the event, and return whether it has already been encountered. */ async function encounterEvent(event: Event): Promise { - const preexisting = (await memorelay.countEvents([{ ids: [event.id] }])) > 0; - memorelay.storeEvent(event); - reqmeister.storeEvent(event); + const preexisting = (await memorelay.count([{ ids: [event.id] }])) > 0; + memorelay.add(event); + reqmeister.add(event); return preexisting; } @@ -66,7 +66,7 @@ async function storeEvent(event: Event, data: EventData, opts: StoreEventOpts = const { force = false } = opts; if (force || data.user || isAdminEvent(event) || await isLocallyFollowed(event.pubkey)) { - const [deletion] = await eventsDB.getEvents( + const [deletion] = await eventsDB.filter( [{ kinds: [5], authors: [event.pubkey], '#e': [event.id], limit: 1 }], { limit: 1 }, ); @@ -75,7 +75,7 @@ async function storeEvent(event: Event, data: EventData, opts: StoreEventOpts = return Promise.reject(new RelayError('blocked', 'event was deleted')); } else { await Promise.all([ - eventsDB.storeEvent(event, { data }).catch(debug), + eventsDB.add(event, { data }).catch(debug), updateStats(event).catch(debug), ]); } @@ -88,13 +88,13 @@ async function storeEvent(event: Event, data: EventData, opts: StoreEventOpts = async function processDeletions(event: Event): Promise { if (event.kind === 5) { const ids = getTagSet(event.tags, 'e'); - const events = await eventsDB.getEvents([{ ids: [...ids] }]); + const events = await eventsDB.filter([{ ids: [...ids] }]); const deleteIds = events .filter(({ pubkey, id }) => pubkey === event.pubkey && ids.has(id)) .map((event) => event.id); - await eventsDB.deleteEvents([{ ids: deleteIds }]); + await eventsDB.deleteFilters([{ ids: deleteIds }]); } } @@ -139,7 +139,7 @@ function fetchRelatedEvents(event: Event, data: EventData) { reqmeister.req({ kinds: [0], authors: [event.pubkey] }).catch(() => {}); } for (const [name, id, relay] of event.tags) { - if (name === 'e' && !memorelay.countEvents([{ ids: [id] }])) { + if (name === 'e' && !memorelay.count([{ ids: [id] }])) { reqmeister.req({ ids: [id] }, { relays: [relay] }).catch(() => {}); } } @@ -173,7 +173,7 @@ function broadcast(event: Event, data: EventData) { if (!data.user || !isFresh(event)) return; if (event.kind === 5) { - client.storeEvent(event); + client.add(event); } } diff --git a/src/queries.ts b/src/queries.ts index 0ac61a3..3dc27d7 100644 --- a/src/queries.ts +++ b/src/queries.ts @@ -24,7 +24,7 @@ const getEvent = async ( const { kind, relations, signal = AbortSignal.timeout(1000) } = opts; const microfilter: IdMicrofilter = { ids: [id] }; - const [memoryEvent] = await memorelay.getEvents([microfilter], opts) as DittoEvent[]; + const [memoryEvent] = await memorelay.filter([microfilter], opts) as DittoEvent[]; if (memoryEvent && !relations) { debug(`getEvent: ${id.slice(0, 8)} found in memory`); @@ -36,13 +36,13 @@ const getEvent = async ( filter.kinds = [kind]; } - const dbEvent = await eventsDB.getEvents([filter], { limit: 1, signal }) + const dbEvent = await eventsDB.filter([filter], { limit: 1, signal }) .then(([event]) => event); // TODO: make this DRY-er. if (dbEvent && !dbEvent.author) { - const [author] = await memorelay.getEvents([{ kinds: [0], authors: [dbEvent.pubkey] }], opts); + const [author] = await memorelay.filter([{ kinds: [0], authors: [dbEvent.pubkey] }], opts); dbEvent.author = author; } @@ -52,7 +52,7 @@ const getEvent = async ( } if (memoryEvent && !memoryEvent.author) { - const [author] = await memorelay.getEvents([{ kinds: [0], authors: [memoryEvent.pubkey] }], opts); + const [author] = await memorelay.filter([{ kinds: [0], authors: [memoryEvent.pubkey] }], opts); memoryEvent.author = author; } @@ -76,13 +76,13 @@ const getAuthor = async (pubkey: string, opts: GetEventOpts<0> = {}): Promise event); @@ -95,7 +95,7 @@ const getAuthor = async (pubkey: string, opts: GetEventOpts<0> = {}): Promise | undefined> => { - const [event] = await eventsDB.getEvents([{ authors: [pubkey], kinds: [3], limit: 1 }], { limit: 1, signal }); + const [event] = await eventsDB.filter([{ authors: [pubkey], kinds: [3], limit: 1 }], { limit: 1, signal }); return event; }; @@ -131,7 +131,7 @@ async function getAncestors(event: Event<1>, result = [] as Event<1>[]): Promise } function getDescendants(eventId: string, signal = AbortSignal.timeout(2000)): Promise[]> { - return eventsDB.getEvents( + return eventsDB.filter( [{ kinds: [1], '#e': [eventId], relations: ['author', 'event_stats', 'author_stats'] }], { limit: 200, signal }, ); @@ -139,7 +139,7 @@ function getDescendants(eventId: string, signal = AbortSignal.timeout(2000)): Pr /** Returns whether the pubkey is followed by a local user. */ async function isLocallyFollowed(pubkey: string): Promise { - const [event] = await eventsDB.getEvents([{ kinds: [3], '#p': [pubkey], local: true, limit: 1 }], { limit: 1 }); + const [event] = await eventsDB.filter([{ kinds: [3], '#p': [pubkey], local: true, limit: 1 }], { limit: 1 }); return Boolean(event); } diff --git a/src/stats.ts b/src/stats.ts index fd08cf5..568fac6 100644 --- a/src/stats.ts +++ b/src/stats.ts @@ -125,7 +125,7 @@ function eventStatsQuery(diffs: EventStatDiff[]) { /** Get the last version of the event, if any. */ async function maybeGetPrev(event: Event): Promise> { - const [prev] = await eventsDB.getEvents([ + const [prev] = await eventsDB.filter([ { kinds: [event.kind], authors: [event.pubkey], limit: 1 }, ]); diff --git a/src/storages/events-db.test.ts b/src/storages/events-db.test.ts index 349f82b..9b56171 100644 --- a/src/storages/events-db.test.ts +++ b/src/storages/events-db.test.ts @@ -10,37 +10,37 @@ import { EventsDB } from './events-db.ts'; const eventsDB = new EventsDB(db); Deno.test('count filters', async () => { - assertEquals(await eventsDB.countEvents([{ kinds: [1] }]), 0); - await eventsDB.storeEvent(event1); - assertEquals(await eventsDB.countEvents([{ kinds: [1] }]), 1); + assertEquals(await eventsDB.count([{ kinds: [1] }]), 0); + await eventsDB.add(event1); + assertEquals(await eventsDB.count([{ kinds: [1] }]), 1); }); Deno.test('insert and filter events', async () => { - await eventsDB.storeEvent(event1); + await eventsDB.add(event1); - assertEquals(await eventsDB.getEvents([{ kinds: [1] }]), [event1]); - assertEquals(await eventsDB.getEvents([{ kinds: [3] }]), []); - assertEquals(await eventsDB.getEvents([{ since: 1691091000 }]), [event1]); - assertEquals(await eventsDB.getEvents([{ until: 1691091000 }]), []); + assertEquals(await eventsDB.filter([{ kinds: [1] }]), [event1]); + assertEquals(await eventsDB.filter([{ kinds: [3] }]), []); + assertEquals(await eventsDB.filter([{ since: 1691091000 }]), [event1]); + assertEquals(await eventsDB.filter([{ until: 1691091000 }]), []); assertEquals( - await eventsDB.getEvents([{ '#proxy': ['https://gleasonator.com/objects/8f6fac53-4f66-4c6e-ac7d-92e5e78c3e79'] }]), + await eventsDB.filter([{ '#proxy': ['https://gleasonator.com/objects/8f6fac53-4f66-4c6e-ac7d-92e5e78c3e79'] }]), [event1], ); }); Deno.test('delete events', async () => { - await eventsDB.storeEvent(event1); - assertEquals(await eventsDB.getEvents([{ kinds: [1] }]), [event1]); - await eventsDB.deleteEvents([{ kinds: [1] }]); - assertEquals(await eventsDB.getEvents([{ kinds: [1] }]), []); + await eventsDB.add(event1); + assertEquals(await eventsDB.filter([{ kinds: [1] }]), [event1]); + await eventsDB.deleteFilters([{ kinds: [1] }]); + assertEquals(await eventsDB.filter([{ kinds: [1] }]), []); }); Deno.test('query events with local filter', async () => { - await eventsDB.storeEvent(event1); + await eventsDB.add(event1); - assertEquals(await eventsDB.getEvents([{}]), [event1]); - assertEquals(await eventsDB.getEvents([{ local: true }]), []); - assertEquals(await eventsDB.getEvents([{ local: false }]), [event1]); + assertEquals(await eventsDB.filter([{}]), [event1]); + assertEquals(await eventsDB.filter([{ local: true }]), []); + assertEquals(await eventsDB.filter([{ local: false }]), [event1]); const userEvent = await buildUserEvent({ username: 'alex', @@ -48,20 +48,20 @@ Deno.test('query events with local filter', async () => { inserted_at: new Date(), admin: false, }); - await eventsDB.storeEvent(userEvent); + await eventsDB.add(userEvent); - assertEquals(await eventsDB.getEvents([{ kinds: [1], local: true }]), [event1]); - assertEquals(await eventsDB.getEvents([{ kinds: [1], local: false }]), []); + assertEquals(await eventsDB.filter([{ kinds: [1], local: true }]), [event1]); + assertEquals(await eventsDB.filter([{ kinds: [1], local: false }]), []); }); Deno.test('inserting replaceable events', async () => { - assertEquals(await eventsDB.countEvents([{ kinds: [0], authors: [event0.pubkey] }]), 0); + assertEquals(await eventsDB.count([{ kinds: [0], authors: [event0.pubkey] }]), 0); - await eventsDB.storeEvent(event0); - await assertRejects(() => eventsDB.storeEvent(event0)); - assertEquals(await eventsDB.countEvents([{ kinds: [0], authors: [event0.pubkey] }]), 1); + await eventsDB.add(event0); + await assertRejects(() => eventsDB.add(event0)); + assertEquals(await eventsDB.count([{ kinds: [0], authors: [event0.pubkey] }]), 1); const changeEvent = { ...event0, id: '123', created_at: event0.created_at + 1 }; - await eventsDB.storeEvent(changeEvent); - assertEquals(await eventsDB.getEvents([{ kinds: [0] }]), [changeEvent]); + await eventsDB.add(changeEvent); + assertEquals(await eventsDB.filter([{ kinds: [0] }]), [changeEvent]); }); diff --git a/src/storages/events-db.ts b/src/storages/events-db.ts index 0bff2bd..f6ed16f 100644 --- a/src/storages/events-db.ts +++ b/src/storages/events-db.ts @@ -65,7 +65,7 @@ class EventsDB implements EventStore { } /** Insert an event (and its tags) into the database. */ - async storeEvent(event: Event, opts: StoreEventOpts = {}): Promise { + async add(event: Event, opts: StoreEventOpts = {}): Promise { this.#debug('EVENT', JSON.stringify(event)); if (isDittoInternalKind(event.kind) && event.pubkey !== Conf.pubkey) { @@ -264,7 +264,7 @@ class EventsDB implements EventStore { } /** Get events for filters from the database. */ - async getEvents(filters: DittoFilter[], opts: GetEventsOpts = {}): Promise[]> { + async filter(filters: DittoFilter[], opts: GetEventsOpts = {}): Promise[]> { filters = normalizeFilters(filters); // Improves performance of `{ kinds: [0], authors: ['...'] }` queries. if (opts.signal?.aborted) return Promise.resolve([]); @@ -337,7 +337,7 @@ class EventsDB implements EventStore { } /** Delete events based on filters from the database. */ - async deleteEvents(filters: DittoFilter[]): Promise { + async deleteFilters(filters: DittoFilter[]): Promise { if (!filters.length) return Promise.resolve(); this.#debug('DELETE', JSON.stringify(filters)); @@ -345,7 +345,7 @@ class EventsDB implements EventStore { } /** Get number of events that would be returned by filters. */ - async countEvents(filters: DittoFilter[]): Promise { + async count(filters: DittoFilter[]): Promise { if (!filters.length) return Promise.resolve(0); this.#debug('COUNT', JSON.stringify(filters)); const query = this.getEventsQuery(filters); diff --git a/src/storages/hydrate.ts b/src/storages/hydrate.ts index 4d8edcb..1e45f40 100644 --- a/src/storages/hydrate.ts +++ b/src/storages/hydrate.ts @@ -14,7 +14,7 @@ async function hydrateEvents(opts: HydrateEventOpts): Promi if (filters.some((filter) => filter.relations?.includes('author'))) { const pubkeys = new Set([...events].map((event) => event.pubkey)); - const authors = await storage.getEvents([{ kinds: [0], authors: [...pubkeys] }], { signal }); + const authors = await storage.filter([{ kinds: [0], authors: [...pubkeys] }], { signal }); for (const event of events) { event.author = authors.find((author) => author.pubkey === event.pubkey); diff --git a/src/storages/memorelay.test.ts b/src/storages/memorelay.test.ts index fbf3ba3..536c6eb 100644 --- a/src/storages/memorelay.test.ts +++ b/src/storages/memorelay.test.ts @@ -11,12 +11,12 @@ const memorelay = new Memorelay({ }); Deno.test('memorelay', async () => { - assertEquals(await memorelay.countEvents([{ ids: [event1.id] }]), 0); + assertEquals(await memorelay.count([{ ids: [event1.id] }]), 0); - await memorelay.storeEvent(event1); + await memorelay.add(event1); - assertEquals(await memorelay.countEvents([{ ids: [event1.id] }]), 1); + assertEquals(await memorelay.count([{ ids: [event1.id] }]), 1); - const result = await memorelay.getEvents([{ ids: [event1.id] }]); + const result = await memorelay.filter([{ ids: [event1.id] }]); assertEquals(result[0], event1); }); diff --git a/src/storages/memorelay.ts b/src/storages/memorelay.ts index bfa2a32..550faf6 100644 --- a/src/storages/memorelay.ts +++ b/src/storages/memorelay.ts @@ -26,7 +26,7 @@ class Memorelay implements EventStore { } /** Get events from memory. */ - getEvents(filters: Filter[], opts: GetEventsOpts = {}): Promise[]> { + filter(filters: Filter[], opts: GetEventsOpts = {}): Promise[]> { filters = normalizeFilters(filters); if (opts.signal?.aborted) return Promise.resolve([]); @@ -91,20 +91,20 @@ class Memorelay implements EventStore { } /** Insert an event into memory. */ - storeEvent(event: Event): Promise { + add(event: Event): Promise { this.#cache.set(event.id, event); return Promise.resolve(); } /** Count events in memory for the filters. */ - async countEvents(filters: Filter[]): Promise { - const events = await this.getEvents(filters); + async count(filters: Filter[]): Promise { + const events = await this.filter(filters); return events.length; } /** Delete events from memory. */ - async deleteEvents(filters: Filter[]): Promise { - for (const event of await this.getEvents(filters)) { + async deleteFilters(filters: Filter[]): Promise { + for (const event of await this.filter(filters)) { this.#cache.delete(event.id); } return Promise.resolve(); diff --git a/src/storages/optimizer.ts b/src/storages/optimizer.ts index e0d18fb..966dff2 100644 --- a/src/storages/optimizer.ts +++ b/src/storages/optimizer.ts @@ -25,14 +25,14 @@ class Optimizer implements EventStore { this.#client = opts.client; } - async storeEvent(event: DittoEvent, opts?: StoreEventOpts | undefined): Promise { + async add(event: DittoEvent, opts?: StoreEventOpts | undefined): Promise { await Promise.all([ - this.#db.storeEvent(event, opts), - this.#cache.storeEvent(event, opts), + this.#db.add(event, opts), + this.#cache.add(event, opts), ]); } - async getEvents( + async filter( filters: DittoFilter[], opts: GetEventsOpts | undefined = {}, ): Promise[]> { @@ -52,7 +52,7 @@ class Optimizer implements EventStore { if (filter.ids) { this.#debug(`Filter[${i}] is an IDs filter; querying cache...`); const ids = new Set(filter.ids); - for (const event of await this.#cache.getEvents([filter], opts)) { + for (const event of await this.#cache.filter([filter], opts)) { ids.delete(event.id); results.add(event); if (results.size >= limit) return getResults(); @@ -66,7 +66,7 @@ class Optimizer implements EventStore { // Query the database for events. this.#debug('Querying database...'); - for (const dbEvent of await this.#db.getEvents(filters, opts)) { + for (const dbEvent of await this.#db.filter(filters, opts)) { results.add(dbEvent); if (results.size >= limit) return getResults(); } @@ -79,14 +79,14 @@ class Optimizer implements EventStore { // Query the cache again. this.#debug('Querying cache...'); - for (const cacheEvent of await this.#cache.getEvents(filters, opts)) { + for (const cacheEvent of await this.#cache.filter(filters, opts)) { results.add(cacheEvent); if (results.size >= limit) return getResults(); } // Finally, query the client. this.#debug('Querying client...'); - for (const clientEvent of await this.#client.getEvents(filters, opts)) { + for (const clientEvent of await this.#client.filter(filters, opts)) { results.add(clientEvent); if (results.size >= limit) return getResults(); } diff --git a/src/storages/pool-store.ts b/src/storages/pool-store.ts index eddeeb8..b97fe3d 100644 --- a/src/storages/pool-store.ts +++ b/src/storages/pool-store.ts @@ -27,14 +27,14 @@ class PoolStore implements EventStore { this.#publisher = opts.publisher; } - storeEvent(event: Event, opts: StoreEventOpts = {}): Promise { + add(event: Event, opts: StoreEventOpts = {}): Promise { const { relays = this.#relays } = opts; this.#debug('EVENT', event); this.#pool.publish(event, relays); return Promise.resolve(); } - getEvents(filters: Filter[], opts: GetEventsOpts = {}): Promise[]> { + filter(filters: Filter[], opts: GetEventsOpts = {}): Promise[]> { filters = normalizeFilters(filters); if (opts.signal?.aborted) return Promise.resolve([]); @@ -80,11 +80,11 @@ class PoolStore implements EventStore { }); } - countEvents() { + count() { return Promise.reject(new Error('COUNT not implemented')); } - deleteEvents() { + deleteFilters() { return Promise.reject(new Error('Cannot delete events from relays. Create a kind 5 event instead.')); } } diff --git a/src/storages/reqmeister.ts b/src/storages/reqmeister.ts index 1fff6b6..b43ac14 100644 --- a/src/storages/reqmeister.ts +++ b/src/storages/reqmeister.ts @@ -74,10 +74,10 @@ class Reqmeister extends EventEmitter<{ [filterId: string]: (event: Event) => an if (filters.length) { this.#debug('REQ', JSON.stringify(filters)); - const events = await client.getEvents(filters, { signal: AbortSignal.timeout(timeout) }); + const events = await client.filter(filters, { signal: AbortSignal.timeout(timeout) }); for (const event of events) { - this.storeEvent(event); + this.add(event); } } @@ -119,7 +119,7 @@ class Reqmeister extends EventEmitter<{ [filterId: string]: (event: Event) => an }); } - storeEvent(event: Event): Promise { + add(event: Event): Promise { const filterId = getFilterId(eventToMicroFilter(event)); this.#queue = this.#queue.filter(([id]) => id !== filterId); this.emit(filterId, event); @@ -131,7 +131,7 @@ class Reqmeister extends EventEmitter<{ [filterId: string]: (event: Event) => an return this.#queue.some(([id]) => id === filterId); } - getEvents(filters: Filter[], opts?: GetEventsOpts | undefined): Promise[]> { + filter(filters: Filter[], opts?: GetEventsOpts | undefined): Promise[]> { if (opts?.signal?.aborted) return Promise.resolve([]); if (!filters.length) return Promise.resolve([]); @@ -145,11 +145,11 @@ class Reqmeister extends EventEmitter<{ [filterId: string]: (event: Event) => an return Promise.all(promises); } - countEvents(_filters: Filter[]): Promise { + count(_filters: Filter[]): Promise { throw new Error('COUNT not implemented.'); } - deleteEvents(_filters: Filter[]): Promise { + deleteFilters(_filters: Filter[]): Promise { throw new Error('DELETE not implemented.'); } } diff --git a/src/storages/search-store.ts b/src/storages/search-store.ts index 5afba6d..65176fb 100644 --- a/src/storages/search-store.ts +++ b/src/storages/search-store.ts @@ -30,11 +30,11 @@ class SearchStore implements EventStore { } } - storeEvent(_event: Event, _opts?: StoreEventOpts | undefined): Promise { + add(_event: Event, _opts?: StoreEventOpts | undefined): Promise { throw new Error('EVENT not implemented.'); } - async getEvents( + async filter( filters: DittoFilter[], opts?: GetEventsOpts | undefined, ): Promise[]> { @@ -69,15 +69,15 @@ class SearchStore implements EventStore { return hydrateEvents({ events: [...events], filters, storage: this.#hydrator, signal: opts?.signal }); } else { this.#debug(`Searching for "${query}" locally...`); - return this.#fallback.getEvents(filters, opts); + return this.#fallback.filter(filters, opts); } } - countEvents(_filters: Filter[]): Promise { + count(_filters: Filter[]): Promise { throw new Error('COUNT not implemented.'); } - deleteEvents(_filters: Filter[]): Promise { + deleteFilters(_filters: Filter[]): Promise { throw new Error('DELETE not implemented.'); } } diff --git a/src/storages/types.ts b/src/storages/types.ts index dda2205..65a3725 100644 --- a/src/storages/types.ts +++ b/src/storages/types.ts @@ -38,13 +38,13 @@ interface EventStore { /** Indicates NIPs supported by this data store, similar to NIP-11. For example, `50` would indicate support for `search` filters. */ supportedNips: readonly number[]; /** Add an event to the store. */ - storeEvent(event: Event, opts?: StoreEventOpts): Promise; + add(event: Event, opts?: StoreEventOpts): Promise; /** Get events from filters. */ - getEvents(filters: DittoFilter[], opts?: GetEventsOpts): Promise[]>; + filter(filters: DittoFilter[], opts?: GetEventsOpts): Promise[]>; /** Get the number of events from filters. */ - countEvents(filters: DittoFilter[]): Promise; + count?(filters: DittoFilter[]): Promise; /** Delete events from filters. */ - deleteEvents(filters: DittoFilter[]): Promise; + deleteFilters?(filters: DittoFilter[]): Promise; } export type { DittoEvent, EventStore, GetEventsOpts, StoreEventOpts }; diff --git a/src/utils/api.ts b/src/utils/api.ts index dbedc6c..af0333a 100644 --- a/src/utils/api.ts +++ b/src/utils/api.ts @@ -51,7 +51,7 @@ async function updateEvent>( fn: (prev: Event | undefined) => E, c: AppContext, ): Promise> { - const [prev] = await eventsDB.getEvents([filter], { limit: 1 }); + const [prev] = await eventsDB.filter([filter], { limit: 1 }); return createEvent(fn(prev), c); } diff --git a/src/views.ts b/src/views.ts index 9ade600..4995c82 100644 --- a/src/views.ts +++ b/src/views.ts @@ -11,14 +11,14 @@ async function renderEventAccounts(c: AppContext, filters: Filter[], signal = Ab return c.json([]); } - const events = await eventsDB.getEvents(filters, { signal }); + const events = await eventsDB.filter(filters, { signal }); const pubkeys = new Set(events.map(({ pubkey }) => pubkey)); if (!pubkeys.size) { return c.json([]); } - const authors = await eventsDB.getEvents( + const authors = await eventsDB.filter( [{ kinds: [0], authors: [...pubkeys], relations: ['author_stats'] }], { signal }, ); @@ -33,7 +33,7 @@ async function renderEventAccounts(c: AppContext, filters: Filter[], signal = Ab async function renderAccounts(c: AppContext, authors: string[], signal = AbortSignal.timeout(1000)) { const { since, until, limit } = paginationSchema.parse(c.req.query()); - const events = await eventsDB.getEvents( + const events = await eventsDB.filter( [{ kinds: [0], authors, relations: ['author_stats'], since, until, limit }], { signal }, ); @@ -53,7 +53,7 @@ async function renderStatuses(c: AppContext, ids: string[], signal = AbortSignal const { limit } = paginationSchema.parse(c.req.query()); - const events = await eventsDB.getEvents( + const events = await eventsDB.filter( [{ kinds: [1], ids, relations: ['author', 'event_stats', 'author_stats'], limit }], { signal }, ); diff --git a/src/views/mastodon/relationships.ts b/src/views/mastodon/relationships.ts index 43ed7b8..9f20024 100644 --- a/src/views/mastodon/relationships.ts +++ b/src/views/mastodon/relationships.ts @@ -2,7 +2,7 @@ import { eventsDB } from '@/storages.ts'; import { hasTag } from '@/tags.ts'; async function renderRelationship(sourcePubkey: string, targetPubkey: string) { - const events = await eventsDB.getEvents([ + const events = await eventsDB.filter([ { kinds: [3], authors: [sourcePubkey], limit: 1 }, { kinds: [3], authors: [targetPubkey], limit: 1 }, { kinds: [10000], authors: [sourcePubkey], limit: 1 }, diff --git a/src/views/mastodon/statuses.ts b/src/views/mastodon/statuses.ts index 9e45f49..bb8b95e 100644 --- a/src/views/mastodon/statuses.ts +++ b/src/views/mastodon/statuses.ts @@ -35,7 +35,7 @@ async function renderStatus(event: DittoEvent<1>, viewerPubkey?: string) { Promise.all(mentionedPubkeys.map(toMention)), firstUrl ? unfurlCardCached(firstUrl) : null, viewerPubkey - ? await eventsDB.getEvents([ + ? await eventsDB.filter([ { kinds: [6], '#e': [event.id], authors: [viewerPubkey], limit: 1 }, { kinds: [7], '#e': [event.id], authors: [viewerPubkey], limit: 1 }, { kinds: [10001], '#e': [event.id], authors: [viewerPubkey], limit: 1 },