aboutsummaryrefslogtreecommitdiffstats
path: root/routes/nostr.ts
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2025-01-20 00:19:09 -0800
committerAdam Soltys <[email protected]>2025-01-20 00:19:09 -0800
commit263ffd68ec8051dfad47d20fcb1ab1ea22ade520 (patch)
treeb3d2db9cfd9b7b8a5f1fd328cf8b55d9196862ab /routes/nostr.ts
parenta55eb812c4ea6dc9573f2bdf40f058a1d3ba90ee (diff)
downloadcoinos-server-263ffd68ec8051dfad47d20fcb1ab1ea22ade520.tar.gz
coinos-server-263ffd68ec8051dfad47d20fcb1ab1ea22ade520.zip
nostr threads
Diffstat (limited to 'routes/nostr.ts')
-rw-r--r--routes/nostr.ts57
1 files changed, 57 insertions, 0 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;