substr/handlers/npub.ts
2024-10-21 00:30:34 +02:00

27 lines
647 B
TypeScript

import { Context } from "@oak/oak";
import { nip19 } from "@nostr/tools";
import { log } from "../log.ts";
import { lookupUsernameByPubkey } from "../ldap.ts";
const npubHandler = async function (ctx: Context) {
const npub = ctx.params.path;
try {
const r = nip19.decode(npub);
const username = await lookupUsernameByPubkey(r.data);
if (username) {
ctx.response.redirect(`/@${username}`);
} 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 npubHandler;