substr/handlers/naddr.ts
2024-10-20 21:10:58 +02:00

37 lines
960 B
TypeScript

import { ctx } from "@oak/oak";
import { nip19 } from "@nostr/tools";
import { log } from "../log.ts";
import { articleHtml } from "../html.ts"
import {
fetchReplaceableEvent,
fetchProfileEvent
} from "../nostr.ts";
const naddrHandler = async function (ctx: ctx) {
const { request } = ctx;
const { path } = ctx.params;
try {
const r = nip19.decode(path);
const articleEvent = await fetchReplaceableEvent(r.data.pubkey, r.data.identifier);
const profileEvent = await fetchProfileEvent(r.data.pubkey);
let profile;
if (articleEvent && profileEvent) {
const profile = JSON.parse(profileEvent.content);
const html = articleHtml(articleEvent, profile);
ctx.response.body = html;
} else {
ctx.response.status = 404;
ctx.response.body = "Not Found";
}
} catch (e) {
log(e, "yellow");
ctx.response.status = 404;
ctx.response.body = "Not Found";
}
};
export default naddrHandler;