Merge remote-tracking branch 'origin/main' into router

This commit is contained in:
Alex Gleason 2025-02-21 20:51:33 -06:00
commit 403b16a67b
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
5 changed files with 25 additions and 8 deletions

View file

@ -80,7 +80,13 @@ export const nameRequestController: AppController = async (c) => {
const { conf, relay, user } = c.var; const { conf, relay, user } = c.var;
const pubkey = await user!.signer.getPublicKey(); const pubkey = await user!.signer.getPublicKey();
const { name, reason } = nameRequestSchema.parse(await c.req.json()); const result = nameRequestSchema.safeParse(await c.req.json());
if (!result.success) {
return c.json({ error: 'Invalid username', schema: result.error }, 400);
}
const { name, reason } = result.data;
const [existing] = await relay.query([{ kinds: [3036], authors: [pubkey], '#r': [name], limit: 1 }]); const [existing] = await relay.query([{ kinds: [3036], authors: [pubkey], '#r': [name], limit: 1 }]);
if (existing) { if (existing) {

View file

@ -5,6 +5,9 @@ import { logi } from '@soapbox/logi';
import { errorJson } from '@/utils/log.ts'; import { errorJson } from '@/utils/log.ts';
export const errorHandler: ErrorHandler = (err, c) => { export const errorHandler: ErrorHandler = (err, c) => {
const { method } = c.req;
const { pathname } = new URL(c.req.url);
c.header('Cache-Control', 'no-store'); c.header('Cache-Control', 'no-store');
if (err instanceof HTTPException) { if (err instanceof HTTPException) {
@ -19,7 +22,7 @@ export const errorHandler: ErrorHandler = (err, c) => {
return c.json({ error: 'The server was unable to respond in a timely manner' }, 500); return c.json({ error: 'The server was unable to respond in a timely manner' }, 500);
} }
logi({ level: 'error', ns: 'ditto.http', msg: 'Unhandled error', error: errorJson(err) }); logi({ level: 'error', ns: 'ditto.http', msg: 'Unhandled error', method, pathname, error: errorJson(err) });
return c.json({ error: 'Something went wrong' }, 500); return c.json({ error: 'Something went wrong' }, 500);
}; };

View file

@ -12,8 +12,8 @@ export const logiMiddleware: MiddlewareHandler = async (c, next) => {
await next(); await next();
const end = new Date(); const end = new Date();
const delta = (end.getTime() - start.getTime()) / 1000; const duration = (end.getTime() - start.getTime()) / 1000;
const level = c.res.status >= 500 ? 'error' : 'info'; const level = c.res.status >= 500 ? 'error' : 'info';
logi({ level, ns: 'ditto.http.response', method, pathname, status: c.res.status, delta }); logi({ level, ns: 'ditto.http.response', method, pathname, status: c.res.status, duration });
}; };

View file

@ -190,7 +190,7 @@ async function updateAuthorData(event: NostrEvent, signal: AbortSignal): Promise
if (nip05) { if (nip05) {
const tld = tldts.parse(nip05); const tld = tldts.parse(nip05);
if (tld.isIcann && !tld.isIp && !tld.isPrivate) { if (tld.isIcann && !tld.isIp && !tld.isPrivate) {
const pointer = await nip05Cache.fetch(nip05.toLowerCase(), { signal }); const pointer = await nip05Cache.fetch(nip05, { signal });
if (pointer.pubkey === event.pubkey) { if (pointer.pubkey === event.pubkey) {
updates.nip05 = nip05; updates.nip05 = nip05;
updates.nip05_domain = tld.domain; updates.nip05_domain = tld.domain;

View file

@ -11,6 +11,7 @@ import { RelayError } from '@/RelayError.ts';
import { Storages } from '@/storages.ts'; import { Storages } from '@/storages.ts';
import { nostrNow } from '@/utils.ts'; import { nostrNow } from '@/utils.ts';
import { parseFormData } from '@/utils/formdata.ts'; import { parseFormData } from '@/utils/formdata.ts';
import { errorJson } from '@/utils/log.ts';
import { purifyEvent } from '@/utils/purify.ts'; import { purifyEvent } from '@/utils/purify.ts';
/** EventTemplate with defaults. */ /** EventTemplate with defaults. */
@ -157,9 +158,16 @@ async function updateNames(k: number, d: string, n: Record<string, boolean>, c:
async function publishEvent(event: NostrEvent, c: AppContext): Promise<NostrEvent> { async function publishEvent(event: NostrEvent, c: AppContext): Promise<NostrEvent> {
logi({ level: 'info', ns: 'ditto.event', source: 'api', id: event.id, kind: event.kind }); logi({ level: 'info', ns: 'ditto.event', source: 'api', id: event.id, kind: event.kind });
try { try {
await pipeline.handleEvent(event, { source: 'api', signal: c.req.raw.signal }); const promise = pipeline.handleEvent(event, { source: 'api', signal: c.req.raw.signal });
const client = await Storages.client();
await client.event(purifyEvent(event)); promise.then(async () => {
const client = await Storages.client();
await client.event(purifyEvent(event));
}).catch((e: unknown) => {
logi({ level: 'error', ns: 'ditto.pool', id: event.id, kind: event.kind, error: errorJson(e) });
});
await promise;
} catch (e) { } catch (e) {
if (e instanceof RelayError) { if (e instanceof RelayError) {
throw new HTTPException(422, { throw new HTTPException(422, {