substr/handlers/user-event.ts
Râu Cao f4d1ba897b
Store and serve local profile icons for user pages
Shown e.g. in browser tabs and as RSS icon
2024-12-03 18:50:10 +01:00

39 lines
1.2 KiB
TypeScript

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/errors.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;
}
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 = await articleHtml(article, profile);
await generateOgProfileImage(profile);
ctx.response.body = html;
} else {
notFoundHandler(ctx);
}
};
export default userEventHandler;