substr/handlers/user-atom-feed.ts
2024-10-23 00:27:20 +02:00

41 lines
1.2 KiB
TypeScript

import { Context } from "@oak/oak";
import { lookupPubkeyByUsername } from "../directory.ts";
import { fetchArticlesByAuthor, fetchProfileEvent } from "../nostr.ts";
import { profileAtomFeed } from "../feeds.ts";
import Article from "../models/article.ts";
import Profile from "../models/profile.ts";
import notFoundHandler from "../handlers/not-found.ts";
const userAtomFeedHandler = async function (ctx: Context) {
const username = ctx.state.username;
const pubkey = await lookupPubkeyByUsername(ctx.state.username);
if (!pubkey) {
notFoundHandler(ctx);
return;
}
try {
const profileEvent = await fetchProfileEvent(pubkey);
if (profileEvent) {
const profile = new Profile(profileEvent, username);
if (profile.nip05) {
const articleEvents = await fetchArticlesByAuthor(pubkey);
const articles = articleEvents.map((a) => new Article(a));
const atom = profileAtomFeed(profile, articles);
ctx.response.headers.set("Content-Type", "application/atom+xml");
ctx.response.body = atom;
return;
}
}
notFoundHandler(ctx);
} catch (_e) {
notFoundHandler(ctx);
}
};
export default userAtomFeedHandler;