39 lines
1.2 KiB
TypeScript
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;
|