Redirect npub and nprofile to username if exists

This commit is contained in:
2024-10-20 23:05:45 +02:00
parent 9a19f7249c
commit 0e5b4b1807
4 changed files with 35 additions and 11 deletions

View File

@@ -1,6 +1,7 @@
import { Context } from "@oak/oak";
import { nip19 } from "@nostr/tools";
import { log } from "../log.ts";
import { lookupUsernameByPubkey } from "../ldap.ts";
import { fetchProfileEvent } from "../nostr.ts";
import { profilePageHtml } from "../html.ts"
@@ -10,12 +11,10 @@ const npubHandler = async function (ctx: Context) {
try {
const r = nip19.decode(npub);
const profileEvent = await fetchProfileEvent(r.data);
const username = await lookupUsernameByPubkey(r.data);
if (profileEvent) {
const html = profilePageHtml(profileEvent);
ctx.response.body = html;
if (username) {
ctx.response.redirect(`/@${username}`);
} else {
ctx.response.status = 404;
ctx.response.body = "Not Found";