diff --git a/src/controllers/api/accounts.ts b/src/controllers/api/accounts.ts index d1bc244..211eb77 100644 --- a/src/controllers/api/accounts.ts +++ b/src/controllers/api/accounts.ts @@ -5,7 +5,7 @@ import * as mixer from '@/mixer.ts'; import { getAuthor, getFollowedPubkeys, getFollows, syncUser } from '@/queries.ts'; import { booleanParamSchema, fileSchema } from '@/schema.ts'; import { jsonMetaContentSchema } from '@/schemas/nostr.ts'; -import { toAccount, toRelationship, toStatus } from '@/transformers/nostr-to-mastoapi.ts'; +import { accountFromPubkey, toAccount, toRelationship, toStatus } from '@/transformers/nostr-to-mastoapi.ts'; import { isFollowing, lookupAccount, nostrNow, Time } from '@/utils.ts'; import { paginated, paginationSchema, parseBody } from '@/utils/web.ts'; import { createEvent } from '@/utils/web.ts'; @@ -60,9 +60,9 @@ const verifyCredentialsController: AppController = async (c) => { const event = await getAuthor(pubkey); if (event) { return c.json(await toAccount(event, { withSource: true })); + } else { + return c.json(await accountFromPubkey(pubkey, { withSource: true })); } - - return c.json({ error: 'Could not find user.' }, 404); }; const accountController: AppController = async (c) => { diff --git a/src/deps.ts b/src/deps.ts index a31d988..35b4763 100644 --- a/src/deps.ts +++ b/src/deps.ts @@ -24,6 +24,7 @@ export { nip13, nip19, nip21, + type UnsignedEvent, verifySignature, } from 'npm:nostr-tools@^1.14.0'; export { findReplyTag } from 'https://gitlab.com/soapbox-pub/mostr/-/raw/c67064aee5ade5e01597c6d23e22e53c628ef0e2/src/nostr/tags.ts'; diff --git a/src/transformers/nostr-to-mastoapi.ts b/src/transformers/nostr-to-mastoapi.ts index 5fc02a8..0bf09f3 100644 --- a/src/transformers/nostr-to-mastoapi.ts +++ b/src/transformers/nostr-to-mastoapi.ts @@ -2,12 +2,12 @@ import { isCWTag } from 'https://gitlab.com/soapbox-pub/mostr/-/raw/c67064aee5ad import { Conf } from '@/config.ts'; import * as eventsDB from '@/db/events.ts'; -import { type Event, findReplyTag, lodash, nip19, sanitizeHtml, TTLCache, unfurl } from '@/deps.ts'; +import { type Event, findReplyTag, lodash, nip19, sanitizeHtml, TTLCache, unfurl, type UnsignedEvent } from '@/deps.ts'; import { getMediaLinks, parseNoteContent } from '@/note.ts'; import { getAuthor, getFollowedPubkeys, getFollows } from '@/queries.ts'; import { emojiTagSchema, filteredArray } from '@/schema.ts'; import { jsonMediaDataSchema, jsonMetaContentSchema } from '@/schemas/nostr.ts'; -import { isFollowing, type Nip05, nostrDate, parseNip05, Time } from '@/utils.ts'; +import { isFollowing, type Nip05, nostrDate, nostrNow, parseNip05, Time } from '@/utils.ts'; import { verifyNip05Cached } from '@/utils/nip05.ts'; import { findUser } from '@/db/users.ts'; import { DittoAttachment, renderAttachment } from '@/views/attachment.ts'; @@ -19,7 +19,7 @@ interface ToAccountOpts { withSource?: boolean; } -async function toAccount(event: Event<0>, opts: ToAccountOpts = {}) { +async function toAccount(event: UnsignedEvent<0>, opts: ToAccountOpts = {}) { const { withSource = false } = opts; const { pubkey } = event; @@ -75,6 +75,18 @@ async function toAccount(event: Event<0>, opts: ToAccountOpts = {}) { }; } +function accountFromPubkey(pubkey: string, opts: ToAccountOpts = {}) { + const event: UnsignedEvent<0> = { + kind: 0, + pubkey, + content: '', + tags: [], + created_at: nostrNow(), + }; + + return toAccount(event, opts); +} + async function parseAndVerifyNip05(nip05: string | undefined, pubkey: string): Promise { if (nip05 && await verifyNip05Cached(nip05, pubkey)) { return parseNip05(nip05); @@ -257,7 +269,7 @@ function unfurlCardCached(url: string): Promise { return card; } -function toEmojis(event: Event) { +function toEmojis(event: UnsignedEvent) { const emojiTags = event.tags.filter((tag) => tag[0] === 'emoji'); return filteredArray(emojiTagSchema).parse(emojiTags) @@ -310,4 +322,4 @@ async function toNotificationMention(event: Event<1>, viewerPubkey?: string) { }; } -export { toAccount, toNotification, toRelationship, toStatus }; +export { accountFromPubkey, toAccount, toNotification, toRelationship, toStatus };