diff --git a/src/client.ts b/src/client.ts index 38a6b22..abb5fdc 100644 --- a/src/client.ts +++ b/src/client.ts @@ -1,11 +1,9 @@ import { Conf } from '@/config.ts'; -import { Author, type Event, type Filter, findReplyTag, matchFilters, RelayPool, TTLCache } from '@/deps.ts'; -import { eventDateComparator, type PaginationParams, Time } from '@/utils.ts'; +import { type Event, type Filter, matchFilters, RelayPool, TTLCache } from '@/deps.ts'; +import { Time } from '@/utils.ts'; import type { GetFiltersOpts } from '@/types.ts'; -const db = await Deno.openKv(); - type Pool = InstanceType; /** HACK: Websockets in Deno are finnicky... get a new pool every 30 minutes. */ @@ -72,88 +70,6 @@ function getFilters(filters: Filter[], opts: GetFiltersOpts }); } -/** Get a Nostr event by its ID. */ -const getEvent = async (id: string, kind?: K): Promise | undefined> => { - const event = await (getPool().getEventById(id, Conf.poolRelays, 0) as Promise); - if (event) { - if (event.id !== id) return undefined; - if (kind && event.kind !== kind) return undefined; - return event as Event; - } -}; - -/** Get a Nostr `set_medatadata` event for a user's pubkey. */ -const getAuthor = async (pubkey: string, timeout = 1000): Promise | undefined> => { - const author = new Author(getPool(), Conf.poolRelays, pubkey); - - const event: Event<0> | null = await new Promise((resolve) => { - setTimeout(resolve, timeout, null); - return author.metaData(resolve, 0); - }); - - return event?.pubkey === pubkey ? event : undefined; -}; - -/** Get users the given pubkey follows. */ -const getFollows = async (pubkey: string): Promise | undefined> => { - const [event] = await getFilters([{ authors: [pubkey], kinds: [3] }], { timeout: 5000 }); - - // TODO: figure out a better, more generic & flexible way to handle event cache (and timeouts?) - // Prewarm cache in GET `/api/v1/accounts/verify_credentials` - if (event) { - await db.set(['event3', pubkey], event); - return event; - } else { - return (await db.get>(['event3', pubkey])).value || undefined; - } -}; - -/** Get events from people the user follows. */ -async function getFeed(event3: Event<3>, params: PaginationParams): Promise[]> { - const authors = event3.tags - .filter((tag) => tag[0] === 'p') - .map((tag) => tag[1]); - - authors.push(event3.pubkey); // see own events in feed - - const filter: Filter = { - authors, - kinds: [1], - ...params, - }; - - const results = await getFilters([filter], { timeout: 5000 }) as Event<1>[]; - return results.sort(eventDateComparator); -} - -/** Get a feed of all known text notes. */ -async function getPublicFeed(params: PaginationParams): Promise[]> { - const results = await getFilters([{ kinds: [1], ...params }], { timeout: 5000 }); - return results.sort(eventDateComparator); -} - -async function getAncestors(event: Event<1>, result = [] as Event<1>[]): Promise[]> { - if (result.length < 100) { - const replyTag = findReplyTag(event); - const inReplyTo = replyTag ? replyTag[1] : undefined; - - if (inReplyTo) { - const parentEvent = await getEvent(inReplyTo, 1); - - if (parentEvent) { - result.push(parentEvent); - return getAncestors(parentEvent, result); - } - } - } - - return result.reverse(); -} - -function getDescendants(eventId: string): Promise[]> { - return getFilters([{ kinds: [1], '#e': [eventId] }], { limit: 200, timeout: 2000 }) as Promise[]>; -} - /** Publish an event to the Nostr relay. */ function publish(event: Event, relays = Conf.publishRelays): void { console.log('Publishing event', event, relays); @@ -164,4 +80,4 @@ function publish(event: Event, relays = Conf.publishRelays): void { } } -export { getAncestors, getAuthor, getDescendants, getEvent, getFeed, getFilters, getFollows, getPublicFeed, publish }; +export { getFilters, publish }; diff --git a/src/controllers/activitypub/actor.ts b/src/controllers/activitypub/actor.ts index d3547ac..549f08d 100644 --- a/src/controllers/activitypub/actor.ts +++ b/src/controllers/activitypub/actor.ts @@ -1,5 +1,5 @@ -import { getAuthor } from '@/client.ts'; import { findUser } from '@/db/users.ts'; +import { getAuthor } from '@/queries.ts'; import { toActor } from '@/transformers/nostr-to-activitypub.ts'; import { activityJson } from '@/utils.ts'; diff --git a/src/controllers/api/accounts.ts b/src/controllers/api/accounts.ts index d65c9e1..0952141 100644 --- a/src/controllers/api/accounts.ts +++ b/src/controllers/api/accounts.ts @@ -1,7 +1,8 @@ import { type AppController } from '@/app.ts'; import { type Filter, findReplyTag, z } from '@/deps.ts'; -import { getAuthor, getFollows, publish } from '@/client.ts'; +import { publish } from '@/client.ts'; import { getFilters } from '@/mixer.ts'; +import { getAuthor, getFollows } from '@/queries.ts'; import { jsonMetaContentSchema } from '@/schemas/nostr.ts'; import { signEvent } from '@/sign.ts'; import { toAccount, toStatus } from '@/transformers/nostr-to-mastoapi.ts'; diff --git a/src/controllers/api/statuses.ts b/src/controllers/api/statuses.ts index 90d3be2..03e2235 100644 --- a/src/controllers/api/statuses.ts +++ b/src/controllers/api/statuses.ts @@ -1,6 +1,7 @@ import { type AppController } from '@/app.ts'; -import { getAncestors, getDescendants, getEvent, publish } from '@/client.ts'; +import { publish } from '@/client.ts'; import { type Event, ISO6391, Kind, z } from '@/deps.ts'; +import { getAncestors, getDescendants, getEvent } from '@/queries.ts'; import { signEvent } from '@/sign.ts'; import { toStatus } from '@/transformers/nostr-to-mastoapi.ts'; import { nostrNow, parseBody } from '@/utils.ts'; diff --git a/src/controllers/api/timelines.ts b/src/controllers/api/timelines.ts index 260ee59..5b5fdcd 100644 --- a/src/controllers/api/timelines.ts +++ b/src/controllers/api/timelines.ts @@ -1,4 +1,4 @@ -import { getFeed, getFollows, getPublicFeed } from '@/client.ts'; +import { getFeed, getFollows, getPublicFeed } from '@/queries.ts'; import { toStatus } from '@/transformers/nostr-to-mastoapi.ts'; import { buildLinkHeader, paginationSchema } from '@/utils.ts'; diff --git a/src/queries.ts b/src/queries.ts new file mode 100644 index 0000000..0c8cb86 --- /dev/null +++ b/src/queries.ts @@ -0,0 +1,72 @@ +import { type Event, type Filter, findReplyTag } from '@/deps.ts'; +import { eventDateComparator, type PaginationParams } from '@/utils.ts'; + +import { getFilters as getFiltersMixer } from './mixer.ts'; + +/** Get a Nostr event by its ID. */ +const getEvent = async (id: string, kind?: K): Promise | undefined> => { + const filter: Filter = { ids: [id], limit: 1 }; + if (kind) filter.kinds = [kind]; + const [event] = await getFiltersMixer([filter], { limit: 1, timeout: 1000 }); + return event; +}; + +/** Get a Nostr `set_medatadata` event for a user's pubkey. */ +const getAuthor = async (pubkey: string, timeout = 1000): Promise | undefined> => { + const [event] = await getFiltersMixer([{ authors: [pubkey], kinds: [0] }], { timeout }); + return event; +}; + +/** Get users the given pubkey follows. */ +const getFollows = async (pubkey: string): Promise | undefined> => { + const [event] = await getFiltersMixer([{ authors: [pubkey], kinds: [3] }], { timeout: 5000 }); + return event; +}; + +/** Get events from people the user follows. */ +async function getFeed(event3: Event<3>, params: PaginationParams): Promise[]> { + const authors = event3.tags + .filter((tag) => tag[0] === 'p') + .map((tag) => tag[1]); + + authors.push(event3.pubkey); // see own events in feed + + const filter: Filter = { + authors, + kinds: [1], + ...params, + }; + + const results = await getFiltersMixer([filter], { timeout: 5000 }) as Event<1>[]; + return results.sort(eventDateComparator); +} + +/** Get a feed of all known text notes. */ +async function getPublicFeed(params: PaginationParams): Promise[]> { + const results = await getFiltersMixer([{ kinds: [1], ...params }], { timeout: 5000 }); + return results.sort(eventDateComparator); +} + +async function getAncestors(event: Event<1>, result = [] as Event<1>[]): Promise[]> { + if (result.length < 100) { + const replyTag = findReplyTag(event); + const inReplyTo = replyTag ? replyTag[1] : undefined; + + if (inReplyTo) { + const parentEvent = await getEvent(inReplyTo, 1); + + if (parentEvent) { + result.push(parentEvent); + return getAncestors(parentEvent, result); + } + } + } + + return result.reverse(); +} + +function getDescendants(eventId: string): Promise[]> { + return getFiltersMixer([{ kinds: [1], '#e': [eventId] }], { limit: 200, timeout: 2000 }) as Promise[]>; +} + +export { getAncestors, getAuthor, getDescendants, getEvent, getFeed, getFollows, getPublicFeed }; diff --git a/src/transformers/nostr-to-mastoapi.ts b/src/transformers/nostr-to-mastoapi.ts index 54f1b2e..520246f 100644 --- a/src/transformers/nostr-to-mastoapi.ts +++ b/src/transformers/nostr-to-mastoapi.ts @@ -1,10 +1,10 @@ import { isCWTag } from 'https://gitlab.com/soapbox-pub/mostr/-/raw/c67064aee5ade5e01597c6d23e22e53c628ef0e2/src/nostr/tags.ts'; -import { getAuthor } from '@/client.ts'; import { Conf } from '@/config.ts'; import { type Event, findReplyTag, lodash, nip19, sanitizeHtml, TTLCache, unfurl, z } from '@/deps.ts'; import { verifyNip05Cached } from '@/nip05.ts'; import { getMediaLinks, type MediaLink, parseNoteContent } from '@/note.ts'; +import { getAuthor } from '@/queries.ts'; import { emojiTagSchema, filteredArray } from '@/schema.ts'; import { jsonMetaContentSchema } from '@/schemas/nostr.ts'; import { type Nip05, nostrDate, parseNip05, Time } from '@/utils.ts'; diff --git a/src/utils.ts b/src/utils.ts index face71d..3075914 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,7 +1,7 @@ -import { getAuthor } from '@/client.ts'; import { Conf } from '@/config.ts'; import { type Context, type Event, nip19, parseFormData, z } from '@/deps.ts'; import { lookupNip05Cached } from '@/nip05.ts'; +import { getAuthor } from '@/queries.ts'; /** Get the current time in Nostr format. */ const nostrNow = () => Math.floor(new Date().getTime() / 1000);