From ad2261a37eed3e92073c46db4def04b88785eb46 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 20 Mar 2024 11:55:21 -0500 Subject: [PATCH] Move accountLookup function into a separate module (to prevent circular dependencies) --- src/controllers/api/accounts.ts | 5 +++-- src/utils.ts | 15 --------------- src/utils/lookup.ts | 18 ++++++++++++++++++ 3 files changed, 21 insertions(+), 17 deletions(-) create mode 100644 src/utils/lookup.ts diff --git a/src/controllers/api/accounts.ts b/src/controllers/api/accounts.ts index 3e6902a..d8aadb1 100644 --- a/src/controllers/api/accounts.ts +++ b/src/controllers/api/accounts.ts @@ -1,3 +1,4 @@ +import { NostrFilter } from '@soapbox/nspec'; import { type AppController } from '@/app.ts'; import { Conf } from '@/config.ts'; import { insertUser } from '@/db/users.ts'; @@ -8,13 +9,13 @@ import { jsonMetaContentSchema } from '@/schemas/nostr.ts'; import { eventsDB } from '@/storages.ts'; import { addTag, deleteTag, findReplyTag, getTagSet } from '@/tags.ts'; import { uploadFile } from '@/upload.ts'; -import { lookupAccount, nostrNow } from '@/utils.ts'; +import { nostrNow } from '@/utils.ts'; import { createEvent, paginated, paginationSchema, parseBody, updateListEvent } from '@/utils/api.ts'; +import { lookupAccount } from '@/utils/lookup.ts'; import { renderAccounts, renderEventAccounts, renderStatuses } from '@/views.ts'; import { accountFromPubkey, renderAccount } from '@/views/mastodon/accounts.ts'; import { renderRelationship } from '@/views/mastodon/relationships.ts'; import { renderStatus } from '@/views/mastodon/statuses.ts'; -import { NostrFilter } from '@/interfaces/DittoFilter.ts'; import { hydrateEvents } from '@/storages/hydrate.ts'; const usernameSchema = z diff --git a/src/utils.ts b/src/utils.ts index 0fcd920..d714ce7 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,6 +1,4 @@ import { type EventTemplate, getEventHash, nip19, type NostrEvent, z } from '@/deps.ts'; -import { getAuthor } from '@/queries.ts'; -import { nip05Cache } from '@/utils/nip05.ts'; import { nostrIdSchema } from '@/schemas/nostr.ts'; /** Get the current time in Nostr format. */ @@ -55,18 +53,6 @@ function parseNip05(value: string): Nip05 { }; } -/** Resolve a bech32 or NIP-05 identifier to an account. */ -async function lookupAccount(value: string, signal = AbortSignal.timeout(3000)): Promise { - console.log(`Looking up ${value}`); - - const pubkey = bech32ToPubkey(value) || - await nip05Cache.fetch(value, { signal }).then(({ pubkey }) => pubkey).catch(() => undefined); - - if (pubkey) { - return getAuthor(pubkey); - } -} - /** Return the event's age in milliseconds. */ function eventAge(event: NostrEvent): number { return Date.now() - nostrDate(event.created_at).getTime(); @@ -153,7 +139,6 @@ export { isNostrId, isRelay, isURL, - lookupAccount, type Nip05, nostrDate, nostrNow, diff --git a/src/utils/lookup.ts b/src/utils/lookup.ts new file mode 100644 index 0000000..c0f52ec --- /dev/null +++ b/src/utils/lookup.ts @@ -0,0 +1,18 @@ +import { type NostrEvent } from '@/deps.ts'; +import { getAuthor } from '@/queries.ts'; +import { bech32ToPubkey } from '@/utils.ts'; +import { nip05Cache } from '@/utils/nip05.ts'; + +/** Resolve a bech32 or NIP-05 identifier to an account. */ +async function lookupAccount(value: string, signal = AbortSignal.timeout(3000)): Promise { + console.log(`Looking up ${value}`); + + const pubkey = bech32ToPubkey(value) || + await nip05Cache.fetch(value, { signal }).then(({ pubkey }) => pubkey).catch(() => undefined); + + if (pubkey) { + return getAuthor(pubkey); + } +} + +export { lookupAccount };