substr/handlers/user-event.ts

46 lines
1.1 KiB
TypeScript

import { Context } from "@oak/oak";
import { log } from "../log.ts";
import { lookupPubkeyByUsername } from "../ldap.ts";
import {
fetchProfileEvent,
fetchReplaceableEvent,
} from "../nostr.ts";
import { articleHtml } from "../html.ts";
const userEventHandler = async function (ctx: Context) {
const username = ctx.params.user.replace(/^(@|~)/, "");
const identifier = ctx.params.identifier;
const pubkey = await lookupPubkeyByUsername(username);
if (!pubkey) {
ctx.response.status = 404;
ctx.response.body = "Not Found";
return;
}
try {
const articleEvent = await fetchReplaceableEvent(
pubkey,
identifier,
);
const profileEvent = await fetchProfileEvent(pubkey);
let profile;
if (articleEvent && profileEvent) {
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 userEventHandler;