aboutsummaryrefslogtreecommitdiffstats
path: root/lib/nostr.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2022-12-28 15:05:56 -0800
committerAdam Soltys <[email protected]>2022-12-28 15:05:56 -0800
commit33d8a7d5235281d705bc1ac27113e150b2b9c617 (patch)
tree2d37343db785e4d1cfc54fed193c8db545359797 /lib/nostr.js
parent581a905b2fc14240d0bcbe6e2c14023fae3589a4 (diff)
downloadcoinos-server-33d8a7d5235281d705bc1ac27113e150b2b9c617.tar.gz
coinos-server-33d8a7d5235281d705bc1ac27113e150b2b9c617.zip
fix follows and followers endpoints
Diffstat (limited to 'lib/nostr.js')
-rw-r--r--lib/nostr.js44
1 files changed, 30 insertions, 14 deletions
diff --git a/lib/nostr.js b/lib/nostr.js
index 257bca9e..034a4aaa 100644
--- a/lib/nostr.js
+++ b/lib/nostr.js
@@ -41,32 +41,35 @@ pool.on("event", async (relay, sub, ev) => {
let newuser = {
username: pubkey.substr(0, 6),
pubkey,
- anon: true
+ anon: true,
+ follows: [],
+ followers: []
};
ev.user = JSON.parse(await redis.get(`user:${pubkey}`)) || newuser;
- if (!ev.user.initialized) {
- relay.subscribe("zero", {
- limit: 1,
- kinds: [0],
- authors: [pubkey]
- });
- }
+ relay.subscribe("zero", {
+ limit: 1,
+ kinds: [0],
+ authors: [pubkey]
+ });
- if (ev.kind === 1) {
- broadcast("event", ev);
- }
+ broadcast("event", ev);
}
} else if (sub === "zero") {
let { pubkey } = ev;
let newuser = {
username: pubkey.substr(0, 6),
pubkey,
- anon: true
+ anon: true,
+ follows: [],
+ followers: []
};
let user = JSON.parse(await redis.get(`user:${pubkey}`)) || newuser;
+
+ if (user.profileUpdated > ev.created_at) return;
+
if (parsed.name && user.username === pubkey.substr(0, 6))
user.username = parsed.name;
@@ -75,17 +78,30 @@ pool.on("event", async (relay, sub, ev) => {
user = {
...user,
...parsed,
- initialized: true
+ initialized: true,
+ profileUpdated: ev.created_at
};
await redis.set(`user:${pubkey}`, JSON.stringify(user));
+ await redis.set(`user:${user.username}`, JSON.stringify(user));
} else if (sub.includes("follows")) {
let pubkey = sub.split(":")[0];
await redis.set(`${pubkey}:follows`, JSON.stringify(ev.tags));
for (let f of ev.tags) {
- await redis.sAdd(`${f[1]}:followers`, pubkey);
+ let [_, followPubkey] = f;
+ pool.subscribe("zero", {
+ limit: 1,
+ kinds: [0],
+ authors: [followPubkey]
+ });
+ await redis.sAdd(`${followPubkey}:followers`, pubkey);
}
store.fetching[pubkey] = false;
+ } else if (sub.includes("followers")) {
+ let followed = sub.split(":")[0];
+ let { pubkey } = ev;
+ await redis.sAdd(`${followed}:followers`, pubkey);
+ store.fetching[pubkey] = false;
} else {
if (!relay.url.includes("coinos")) return;