even better kind 0 handling

This commit is contained in:
Siddharth Singh 2024-08-06 00:56:27 +05:30
parent 2283f64204
commit 166106b12b
No known key found for this signature in database
2 changed files with 22 additions and 11 deletions

View file

@ -37,18 +37,26 @@ async function buildTemplateOpts(params: PathParams, url: string): Promise<OpenG
if (params.acct && !params.statusId) { if (params.acct && !params.statusId) {
const key = /^[a-f0-9]{64}$/.test(params.acct) ? 'pubkey' : 'handle'; const key = /^[a-f0-9]{64}$/.test(params.acct) ? 'pubkey' : 'handle';
console.log(key); console.log(key);
const profile = await fetchProfile({ [key]: params.acct }); let handle = '';
const handle = await getHandle(params.acct, profile); try {
const profile = await fetchProfile({ [key]: params.acct });
handle = await getHandle(params.acct, profile);
res.description = profile.meta.about || `@${handle}'s Nostr profile`;
if (profile.meta.picture) {
res.image = { url: profile.meta.picture, h: 150, w: 150 };
}
} catch (e) {
console.debug(e);
// @ts-ignore we don't want getHandle trying to do a lookup here
// but we do want it to give us a nice pretty npub
handle = await getHandle(params.acct, {});
res.description = `@${handle}'s Nostr profile`;
}
Object.assign(res, { Object.assign(res, {
type: 'profile', type: 'profile',
title: `View @${handle}'s profile on Ditto`, title: `View @${handle}'s profile on Ditto`,
description: profile.meta.about || `@${handle}'s Nostr profile`,
}); });
if (profile.meta.picture) {
Object.assign(res, { image: { url: profile.meta.picture, h: 150, w: 150 } });
}
} else if (params.statusId) { } else if (params.statusId) {
const { description, image, title } = await getStatusInfo(params.statusId); const { description, image, title } = await getStatusInfo(params.statusId);
Object.assign(res, { description, title }); Object.assign(res, { description, title });
@ -63,11 +71,14 @@ async function buildTemplateOpts(params: PathParams, url: string): Promise<OpenG
return res; return res;
} }
const SHOULD_INJECT_RE = new RegExp(Conf.opengraphRouteRegex, 'i');
export const frontendController: AppMiddleware = async (c, next) => { export const frontendController: AppMiddleware = async (c, next) => {
try { try {
const content = await Deno.readTextFile(new URL('../../public/index.html', import.meta.url)); const content = await Deno.readTextFile(new URL('../../public/index.html', import.meta.url));
const shouldInject = new RegExp(Conf.opengraphRouteRegex, 'i'); const ua = c.req.header('User-Agent');
if (!shouldInject) { console.debug('got ua', ua);
if (!SHOULD_INJECT_RE.test(ua || '')) {
return c.html(content); return c.html(content);
} }
if (content.includes(META_PLACEHOLDER)) { if (content.includes(META_PLACEHOLDER)) {

View file

@ -112,8 +112,8 @@ export async function getHandle(id: string, acc?: ProfileInfo) {
const fallback = nip19.npubEncode(pubkey).slice(0, 8); const fallback = nip19.npubEncode(pubkey).slice(0, 8);
try { try {
const author = acc || await fetchProfile({ pubkey }); const author = acc || await fetchProfile({ pubkey });
if (author.meta.nip05) return parseNip05(author.meta.nip05).handle; if (author?.meta?.nip05) return parseNip05(author.meta.nip05).handle;
else if (author.meta.name) return author.meta.name; else if (author?.meta?.name) return author.meta.name;
} catch (e) { } catch (e) {
console.debug('Error in getHandle: ', e); console.debug('Error in getHandle: ', e);
} }