Move accountLookup function into a separate module (to prevent circular dependencies)

This commit is contained in:
Alex Gleason 2024-03-20 11:55:21 -05:00
parent c8b378ad10
commit ad2261a37e
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
3 changed files with 21 additions and 17 deletions

View File

@ -1,3 +1,4 @@
import { NostrFilter } from '@soapbox/nspec';
import { type AppController } from '@/app.ts'; import { type AppController } from '@/app.ts';
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import { insertUser } from '@/db/users.ts'; import { insertUser } from '@/db/users.ts';
@ -8,13 +9,13 @@ import { jsonMetaContentSchema } from '@/schemas/nostr.ts';
import { eventsDB } from '@/storages.ts'; import { eventsDB } from '@/storages.ts';
import { addTag, deleteTag, findReplyTag, getTagSet } from '@/tags.ts'; import { addTag, deleteTag, findReplyTag, getTagSet } from '@/tags.ts';
import { uploadFile } from '@/upload.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 { createEvent, paginated, paginationSchema, parseBody, updateListEvent } from '@/utils/api.ts';
import { lookupAccount } from '@/utils/lookup.ts';
import { renderAccounts, renderEventAccounts, renderStatuses } from '@/views.ts'; import { renderAccounts, renderEventAccounts, renderStatuses } from '@/views.ts';
import { accountFromPubkey, renderAccount } from '@/views/mastodon/accounts.ts'; import { accountFromPubkey, renderAccount } from '@/views/mastodon/accounts.ts';
import { renderRelationship } from '@/views/mastodon/relationships.ts'; import { renderRelationship } from '@/views/mastodon/relationships.ts';
import { renderStatus } from '@/views/mastodon/statuses.ts'; import { renderStatus } from '@/views/mastodon/statuses.ts';
import { NostrFilter } from '@/interfaces/DittoFilter.ts';
import { hydrateEvents } from '@/storages/hydrate.ts'; import { hydrateEvents } from '@/storages/hydrate.ts';
const usernameSchema = z const usernameSchema = z

View File

@ -1,6 +1,4 @@
import { type EventTemplate, getEventHash, nip19, type NostrEvent, z } from '@/deps.ts'; 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'; import { nostrIdSchema } from '@/schemas/nostr.ts';
/** Get the current time in Nostr format. */ /** 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<NostrEvent | undefined> {
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. */ /** Return the event's age in milliseconds. */
function eventAge(event: NostrEvent): number { function eventAge(event: NostrEvent): number {
return Date.now() - nostrDate(event.created_at).getTime(); return Date.now() - nostrDate(event.created_at).getTime();
@ -153,7 +139,6 @@ export {
isNostrId, isNostrId,
isRelay, isRelay,
isURL, isURL,
lookupAccount,
type Nip05, type Nip05,
nostrDate, nostrDate,
nostrNow, nostrNow,

18
src/utils/lookup.ts Normal file
View File

@ -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<NostrEvent | undefined> {
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 };