diff options
| author | Adam Soltys <[email protected]> | 2023-01-16 23:40:25 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2023-01-16 23:40:25 -0800 |
| commit | 91e847126c832efa25ab0bf8d6f3b7e537378fa7 (patch) | |
| tree | 4a42b6e3bec6eb0f640ed44533b54e6d3d6a5e0e /routes/nostr.js | |
| parent | 6bafea9bffe24d2227d269812a73a9a513d92ddf (diff) | |
| download | coinos-server-91e847126c832efa25ab0bf8d6f3b7e537378fa7.tar.gz coinos-server-91e847126c832efa25ab0bf8d6f3b7e537378fa7.zip | |
shuffle things around
Diffstat (limited to 'routes/nostr.js')
| -rw-r--r-- | routes/nostr.js | 161 |
1 files changed, 161 insertions, 0 deletions
diff --git a/routes/nostr.js b/routes/nostr.js new file mode 100644 index 00000000..3d326e94 --- /dev/null +++ b/routes/nostr.js @@ -0,0 +1,161 @@ +export default { + async event({ params: { id } }, res) { + let event = JSON.parse(await g(`ev:${id}`)); + let { pubkey } = event; + + event.user = JSON.parse(await g(`user:${pubkey}`)) || { + username: pubkey.substr(0, 6), + pubkey, + anon: true, + follows: [], + followers: [] + }; + + res.send(event); + }, + + async notes({ params: { pubkey } }, res) { + let user = JSON.parse(await g(`user:${pubkey}`)); + + if (!user) { + user = await db.User.findOne({ + where: { pubkey } + }); + } + + if (!user) + user = { + username: pubkey.substr(0, 6), + pubkey, + anon: true, + follows: [], + followers: [] + }; + + let params = { + kinds: [1], + authors: [pubkey] + }, + opts = { since: 0 }; + + q(`${pubkey}:notes`, params, opts).catch(nada); + + await g(`user:${pubkey}`, JSON.stringify(user)); + + let ids = await db.sMembers(pubkey); + + let events = ids.length + ? (await db.mGet(ids.map(k => "ev:" + k))).map(JSON.parse) + : []; + + res.send(events.map(e => ({ ...e, user }))); + }, + + async broadcast(req, res) { + let { event } = req.body; + pool.send(["EVENT", event]); + res.send(event); + }, + + async follows({ params: { pubkey }, query: { tagsOnly } }, res) { + let sub = `${pubkey}:follows`, + params = { + limit: 1, + kinds: [3], + authors: [pubkey] + }, + opts = { timeout: 60000, eager: 60000 }; + + q(sub, params, opts).catch(nada); + + let tags = JSON.parse(await g(`${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 g(`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); + }, + + async followers({ params: { pubkey}}, res) { + try { + let pubkeys = [ + ...new Set([ + ...(await got(`${config.nostr}/followers?pubkey=${pubkey}`).json()), + ...(await db.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 g(`user:${pubkey}`)); + if (!user || !user.updated) { + q(`${pubkey}:profile:f2`, { + limit: 1, + kinds: [0], + authors: [pubkey] + }).catch(nada); + + user = JSON.parse(await g(`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); + } + }, + + async identities(req, res) { + res.send({ + names: { + adam: + "566c166f3adab0c8fba5da015b0b3bcc8eb3696b455f2a1d43bfbd97059646a8", + asoltys: + "566c166f3adab0c8fba5da015b0b3bcc8eb3696b455f2a1d43bfbd97059646a8" + } + }); + } +}; |
