24 lines
587 B
TypeScript
24 lines
587 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 npubHandler = async function (ctx: Context) {
|
|
const npub = ctx.state.path;
|
|
|
|
try {
|
|
const pubkey = nip19.decode(npub).data as string;
|
|
const username = await lookupUsernameByPubkey(pubkey);
|
|
|
|
if (username) {
|
|
ctx.response.redirect(`/@${username}`);
|
|
} else {
|
|
notFoundHandler(ctx);
|
|
}
|
|
} catch (_e) {
|
|
notFoundHandler(ctx);
|
|
}
|
|
};
|
|
|
|
export default npubHandler;
|