Catch metadata when parsing

This commit is contained in:
Alex Gleason 2024-05-01 19:55:58 -05:00
parent fc7ed8bf24
commit 4045a6bdfc
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
8 changed files with 8 additions and 8 deletions

View File

@ -197,7 +197,7 @@ const updateCredentialsController: AppController = async (c) => {
} }
const author = await getAuthor(pubkey); const author = await getAuthor(pubkey);
const meta = author ? n.json().pipe(n.metadata()).parse(author.content) : {}; const meta = author ? n.json().pipe(n.metadata()).catch({}).parse(author.content) : {};
const { const {
avatar: avatarFile, avatar: avatarFile,

View File

@ -10,7 +10,7 @@ const instanceController: AppController = async (c) => {
const { signal } = c.req.raw; const { signal } = c.req.raw;
const [event] = await Storages.db.query([{ kinds: [0], authors: [Conf.pubkey], limit: 1 }], { signal }); const [event] = await Storages.db.query([{ kinds: [0], authors: [Conf.pubkey], limit: 1 }], { signal });
const meta = n.json().pipe(serverMetaSchema).parse(event?.content); const meta = n.json().pipe(serverMetaSchema).catch({}).parse(event?.content);
/** Protocol to use for WebSocket URLs, depending on the protocol of the `LOCAL_DOMAIN`. */ /** Protocol to use for WebSocket URLs, depending on the protocol of the `LOCAL_DOMAIN`. */
const wsProtocol = protocol === 'http:' ? 'ws:' : 'wss:'; const wsProtocol = protocol === 'http:' ? 'ws:' : 'wss:';

View File

@ -405,7 +405,7 @@ const zapController: AppController = async (c) => {
const target = await getEvent(id, { kind: 1, relations: ['author', 'event_stats', 'author_stats'], signal }); const target = await getEvent(id, { kind: 1, relations: ['author', 'event_stats', 'author_stats'], signal });
const author = target?.author; const author = target?.author;
const meta = n.json().pipe(n.metadata()).parse(author?.content); const meta = n.json().pipe(n.metadata()).catch({}).parse(author?.content);
const lnurl = getLnurl(meta); const lnurl = getLnurl(meta);
if (target && lnurl) { if (target && lnurl) {

View File

@ -8,7 +8,7 @@ import { Storages } from '@/storages.ts';
const relayInfoController: AppController = async (c) => { const relayInfoController: AppController = async (c) => {
const { signal } = c.req.raw; const { signal } = c.req.raw;
const [event] = await Storages.db.query([{ kinds: [0], authors: [Conf.pubkey], limit: 1 }], { signal }); const [event] = await Storages.db.query([{ kinds: [0], authors: [Conf.pubkey], limit: 1 }], { signal });
const meta = n.json().pipe(serverMetaSchema).parse(event?.content); const meta = n.json().pipe(serverMetaSchema).catch({}).parse(event?.content);
return c.json({ return c.json({
name: meta.name ?? 'Ditto', name: meta.name ?? 'Ditto',

View File

@ -111,7 +111,7 @@ async function parseMetadata(event: NostrEvent, signal: AbortSignal): Promise<vo
if (event.kind !== 0) return; if (event.kind !== 0) return;
// Parse metadata. // Parse metadata.
const metadata = n.json().pipe(n.metadata()).safeParse(event.content); const metadata = n.json().pipe(n.metadata()).catch({}).safeParse(event.content);
if (!metadata.success) return; if (!metadata.success) return;
// Get nip05. // Get nip05.

View File

@ -411,7 +411,7 @@ function buildSearchContent(event: NostrEvent): string {
/** Build search content for a user. */ /** Build search content for a user. */
function buildUserSearchContent(event: NostrEvent): string { function buildUserSearchContent(event: NostrEvent): string {
const { name, nip05, about } = n.json().pipe(n.metadata()).parse(event.content); const { name, nip05, about } = n.json().pipe(n.metadata()).catch({}).parse(event.content);
return [name, nip05, about].filter(Boolean).join('\n'); return [name, nip05, about].filter(Boolean).join('\n');
} }

View File

@ -8,7 +8,7 @@ import type { Actor } from '@/schemas/activitypub.ts';
/** Nostr metadata event to ActivityPub actor. */ /** Nostr metadata event to ActivityPub actor. */
async function renderActor(event: NostrEvent, username: string): Promise<Actor | undefined> { async function renderActor(event: NostrEvent, username: string): Promise<Actor | undefined> {
const content = n.json().pipe(n.metadata()).parse(event.content); const content = n.json().pipe(n.metadata()).catch({}).parse(event.content);
return { return {
type: 'Person', type: 'Person',

View File

@ -28,7 +28,7 @@ async function renderAccount(
about, about,
lud06, lud06,
lud16, lud16,
} = n.json().pipe(n.metadata()).parse(event.content); } = n.json().pipe(n.metadata()).catch({}).parse(event.content);
const npub = nip19.npubEncode(pubkey); const npub = nip19.npubEncode(pubkey);
const parsed05 = await parseAndVerifyNip05(nip05, pubkey); const parsed05 = await parseAndVerifyNip05(nip05, pubkey);