aboutsummaryrefslogtreecommitdiffstats
path: root/routes
diff options
context:
space:
mode:
Diffstat (limited to 'routes')
-rw-r--r--routes/nostr.ts57
-rw-r--r--routes/users.ts36
2 files changed, 60 insertions, 33 deletions
diff --git a/routes/nostr.ts b/routes/nostr.ts
index 7e3dfcc7..61e23c30 100644
--- a/routes/nostr.ts
+++ b/routes/nostr.ts
@@ -4,6 +4,7 @@ import ln from "$lib/ln";
import {
anon,
getCount,
+ getNostrUser,
getProfile,
getRelays,
send,
@@ -17,6 +18,7 @@ import type { Event } from "nostr-tools";
import { decode } from "nostr-tools/nip19";
import type { ProfilePointer } from "nostr-tools/nip19";
import { getZapEndpoint, makeZapRequest } from "nostr-tools/nip57";
+import { Relay } from "nostr-tools/relay";
export default {
async event(req, res) {
@@ -51,6 +53,61 @@ export default {
res.send({ parts, names });
},
+ async thread(req, res) {
+ try {
+ const { id } = req.params;
+ const relay = await Relay.connect("wss://relay.primal.net");
+
+ console.log("ID", id);
+ const event: Event = await new Promise((resolve) => {
+ const sub = relay.subscribe([{ ids: [id] }], {
+ async onevent(event) {
+ resolve(event);
+ },
+ oneose() {
+ sub.close();
+ },
+ });
+ });
+
+ const thread = [];
+
+ const root =
+ event.tags.find((tag) => tag[0] === "e" && tag[3] === "root")?.[1] ||
+ event.id;
+
+ await new Promise<void>((resolve) => {
+ const sub = relay.subscribe([{ kinds: [1], "#e": [root] }], {
+ async onevent(event) {
+ thread.push(event);
+ },
+ oneose() {
+ sub.close();
+ resolve();
+ },
+ });
+ });
+
+ for (const e of thread) {
+ e.author = await getNostrUser(e.pubkey);
+ console.log("AUTHOR", e.author);
+ e.parts = parseContent(e);
+ e.names = {};
+ for (const { type, value } of e.parts) {
+ if (type.includes("nprofile") || type.includes("npub")) {
+ const { name } = await getProfile(value.pubkey);
+ e.names[value.pubkey] = name;
+ }
+ }
+ }
+
+ res.send(thread);
+ } catch (e) {
+ console.log(e);
+ bail(res, e.message);
+ }
+ },
+
async zaps(req, res) {
try {
let { id } = req.params;
diff --git a/routes/users.ts b/routes/users.ts
index 4bc54bd6..9b0fd240 100644
--- a/routes/users.ts
+++ b/routes/users.ts
@@ -5,7 +5,7 @@ import { requirePin } from "$lib/auth";
import { db, g, s } from "$lib/db";
import { err, l, warn } from "$lib/logging";
import { mail, templates } from "$lib/mail";
-import { getProfile } from "$lib/nostr";
+import { getNostrUser, getProfile } from "$lib/nostr";
import { reconcile, types } from "$lib/payments";
import register from "$lib/register";
import { emit } from "$lib/sockets";
@@ -13,7 +13,7 @@ import upload from "$lib/upload";
import { bail, fail, fields, getUser, pick } from "$lib/utils";
import whitelist from "$lib/whitelist";
import rpc from "@coinos/rpc";
-import { bytesToHex, randomBytes } from "@noble/hashes/utils";
+import { randomBytes } from "@noble/hashes/utils";
import { $ } from "bun";
import jwt from "jsonwebtoken";
import { getPublicKey, nip19, verifyEvent } from "nostr-tools";
@@ -86,37 +86,7 @@ export default {
} catch (e) {}
}
- let user = await getUser(key);
-
- if (key.length === 64) {
- const nostr: any = await getProfile(key);
- if (nostr) {
- nostr.username = nostr.name || key.substr(0, 6);
- nostr.display = nostr.display_name || nostr.displayName;
- nostr.display_name = undefined;
- nostr.displayName = undefined;
- nostr.name = undefined;
- }
-
- if (user) {
- user.anon = false;
- nostr.display = undefined;
- }
-
- user = {
- ...nostr,
- currency: "USD",
- pubkey: key,
- anon: true,
- ...user,
- };
- }
-
- if (!user) return res.code(500).send("User not found");
-
- if (user.pubkey) user.npub = nip19.npubEncode(user.pubkey);
- user.prompt = !!user.prompt;
-
+ const user = await getNostrUser(key);
res.send(pick(user, fields));
} catch (e) {
err("problem getting user", e.message);