git ssb

2+

mixmix / ticktack



Tree: 1c2359030ac279f1fdfef805d7938f3003372c24

Files: 1c2359030ac279f1fdfef805d7938f3003372c24 / channel / obs / subscribedTo.js

1008 bytesRaw
1const nest = require('depnest')
2const ref = require('ssb-ref')
3const { computed, onceTrue } = require('mutant')
4
5exports.needs = nest({
6 'keys.sync.id': 'first',
7 'channel.obs.subscribed': 'first',
8 'sbot.obs.connection': 'first'
9})
10
11exports.gives = nest('channel.obs.isSubscribedTo')
12
13exports.create = function (api) {
14 var subscriptions = {}
15 var myId
16
17 return nest('channel.obs.isSubscribedTo', isSubscribedTo)
18
19 function isSubscribedTo (channel, id) {
20 // channel = channel.replace(/^#/, '')
21 if (!ref.isFeed(id)) {
22 id = getMyId()
23 }
24
25 // TODO - use ssb-server-channel index to make a better subscribed obs
26 return computed(getSubscriptions(id), channels => {
27 return channels.has(channel)
28 })
29 }
30
31 // cache getters
32
33 function getMyId () {
34 if (!myId) myId = api.keys.sync.id()
35 return myId
36 }
37
38 function getSubscriptions (id) {
39 if (subscriptions[id] === undefined) subscriptions[id] = api.channel.obs.subscribed(id)
40 return subscriptions[id]
41 }
42}
43
44

Built with git-ssb-web