aboutsummaryrefslogtreecommitdiffstats
path: root/lib/nostr.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2023-01-08 21:05:34 -0800
committerAdam Soltys <[email protected]>2023-01-08 21:05:34 -0800
commitdce07aa93b3c2f47aca0c24cea6adde9ec37f1a7 (patch)
tree6c6b8930ebe9c2cc5b8d1f759e61260f509fdd7a /lib/nostr.js
parentf7b844883506a8072213245b9b8a2b988694e4f5 (diff)
downloadcoinos-server-dce07aa93b3c2f47aca0c24cea6adde9ec37f1a7.tar.gz
coinos-server-dce07aa93b3c2f47aca0c24cea6adde9ec37f1a7.zip
tech debt jubilee
Diffstat (limited to 'lib/nostr.js')
-rw-r--r--lib/nostr.js194
1 files changed, 186 insertions, 8 deletions
diff --git a/lib/nostr.js b/lib/nostr.js
index d170fed3..bc75cc96 100644
--- a/lib/nostr.js
+++ b/lib/nostr.js
@@ -5,16 +5,15 @@ import { broadcast } from "$lib/sockets";
import store from "$lib/store";
import redis from "$lib/redis";
import proxy from "@fastify/http-proxy";
-import { wait } from "$lib/utils";
+import { nada, wait } from "$lib/utils";
-app.register(proxy, {
- upstream: config.nostr,
- prefix: "/nostr",
- rewritePrefix: "",
- disableRequestLogging: true
-});
+export let pool;
+try {
+ pool = RelayPool(config.relays);
+} catch(e) {
+ console.log(e)
+}
-export const pool = RelayPool(config.relays);
pool.on("open", relay => {
if (relay.url.includes(config.relay)) coinos = relay;
@@ -168,3 +167,182 @@ pool.on("event", async (relay, sub, ev) => {
console.log(e);
}
});
+
+app.get("/event/:id", async (req, res) => {
+ try {
+ let event = JSON.parse(await redis.get(`ev:${req.params.id}`));
+ let { pubkey } = event;
+
+ event.user = JSON.parse(await redis.get(`user:${pubkey}`)) || {
+ username: pubkey.substr(0, 6),
+ pubkey,
+ anon: true,
+ follows: [],
+ followers: []
+ };
+
+ res.send(event);
+ } catch (e) {
+ res.code(500).send("Problem fetching event");
+ }
+});
+
+app.get("/:pubkey/notes", async (req, res) => {
+ try {
+ let { pubkey } = req.params;
+
+ let user = JSON.parse(await redis.get(`user:${pubkey}`));
+
+ if (!user) {
+ user = await db.User.findOne({
+ where: { pubkey }
+ });
+ }
+
+ if (!user)
+ user = {
+ username: pubkey.substr(0, 6),
+ pubkey,
+ anon: true,
+ follows: [],
+ followers: []
+ };
+
+ q(
+ `${pubkey}:notes`,
+ {
+ kinds: [1],
+ authors: [pubkey]
+ },
+ { since: 0 }
+ ).catch(nada);
+
+ await redis.set(`user:${pubkey}`, JSON.stringify(user));
+
+ let ids = await redis.sMembers(pubkey);
+
+ let events = ids.length
+ ? (await redis.mGet(ids.map(k => "ev:" + k))).map(JSON.parse)
+ : [];
+
+ res.send(events.map(e => ({ ...e, user })));
+ } catch (e) {
+ console.log(e);
+ res.code(500).send("problem fetching user events");
+ }
+});
+
+app.post("/event", async (req, res) => {
+ let { event } = req.body;
+ pool.send(["EVENT", event]);
+ res.send(event);
+});
+
+app.get("/:pubkey/follows", async (req, res) => {
+ try {
+ let { pubkey } = req.params;
+ let { tagsonly } = req.query;
+
+ let sub = `${pubkey}:follows`;
+ q(
+ sub,
+ {
+ limit: 1,
+ kinds: [3],
+ authors: [pubkey]
+ },
+ { timeout: 60000, eager: 60000 }
+ ).catch(nada);
+
+ let tags = JSON.parse(await redis.get(`${pubkey}:follows`)) || [];
+ if (tagsonly) return res.send(tags);
+
+ let follows = [];
+ for (let f of tags) {
+ let [_, pubkey] = f;
+
+ q(`${pubkey}:profile:f1`, {
+ limit: 1,
+ kinds: [0],
+ authors: [pubkey]
+ }).catch(nada);
+
+ let user = JSON.parse(await redis.get(`user:${pubkey}`));
+
+ if (!user)
+ user = {
+ username: pubkey.substr(0, 6),
+ pubkey,
+ anon: true
+ };
+
+ follows.push(user);
+ }
+
+ follows = uniq(follows, e => e.pubkey);
+ follows.sort((a, b) => a.username.localeCompare(b.username));
+
+ res.send(follows);
+ } catch (e) {
+ console.log(e);
+ res.code(500).send(e && e.message);
+ }
+});
+
+app.get("/:pubkey/followers", async (req, res) => {
+ try {
+ let { pubkey } = req.params;
+
+ let pubkeys = [
+ ...new Set([
+ ...(await got(`${config.nostr}/followers?pubkey=${pubkey}`).json()),
+ ...(await redis.sMembers(`${pubkey}:followers`))
+ ])
+ ];
+
+ let followers = [];
+
+ q(
+ `${pubkey}:followers`,
+ { kinds: [3], "#p": [pubkey] },
+ { timeout: 60000, eager: 60000 }
+ ).catch(nada);
+
+ for (let pubkey of pubkeys) {
+ let user = JSON.parse(await redis.get(`user:${pubkey}`));
+ if (!user || !user.updated) {
+ q(`${pubkey}:profile:f2`, {
+ limit: 1,
+ kinds: [0],
+ authors: [pubkey]
+ }).catch(nada);
+
+ user = JSON.parse(await redis.get(`user:${pubkey}`));
+ }
+
+ if (!user)
+ user = {
+ username: pubkey.substr(0, 6),
+ pubkey,
+ anon: true
+ };
+
+ followers.push(user);
+ }
+
+ followers = uniq(followers, e => e.pubkey);
+ followers.sort((a, b) => a.username.localeCompare(b.username));
+
+ res.send(followers);
+ } catch (e) {
+ console.log(e);
+ res.code(500).send(e && e.message);
+ }
+});
+
+app.get('/nostr.json', async (req, res) => {
+ res.send({
+ names: { adam: "566c166f3adab0c8fba5da015b0b3bcc8eb3696b455f2a1d43bfbd97059646a8", asoltys: "566c166f3adab0c8fba5da015b0b3bcc8eb3696b455f2a1d43bfbd97059646a8" }
+ });
+});
+