diff --git a/src/controllers/frontend.ts b/src/controllers/frontend.ts index 30186845..773ffb7d 100644 --- a/src/controllers/frontend.ts +++ b/src/controllers/frontend.ts @@ -1,14 +1,15 @@ import { AppMiddleware } from '@/app.ts'; import { Conf } from '@/config.ts'; -import { html, r } from '@/utils/html.ts'; +import { html } from '@/utils/html.ts'; +import { Storages } from '@/storages.ts'; import { - getInstanceName, getPathParams, getProfileInfo, getStatusInfo, OpenGraphTemplateOpts, PathParams, } from '@/utils/og-metadata.ts'; +import { getInstanceMetadata } from '@/utils/instance.ts'; /** Placeholder to find & replace with metadata. */ const META_PLACEHOLDER = '' as const; @@ -22,54 +23,45 @@ const META_PLACEHOLDER = '' as const; * @param opts the metadata to use to fill the template. * @returns the built OpenGraph metadata. */ -const tpl = async ({ title, type, url, image, description }: OpenGraphTemplateOpts): Promise => - html`\ - - - - - +const tpl = ({ title, type, url, image, description, site }: OpenGraphTemplateOpts): string => { + const res = []; + res.push(html`\ + + + + + + + + + `); -${ - image - ? r(html` - - - -${image.alt ? r(html``) : ''} -`) - : '' + if (image) { + res.push(html`\ + + + + + `); + if (image.alt) { + res.push(html``); + res.push(html``); + } } - - - -${ - image - ? r(html` - -${image.alt ? r(html``) : ''} -`) - : '' - } -`.replace(/\n+/g, '\n'); + return res.join('\n').replace(/\n+/g, '\n').replace(/^[ ]+/gm, ''); +}; -const BLANK_META = (url: string) => - tpl({ - title: 'Ditto', - type: 'website', - url, - description: 'Ditto, a decentralized, self-hosted social media server', - }); +const store = await Storages.db(); -const buildMetaTags = async (params: PathParams, url: string): Promise => { - if (!params.acct && !params.statusId) return await BLANK_META(url); +async function buildMetaTags(params: PathParams, url: string): Promise { + // should never happen + if (!params.acct && !params.statusId) return ''; + const meta = await getInstanceMetadata(store); const kind0 = await getProfileInfo(params.acct); - console.log(params.acct); const { description, image } = await getStatusInfo(params.statusId || ''); const handle = kind0.nip05?.replace(/^_@/, '') || kind0.name || 'npub1xxx'; - console.log({ n: kind0.nip05, handle }); if (params.acct && params.statusId) { return tpl({ @@ -78,6 +70,7 @@ const buildMetaTags = async (params: PathParams, url: string): Promise = image, description, url, + site: meta.name, }); } else if (params.acct) { return tpl({ @@ -85,6 +78,7 @@ const buildMetaTags = async (params: PathParams, url: string): Promise = type: 'profile', description: kind0.about || '', url, + site: meta.name, image: kind0.picture ? { url: kind0.picture, @@ -101,11 +95,12 @@ const buildMetaTags = async (params: PathParams, url: string): Promise = description, image, url, + site: meta.name, }); } - return await BLANK_META(url); -}; + return ''; +} export const frontendController: AppMiddleware = async (c, next) => { try { diff --git a/src/utils/nip05.ts b/src/utils/nip05.ts index a579da6c..e9dd78cc 100644 --- a/src/utils/nip05.ts +++ b/src/utils/nip05.ts @@ -1,12 +1,13 @@ +import { nip19 } from 'nostr-tools'; import { NIP05, NStore } from '@nostrify/nostrify'; import Debug from '@soapbox/stickynotes/debug'; -import { nip19 } from 'nostr-tools'; import tldts from 'tldts'; import { Conf } from '@/config.ts'; +import { Storages } from '@/storages.ts'; import { SimpleLRU } from '@/utils/SimpleLRU.ts'; import { Time } from '@/utils/time.ts'; -import { Storages } from '@/storages.ts'; +import { Nip05, parseNip05 } from '@/utils.ts'; import { fetchWorker } from '@/workers/fetch.ts'; const debug = Debug('ditto:nip05'); @@ -60,4 +61,20 @@ async function localNip05Lookup(store: NStore, localpart: string): Promise { + if (!nip05) return; + try { + const result = await nip05Cache.fetch(nip05, { signal }); + if (result.pubkey === pubkey) { + return parseNip05(nip05); + } + } catch (_e) { + // do nothing + } +} + export { localNip05Lookup, nip05Cache }; diff --git a/src/views/mastodon/accounts.ts b/src/views/mastodon/accounts.ts index 5abb1aca..eb2d6891 100644 --- a/src/views/mastodon/accounts.ts +++ b/src/views/mastodon/accounts.ts @@ -6,9 +6,9 @@ import { Conf } from '@/config.ts'; import { MastodonAccount } from '@/entities/MastodonAccount.ts'; import { type DittoEvent } from '@/interfaces/DittoEvent.ts'; import { getLnurl } from '@/utils/lnurl.ts'; -import { nip05Cache } from '@/utils/nip05.ts'; +import { parseAndVerifyNip05 } from '@/utils/nip05.ts'; import { getTagSet } from '@/utils/tags.ts'; -import { Nip05, nostrDate, nostrNow, parseNip05 } from '@/utils.ts'; +import { nostrDate, nostrNow } from '@/utils.ts'; import { renderEmojis } from '@/views/mastodon/emojis.ts'; interface ToAccountOpts { @@ -113,20 +113,4 @@ function accountFromPubkey(pubkey: string, opts: ToAccountOpts = {}): Promise { - if (!nip05) return; - try { - const result = await nip05Cache.fetch(nip05, { signal }); - if (result.pubkey === pubkey) { - return parseNip05(nip05); - } - } catch (_e) { - // do nothing - } -} - export { accountFromPubkey, renderAccount };