diff options
| author | John Barratt <[email protected]> | 2023-02-28 13:55:59 -0800 |
|---|---|---|
| committer | John Barratt <[email protected]> | 2023-02-28 13:55:59 -0800 |
| commit | e47d50a443f0a396321716a506e1df325dd4a1fb (patch) | |
| tree | 1c010c5970e9cbfbf8671e6f00a97d6518446fdd /lib/nostr.js | |
| parent | 58e996a4ac227162f171e84530fe898cb504e045 (diff) | |
| parent | e4702b4d13e4d966d735c2ba4657e10d7c436a57 (diff) | |
| download | coinos-server-e47d50a443f0a396321716a506e1df325dd4a1fb.tar.gz coinos-server-e47d50a443f0a396321716a506e1df325dd4a1fb.zip | |
Merge branch 'master' of https://github.com/coinos/coinos-server into notifications
Diffstat (limited to 'lib/nostr.js')
| -rw-r--r-- | lib/nostr.js | 42 |
1 files changed, 29 insertions, 13 deletions
diff --git a/lib/nostr.js b/lib/nostr.js index 065fafb0..604d207d 100644 --- a/lib/nostr.js +++ b/lib/nostr.js @@ -1,16 +1,17 @@ import config from "$config"; import { Relay, RelayPool } from "nostr"; -import { broadcast } from "$lib/sockets"; +import { broadcast, emit } from "$lib/sockets"; import store from "$lib/store"; import { g, s, db } from "$lib/db"; import { nada, wait } from "$lib/utils"; +export let coinos; export let pool; export let fillPool = () => { pool = RelayPool(config.relays); pool.on("open", relay => { - if (relay.url.includes(config.relay)) coinos = relay; + if (relay.url.includes(config.nostr)) coinos = relay; relay.subscribe("live", { limit: 1 }); }); @@ -39,6 +40,13 @@ export let fillPool = () => { // coinos.send(["EVENT", ev]); ev.user = await getUser(pubkey); // broadcast("event", ev); + + if (ev.kind === 4) { + let uid = await g(`user:${ev.tags[0][1]}`); + if (uid) ev.recipient = await g(`user:${uid}`); + ev.author = ev.user; + emit(uid, "event", ev); + } } } else if (sub.includes("profile")) { let { pubkey } = ev; @@ -59,9 +67,13 @@ export let fillPool = () => { await s(`user:${pubkey}`, JSON.stringify(user)); coinos.send(ev); + } else if (sub.includes("messages")) { + let pubkey = sub.split(":")[0]; + await db.sAdd(`${pubkey}:messages`, ev.id); + await s(`ev:${ev.id}`, ev); } else if (sub.includes("follows")) { let pubkey = sub.split(":")[0]; - await s(`${pubkey}:follows`, JSON.stringify(ev.tags)); + await s(`${pubkey}:follows`, ev.tags); for (let f of ev.tags) { let [_, followPubkey] = f; await db.sAdd(`${followPubkey}:followers`, pubkey); @@ -75,7 +87,7 @@ export let fillPool = () => { } else if (sub.includes("notes")) { let pubkey = sub.split(":")[0]; await db.sAdd(pubkey, ev.id); - await s(`ev:${ev.id}`, JSON.stringify(ev)); + await s(`ev:${ev.id}`, ev); } } catch (e) { console.log(e); @@ -83,7 +95,6 @@ export let fillPool = () => { }); }; -export let coinos; let now = () => Math.round(Date.now() / 1000); let timeouts = {}; @@ -96,8 +107,8 @@ export let q = async ( let start = Date.now(); let seen = []; let rejected; - query.since = await g(`since:${sub}`); - if (now() - query.since < since) return r(); + // query.since = await g(`since:${sub}`); + // if (now() - query.since < since) return r(); let done = { [sub]: [] }; @@ -156,9 +167,14 @@ export let q = async ( let seen = []; -let getUser = async pubkey => - JSON.parse(await g(`user:${pubkey}`)) || { - username: pubkey.substr(0, 6), - pubkey, - anon: true - }; +let getUser = async pubkey => { + let id = await g(`user:${pubkey}`); + let user = await g(`user:${id}`); + return ( + user || { + username: pubkey.substr(0, 6), + pubkey, + anon: true + } + ); +}; |
