Support pretty URLs for articles, redirect from naddr
This commit is contained in:
parent
18f3e888a6
commit
fe9db7509d
@ -1,27 +1,17 @@
|
|||||||
import { Context } from "@oak/oak";
|
import { Context } from "@oak/oak";
|
||||||
import { nip19 } from "@nostr/tools";
|
import { nip19 } from "@nostr/tools";
|
||||||
import { log } from "../log.ts";
|
import { log } from "../log.ts";
|
||||||
import { articleHtml } from "../html.ts";
|
import { lookupUsernameByPubkey } from "../ldap.ts";
|
||||||
import { fetchProfileEvent, fetchReplaceableEvent } from "../nostr.ts";
|
|
||||||
|
|
||||||
const naddrHandler = async function (ctx: Context) {
|
const naddrHandler = async function (ctx: Context) {
|
||||||
const { request } = ctx;
|
|
||||||
const naddr = ctx.params.path;
|
const naddr = ctx.params.path;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const r = nip19.decode(naddr);
|
const r = nip19.decode(naddr);
|
||||||
const articleEvent = await fetchReplaceableEvent(
|
const username = await lookupUsernameByPubkey(r.data.pubkey);
|
||||||
r.data.pubkey,
|
|
||||||
r.data.identifier,
|
|
||||||
);
|
|
||||||
const profileEvent = await fetchProfileEvent(r.data.pubkey);
|
|
||||||
let profile;
|
|
||||||
|
|
||||||
if (articleEvent && profileEvent) {
|
if (username && r.data.identifier) {
|
||||||
const profile = JSON.parse(profileEvent.content);
|
ctx.response.redirect(`/@${username}/${r.data.identifier}`);
|
||||||
const html = articleHtml(articleEvent, profile);
|
|
||||||
|
|
||||||
ctx.response.body = html;
|
|
||||||
} else {
|
} else {
|
||||||
ctx.response.status = 404;
|
ctx.response.status = 404;
|
||||||
ctx.response.body = "Not Found";
|
ctx.response.body = "Not Found";
|
||||||
|
45
handlers/user-event.ts
Normal file
45
handlers/user-event.ts
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
import { Context } from "@oak/oak";
|
||||||
|
import { log } from "../log.ts";
|
||||||
|
import { lookupPubkeyByUsername } from "../ldap.ts";
|
||||||
|
import {
|
||||||
|
fetchProfileEvent,
|
||||||
|
fetchReplaceableEvent,
|
||||||
|
} from "../nostr.ts";
|
||||||
|
import { articleHtml } from "../html.ts";
|
||||||
|
|
||||||
|
const userEventHandler = async function (ctx: Context) {
|
||||||
|
const username = ctx.params.user.replace(/^(@|~)/, "");
|
||||||
|
const identifier = ctx.params.identifier;
|
||||||
|
const pubkey = await lookupPubkeyByUsername(username);
|
||||||
|
|
||||||
|
if (!pubkey) {
|
||||||
|
ctx.response.status = 404;
|
||||||
|
ctx.response.body = "Not Found";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const articleEvent = await fetchReplaceableEvent(
|
||||||
|
pubkey,
|
||||||
|
identifier,
|
||||||
|
);
|
||||||
|
const profileEvent = await fetchProfileEvent(pubkey);
|
||||||
|
let profile;
|
||||||
|
|
||||||
|
if (articleEvent && profileEvent) {
|
||||||
|
profile = JSON.parse(profileEvent.content);
|
||||||
|
const html = articleHtml(articleEvent, profile);
|
||||||
|
|
||||||
|
ctx.response.body = html;
|
||||||
|
} 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 userEventHandler;
|
17
main.ts
17
main.ts
@ -4,6 +4,7 @@ import naddrHandler from "./handlers/naddr.ts";
|
|||||||
import nprofileHandler from "./handlers/nprofile.ts";
|
import nprofileHandler from "./handlers/nprofile.ts";
|
||||||
import npubHandler from "./handlers/npub.ts";
|
import npubHandler from "./handlers/npub.ts";
|
||||||
import usernameHandler from "./handlers/username.ts";
|
import usernameHandler from "./handlers/username.ts";
|
||||||
|
import userEventHandler from "./handlers/user-event.ts";
|
||||||
|
|
||||||
const router = new Router();
|
const router = new Router();
|
||||||
|
|
||||||
@ -29,6 +30,22 @@ router.get("/:path", async (ctx: ctx) => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
router.get("/:user/:identifier", async (ctx: ctx) => {
|
||||||
|
const { user } = ctx.params;
|
||||||
|
|
||||||
|
if (user.startsWith("@") || user.startsWith("~")) {
|
||||||
|
await userEventHandler(ctx);
|
||||||
|
} else {
|
||||||
|
ctx.response.status = 404;
|
||||||
|
ctx.response.body = "Not Found";
|
||||||
|
}
|
||||||
|
|
||||||
|
log(
|
||||||
|
`${ctx.request.method} ${ctx.request.url} - ${ctx.response.status}`,
|
||||||
|
"gray",
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
const app = new Application();
|
const app = new Application();
|
||||||
app.use(router.routes());
|
app.use(router.routes());
|
||||||
app.use(router.allowedMethods());
|
app.use(router.allowedMethods());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user