Compare commits

..

No commits in common. "ba7336b4eee047f349cd3647e20362e7c3c70de6" and "3914ca25a1124bba8f2bc4d326039685fda99db9" have entirely different histories.

9 changed files with 40 additions and 58 deletions

View File

@ -2,7 +2,6 @@ import { Context } from "@oak/oak";
import { nip19 } from "@nostr/tools";
import { log } from "../log.ts";
import { lookupUsernameByPubkey } from "../ldap.ts";
import notFoundHandler from "../handlers/not-found.ts";
const naddrHandler = async function (ctx: Context) {
const naddr = ctx.params.path;
@ -14,11 +13,13 @@ const naddrHandler = async function (ctx: Context) {
if (username && r.data.identifier) {
ctx.response.redirect(`/@${username}/${r.data.identifier}`);
} else {
notFoundHandler(ctx);
ctx.response.status = 404;
ctx.response.body = "Not Found";
}
} catch (e) {
log(e, "yellow");
notFoundHandler(ctx);
ctx.response.status = 404;
ctx.response.body = "Not Found";
}
};

View File

@ -1,10 +0,0 @@
import { Context } from "@oak/oak";
import { errorPageHtml } from "../html.ts";
const notFoundHandler = function (ctx: Context) {
const html = errorPageHtml(404, "Not found");
ctx.response.body = html;
ctx.response.status = 404;
};
export default notFoundHandler;

View File

@ -2,7 +2,6 @@ import { Context } from "@oak/oak";
import { nip19 } from "@nostr/tools";
import { log } from "../log.ts";
import { lookupUsernameByPubkey } from "../ldap.ts";
import notFoundHandler from "../handlers/not-found.ts";
const nprofileHandler = async function (ctx: Context) {
const nprofile = ctx.params.path;
@ -14,11 +13,13 @@ const nprofileHandler = async function (ctx: Context) {
if (username) {
ctx.response.redirect(`/@${username}`);
} else {
notFoundHandler(ctx);
ctx.response.status = 404;
ctx.response.body = "Not Found";
}
} catch (e) {
log(e, "yellow");
notFoundHandler(ctx);
ctx.response.status = 404;
ctx.response.body = "Not Found";
}
};

View File

@ -2,7 +2,6 @@ import { Context } from "@oak/oak";
import { nip19 } from "@nostr/tools";
import { log } from "../log.ts";
import { lookupUsernameByPubkey } from "../ldap.ts";
import notFoundHandler from "../handlers/not-found.ts";
const npubHandler = async function (ctx: Context) {
const npub = ctx.params.path;
@ -14,11 +13,13 @@ const npubHandler = async function (ctx: Context) {
if (username) {
ctx.response.redirect(`/@${username}`);
} else {
notFoundHandler(ctx);
ctx.response.status = 404;
ctx.response.body = "Not Found";
}
} catch (e) {
log(e, "yellow");
notFoundHandler(ctx);
ctx.response.status = 404;
ctx.response.body = "Not Found";
}
};

View File

@ -5,14 +5,14 @@ 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.params.user.replace(/^(@|~)/, "");
const pubkey = await lookupPubkeyByUsername(username);
if (!pubkey) {
notFoundHandler(ctx);
ctx.response.status = 404;
ctx.response.body = "Not Found";
return;
}
@ -33,7 +33,8 @@ const userAtomFeedHandler = async function (ctx: Context) {
}
} catch (e) {
log(e, "yellow");
notFoundHandler(ctx);
ctx.response.status = 404;
ctx.response.body = "Not Found";
}
};

View File

@ -5,7 +5,6 @@ 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/not-found.ts";
const userEventHandler = async function (ctx: Context) {
const username = ctx.params.user.replace(/^(@|~)/, "");
@ -13,7 +12,8 @@ const userEventHandler = async function (ctx: Context) {
const pubkey = await lookupPubkeyByUsername(username);
if (!pubkey) {
notFoundHandler(ctx);
ctx.response.status = 404;
ctx.response.body = "Not Found";
return;
}
@ -31,11 +31,13 @@ const userEventHandler = async function (ctx: Context) {
ctx.response.body = html;
} else {
notFoundHandler(ctx);
ctx.response.status = 404;
ctx.response.body = "Not Found";
}
} catch (e) {
log(e, "yellow");
notFoundHandler(ctx);
ctx.response.status = 404;
ctx.response.body = "Not Found";
}
};

View File

@ -5,14 +5,14 @@ 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";
const userProfileHandler = async function (ctx: Context) {
const username = ctx.params.path.replace(/^(@|~)/, "");
const pubkey = await lookupPubkeyByUsername(username);
if (!pubkey) {
notFoundHandler(ctx);
ctx.response.status = 404;
ctx.response.body = "Not Found";
return;
}
@ -27,11 +27,13 @@ const userProfileHandler = async function (ctx: Context) {
ctx.response.body = html;
} else {
notFoundHandler(ctx);
ctx.response.status = 404;
ctx.response.body = "Not Found";
}
} catch (e) {
log(e, "yellow");
notFoundHandler(ctx);
ctx.response.status = 404;
ctx.response.body = "Not Found";
}
};

20
html.ts
View File

@ -2,13 +2,7 @@ import { localizeDate } from "./dates.ts";
import Article from "./models/article.ts";
import Profile from "./models/profile.ts";
function htmlLayout(title: string, body: string, profile?: Profile): string {
let feedLinksHtml = "";
if (profile) {
feedLinksHtml =
`<link rel="alternate" type="application/atom+xml" href="/@${profile.username}/articles.atom" title="Articles by ${profile.name}" />`;
}
function htmlLayout(title: string, body: string, profile: Profile): string {
return `
<!DOCTYPE html>
<html>
@ -17,7 +11,7 @@ function htmlLayout(title: string, body: string, profile?: Profile): string {
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>${title}</title>
${feedLinksHtml}
<link rel="alternate" type="application/atom+xml" href="/@${profile.username}/articles.atom" title="Articles by ${profile.name}" />
<link rel="stylesheet" type="text/css" href="/assets/css/layout.css" />
<link rel="stylesheet" type="text/css" href="/assets/css/themes/default-light.css" />
</head>
@ -28,16 +22,6 @@ function htmlLayout(title: string, body: string, profile?: Profile): string {
`;
}
export function errorPageHtml(statusCode: number, title: string): string {
const body = `
<main>
<h1>${statusCode} - ${title}</h1>
</main>
`;
return htmlLayout(title, body);
}
export function articleHtml(article: Article, profile: Profile): string {
const publishedAtFormatted = localizeDate(article.publishedAt);

View File

@ -6,7 +6,6 @@ import npubHandler from "./handlers/npub.ts";
import userProfileHandler from "./handlers/user-profile.ts";
import userEventHandler from "./handlers/user-event.ts";
import userAtomFeedHandler from "./handlers/user-atom-feed.ts";
import notFoundHandler from "./handlers/not-found.ts";
const router = new Router();
@ -22,20 +21,19 @@ router.get("/:path", async (ctx: ctx) => {
} else if (path.startsWith("@") || path.startsWith("~")) {
await userProfileHandler(ctx);
} else {
notFoundHandler(ctx);
ctx.response.status = 404;
ctx.response.body = "Not Found";
}
});
router.get("/:user/:kind.atom", async (ctx: ctx) => {
const { user, kind } = ctx.params;
const { user } = ctx.params;
if (
kind === "articles" &&
(user.startsWith("@") || user.startsWith("~"))
) {
if (user.startsWith("@") || user.startsWith("~") || kind === "articles") {
await userAtomFeedHandler(ctx);
} else {
notFoundHandler(ctx);
ctx.response.status = 404;
ctx.response.body = "Not Found";
}
});
@ -45,7 +43,8 @@ router.get("/:user/:identifier", async (ctx: ctx) => {
if (user.startsWith("@") || user.startsWith("~")) {
await userEventHandler(ctx);
} else {
notFoundHandler(ctx);
ctx.response.status = 404;
ctx.response.body = "Not Found";
}
});
@ -57,7 +56,8 @@ router.get("/assets/:path*", async (ctx) => {
root: `${Deno.cwd()}/assets`,
});
} catch (_e) {
notFoundHandler(ctx);
ctx.response.status = 404;
ctx.response.body = "Not Found";
}
});