import { Context } from "@oak/oak"; import { log } from "../log.ts"; import { lookupPubkeyByUsername } from "../ldap.ts"; import { fetchArticlesByAuthor, fetchProfileEvent } from "../nostr.ts"; import Article from "../models/article.ts"; import Profile from "../models/profile.ts"; import { profilePageHtml } from "../html.ts"; const userProfileHandler = async function (ctx: Context) { const username = ctx.params.path.replace(/^(@|~)/, ""); const pubkey = await lookupPubkeyByUsername(username); if (!pubkey) { ctx.response.status = 404; ctx.response.body = "Not Found"; return; } try { const profileEvent = await fetchProfileEvent(pubkey); if (profileEvent) { const profile = new Profile(profileEvent, username); const articleEvents = await fetchArticlesByAuthor(pubkey); const articles = articleEvents.map((a) => new Article(a)); const html = profilePageHtml(profile, articles); 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 userProfileHandler;