substr/handlers/user-profile.ts
2024-10-24 14:28:49 +02:00

39 lines
1.2 KiB
TypeScript

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