WIP Resolve username via npub, LDAP

This commit is contained in:
2024-10-20 22:52:47 +02:00
parent b618c6a1a1
commit 9a19f7249c
10 changed files with 248 additions and 13 deletions

View File

@@ -1,4 +1,4 @@
import { ctx } from "@oak/oak";
import { Context } from "@oak/oak";
import { nip19 } from "@nostr/tools";
import { log } from "../log.ts";
import { articleHtml } from "../html.ts"
@@ -7,12 +7,12 @@ import {
fetchProfileEvent
} from "../nostr.ts";
const naddrHandler = async function (ctx: ctx) {
const naddrHandler = async function (ctx: Context) {
const { request } = ctx;
const { path } = ctx.params;
const naddr = ctx.params.path;
try {
const r = nip19.decode(path);
const r = nip19.decode(naddr);
const articleEvent = await fetchReplaceableEvent(r.data.pubkey, r.data.identifier);
const profileEvent = await fetchProfileEvent(r.data.pubkey);
let profile;

View File

@@ -1,10 +1,30 @@
import { Context } from "@oak/oak";
import { nip19 } from "@nostr/tools";
import { log } from "../log.ts";
import { fetchProfileEvent } from "../nostr.ts";
import { profilePageHtml } from "../html.ts"
const nprofileHandler = function (context: Context) {
const { request, response } = context;
const fullPath = request.url.pathname;
const nprofileHandler = async function (ctx: Context) {
const { request } = ctx;
const nprofile = ctx.params.path;
response.body = `You are viewing an nprofile with address: ${fullPath}`;
try {
const r = nip19.decode(nprofile);
const profileEvent = await fetchProfileEvent(r.data.pubkey);
if (profileEvent) {
const html = profilePageHtml(profileEvent);
ctx.response.body = html;
} 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 nprofileHandler;

30
handlers/npub.ts Normal file
View File

@@ -0,0 +1,30 @@
import { Context } from "@oak/oak";
import { nip19 } from "@nostr/tools";
import { log } from "../log.ts";
import { fetchProfileEvent } from "../nostr.ts";
import { profilePageHtml } from "../html.ts"
const npubHandler = async function (ctx: Context) {
const { request } = ctx;
const npub = ctx.params.path;
try {
const r = nip19.decode(npub);
const profileEvent = await fetchProfileEvent(r.data);
if (profileEvent) {
const html = profilePageHtml(profileEvent);
ctx.response.body = html;
} 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;

31
handlers/username.ts Normal file
View File

@@ -0,0 +1,31 @@
import { Context } from "@oak/oak";
import { nip19 } from "@nostr/tools";
import { log } from "../log.ts";
import { lookupPubkeyByUsername } from "../ldap.ts";
import { fetchProfileEvent } from "../nostr.ts";
import { profilePageHtml } from "../html.ts"
const usernameHandler = async function (ctx: Context) {
const { request } = ctx;
const username = ctx.params.path.replace(/^@/, '');;
const pubkey = await lookupPubkeyByUsername(username);
try {
const profileEvent = await fetchProfileEvent(pubkey);
if (profileEvent) {
const html = profilePageHtml(profileEvent);
ctx.response.body = html;
} 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 usernameHandler;