substr/handlers/user-event.ts
2024-10-21 15:43:58 +02:00

45 lines
1.2 KiB
TypeScript

import { Context } from "@oak/oak";
import { log } from "../log.ts";
import { lookupPubkeyByUsername } from "../ldap.ts";
import { fetchProfileEvent, fetchReplaceableEvent } from "../nostr.ts";
import Article from "../models/article.ts";
import Profile from "../models/profile.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);
if (articleEvent && profileEvent) {
const article = new Article(articleEvent);
const profile = new Profile(profileEvent, username);
const html = articleHtml(article, 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;