MVP
This commit is contained in:
41
handlers/user-atom-feed.ts
Normal file
41
handlers/user-atom-feed.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { Context } from "@oak/oak";
|
||||
import { log } from "../log.ts";
|
||||
import { lookupPubkeyByUsername } from "../ldap.ts";
|
||||
import { fetchArticlesByAuthor, fetchProfileEvent } from "../nostr.ts";
|
||||
import { profileAtomFeed } from "../feeds.ts";
|
||||
import Article from "../models/article.ts";
|
||||
import Profile from "../models/profile.ts";
|
||||
|
||||
const userAtomFeedHandler = async function (ctx: Context) {
|
||||
const username = ctx.params.user.replace(/^(@|~)/, "");
|
||||
const pubkey = await lookupPubkeyByUsername(username);
|
||||
|
||||
if (!pubkey) {
|
||||
ctx.response.status = 404;
|
||||
ctx.response.body = "Not Found";
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const profileEvent = await fetchProfileEvent(pubkey);
|
||||
const profile = new Profile(profileEvent, username);
|
||||
|
||||
if (profileEvent && 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;
|
||||
} 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 userAtomFeedHandler;
|
||||
@@ -1,10 +1,9 @@
|
||||
import { Context } from "@oak/oak";
|
||||
import { log } from "../log.ts";
|
||||
import { lookupPubkeyByUsername } from "../ldap.ts";
|
||||
import {
|
||||
fetchProfileEvent,
|
||||
fetchReplaceableEvent,
|
||||
} from "../nostr.ts";
|
||||
import { fetchProfileEvent, fetchReplaceableEvent } from "../nostr.ts";
|
||||
import Article from "../models/article.ts";
|
||||
import Profile from "../models/profile.ts";
|
||||
import { articleHtml } from "../html.ts";
|
||||
|
||||
const userEventHandler = async function (ctx: Context) {
|
||||
@@ -24,11 +23,11 @@ const userEventHandler = async function (ctx: Context) {
|
||||
identifier,
|
||||
);
|
||||
const profileEvent = await fetchProfileEvent(pubkey);
|
||||
let profile;
|
||||
|
||||
if (articleEvent && profileEvent) {
|
||||
profile = JSON.parse(profileEvent.content);
|
||||
const html = articleHtml(articleEvent, profile);
|
||||
const article = new Article(articleEvent);
|
||||
const profile = new Profile(profileEvent, username);
|
||||
const html = articleHtml(article, profile);
|
||||
|
||||
ctx.response.body = html;
|
||||
} else {
|
||||
|
||||
@@ -2,9 +2,11 @@ import { Context } from "@oak/oak";
|
||||
import { log } from "../log.ts";
|
||||
import { lookupPubkeyByUsername } from "../ldap.ts";
|
||||
import { fetchArticlesByAuthor, fetchProfileEvent } from "../nostr.ts";
|
||||
import Article from "../models/article.ts";
|
||||
import Profile from "../models/profile.ts";
|
||||
import { profilePageHtml } from "../html.ts";
|
||||
|
||||
const usernameHandler = async function (ctx: Context) {
|
||||
const userProfileHandler = async function (ctx: Context) {
|
||||
const username = ctx.params.path.replace(/^(@|~)/, "");
|
||||
const pubkey = await lookupPubkeyByUsername(username);
|
||||
|
||||
@@ -18,8 +20,10 @@ const usernameHandler = async function (ctx: Context) {
|
||||
const profileEvent = await fetchProfileEvent(pubkey);
|
||||
|
||||
if (profileEvent) {
|
||||
const profile = new Profile(profileEvent, username);
|
||||
const articleEvents = await fetchArticlesByAuthor(pubkey);
|
||||
const html = profilePageHtml(profileEvent, articleEvents);
|
||||
const articles = articleEvents.map((a) => new Article(a));
|
||||
const html = profilePageHtml(profile, articles);
|
||||
|
||||
ctx.response.body = html;
|
||||
} else {
|
||||
@@ -33,4 +37,4 @@ const usernameHandler = async function (ctx: Context) {
|
||||
}
|
||||
};
|
||||
|
||||
export default usernameHandler;
|
||||
export default userProfileHandler;
|
||||
Reference in New Issue
Block a user