diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/nostr.js | 44 | ||||
| -rw-r--r-- | lib/utils.js | 3 |
2 files changed, 33 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; diff --git a/lib/utils.js b/lib/utils.js index 17283836..5355cc2c 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -10,6 +10,7 @@ import lnd from "$lib/lnd"; import ln from "$lib/ln"; import { coinos } from "$lib/nostr"; + const { Op } = sequelize; export const sleep = n => new Promise(r => setTimeout(r, n)); @@ -204,3 +205,5 @@ export const derivePayRequest = async ({ amount, memo, tip }) => { await ln.invoice(value ? `${value}sat` : "any", new Date(), memo, 360) ).bolt11; }; + +export const uniq = (a, k) => [...new Map(a.map(x => [k(x), x])).values()]; |
