mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
move parseAndVerifyNip05 to utils/nip05
This commit is contained in:
parent
29d7495c39
commit
c7e5aed679
3 changed files with 59 additions and 63 deletions
|
|
@ -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 = '<!--server-generated-meta-->' as const;
|
||||
|
|
@ -22,54 +23,45 @@ const META_PLACEHOLDER = '<!--server-generated-meta-->' 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<string> =>
|
||||
html`\
|
||||
<meta content="${title}" property="og:title">
|
||||
<meta content="${type}" property="og:type">
|
||||
<meta content="${url}" property="og:url">
|
||||
<meta content="${description}" property="og:description">
|
||||
<meta content="${await getInstanceName()}" property="og:site_name">
|
||||
const tpl = ({ title, type, url, image, description, site }: OpenGraphTemplateOpts): string => {
|
||||
const res = [];
|
||||
res.push(html`\
|
||||
<meta content="${title}" property="og:title">
|
||||
<meta content="${type}" property="og:type">
|
||||
<meta content="${url}" property="og:url">
|
||||
<meta content="${description}" property="og:description">
|
||||
<meta content="${site}" property="og:site_name">
|
||||
<meta name="twitter:card" content="summary">
|
||||
<meta name="twitter:title" content="${title}">
|
||||
<meta name="twitter:description" content="${description}">
|
||||
`);
|
||||
|
||||
${
|
||||
image
|
||||
? r(html`
|
||||
<meta content="${image.url}" property="og:image">
|
||||
<meta content="${image.w}" property="og:image:width">
|
||||
<meta content="${image.h}" property="og:image:height">
|
||||
${image.alt ? r(html`<meta content="${image.alt}" property="og:image:alt">`) : ''}
|
||||
`)
|
||||
: ''
|
||||
if (image) {
|
||||
res.push(html`\
|
||||
<meta content="${image.url}" property="og:image">
|
||||
<meta content="${image.w}" property="og:image:width">
|
||||
<meta content="${image.h}" property="og:image:height">
|
||||
<meta name="twitter:image" content="${image.url}">
|
||||
`);
|
||||
if (image.alt) {
|
||||
res.push(html`<meta content="${image.alt}" property="og:image:alt">`);
|
||||
res.push(html`<meta content="${image.alt}" property="twitter:image:alt">`);
|
||||
}
|
||||
}
|
||||
|
||||
<meta name="twitter:card" content="summary">
|
||||
<meta name="twitter:title" content="${title}">
|
||||
<meta name="twitter:description" content="${description}">
|
||||
${
|
||||
image
|
||||
? r(html`
|
||||
<meta name="twitter:image" content="${image.url}">
|
||||
${image.alt ? r(html`<meta content="${image.alt}" property="twitter:image:alt">`) : ''}
|
||||
`)
|
||||
: ''
|
||||
}
|
||||
`.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<string> => {
|
||||
if (!params.acct && !params.statusId) return await BLANK_META(url);
|
||||
async function buildMetaTags(params: PathParams, url: string): Promise<string> {
|
||||
// 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<string> =
|
|||
image,
|
||||
description,
|
||||
url,
|
||||
site: meta.name,
|
||||
});
|
||||
} else if (params.acct) {
|
||||
return tpl({
|
||||
|
|
@ -85,6 +78,7 @@ const buildMetaTags = async (params: PathParams, url: string): Promise<string> =
|
|||
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<string> =
|
|||
description,
|
||||
image,
|
||||
url,
|
||||
site: meta.name,
|
||||
});
|
||||
}
|
||||
|
||||
return await BLANK_META(url);
|
||||
};
|
||||
return '';
|
||||
}
|
||||
|
||||
export const frontendController: AppMiddleware = async (c, next) => {
|
||||
try {
|
||||
|
|
|
|||
|
|
@ -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<nip19
|
|||
}
|
||||
}
|
||||
|
||||
export async function parseAndVerifyNip05(
|
||||
nip05: string | undefined,
|
||||
pubkey: string,
|
||||
signal = AbortSignal.timeout(3000),
|
||||
): Promise<Nip05 | undefined> {
|
||||
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 };
|
||||
|
|
|
|||
|
|
@ -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<Ma
|
|||
return renderAccount(event, opts);
|
||||
}
|
||||
|
||||
async function parseAndVerifyNip05(
|
||||
nip05: string | undefined,
|
||||
pubkey: string,
|
||||
signal = AbortSignal.timeout(3000),
|
||||
): Promise<Nip05 | undefined> {
|
||||
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 };
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue