substr/handlers/naddr.ts

37 lines
987 B
TypeScript

import { Context } from "@oak/oak";
import { nip19 } from "@nostr/tools";
import { log } from "../log.ts";
import { articleHtml } from "../html.ts";
import { fetchProfileEvent, fetchReplaceableEvent } from "../nostr.ts";
const naddrHandler = async function (ctx: Context) {
const { request } = ctx;
const naddr = ctx.params.path;
try {
const r = nip19.decode(naddr);
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;