substr/handlers/npub.ts

26 lines
622 B
TypeScript

import { Context } from "@oak/oak";
import { nip19 } from "@nostr/tools";
import { log } from "../log.ts";
import { lookupUsernameByPubkey } from "../directory.ts";
import notFoundHandler from "../handlers/not-found.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 {
notFoundHandler(ctx);
}
} catch (e) {
log(e, "yellow");
notFoundHandler(ctx);
}
};
export default npubHandler;