import { Context } from "@oak/oak"; import { nip19 } from "@nostr/tools"; import { log } from "../log.ts"; import { lookupPubkeyByUsername } from "../ldap.ts"; import { fetchArticlesByAuthor, fetchProfileEvent } from "../nostr.ts"; import { profilePageHtml } from "../html.ts"; const usernameHandler = async function (ctx: Context) { const { request } = ctx; 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 articleEvents = await fetchArticlesByAuthor(pubkey); const html = profilePageHtml(profileEvent, articleEvents); 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 usernameHandler;