substr/handlers/nprofile.ts
2024-10-23 00:27:20 +02:00

24 lines
620 B
TypeScript

import { Context } from "@oak/oak";
import { nip19 } from "@nostr/tools";
import { lookupUsernameByPubkey } from "../directory.ts";
import notFoundHandler from "../handlers/not-found.ts";
const nprofileHandler = async function (ctx: Context) {
const nprofile = ctx.state.path;
try {
const data = nip19.decode(nprofile).data as nip19.ProfilePointer;
const username = await lookupUsernameByPubkey(data.pubkey);
if (username) {
ctx.response.redirect(`/@${username}`);
} else {
notFoundHandler(ctx);
}
} catch (_e) {
notFoundHandler(ctx);
}
};
export default nprofileHandler;