import { Context } from "@oak/oak"; import { lookupPubkeyByUsername } from "../directory.ts"; import { fetchProfileEvent, fetchReplaceableEvent } from "../nostr.ts"; import Article from "../models/article.ts"; import Profile from "../models/profile.ts"; import { articleHtml } from "../html.ts"; import notFoundHandler from "../handlers/not-found.ts"; import { generateOgProfileImage } from "../magick.ts"; const userEventHandler = async function (ctx: Context) { const username = ctx.state.username.replace(/^(@|~)/, ""); const identifier = ctx.state.identifier; const pubkey = await lookupPubkeyByUsername(username); if (!pubkey) { notFoundHandler(ctx); 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); generateOgProfileImage(profile); ctx.response.body = html; } else { notFoundHandler(ctx); } } catch (_e) { notFoundHandler(ctx); } }; export default userEventHandler;