substr/handlers/username.ts
2024-10-21 00:30:34 +02:00

37 lines
1006 B
TypeScript

import { Context } from "@oak/oak";
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 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;