git ssb

2+

mixmix / ticktack



Tree: ffaed8d26b49d20b5798ee6e1958fa206bedaf0a

Files: ffaed8d26b49d20b5798ee6e1958fa206bedaf0a / channel / async.js

1072 bytesRaw
1var nest = require('depnest')
2var ref = require('ssb-ref')
3
4exports.needs = nest({
5 'keys.sync.id': 'first',
6 'sbot.async.publish': 'first',
7 'channel.obs.subscribed': 'first',
8})
9
10exports.gives = nest({
11 'channel.async': ['subscribe', 'unsubscribe', 'isSubscribed']
12})
13
14exports.create = function (api) {
15 return nest({
16 'channel.async': {subscribe, unsubscribe, isSubscribed}
17 })
18
19 function subscribe (channel, cb) {
20 if (!channel) throw new Error('a channel must be specified')
21 api.sbot.async.publish({
22 type: 'channel',
23 channel: channel,
24 subscribed: true
25 }, cb)
26 }
27
28 function unsubscribe (channel, cb) {
29 if (!channel) throw new Error('a channel must be specified')
30 api.sbot.async.publish({
31 type: 'channel',
32 channel: channel,
33 subscribed: false
34 }, cb)
35 }
36
37 function isSubscribed (channel, cb) {
38 const myId = api.keys.sync.id()
39
40 const { subscribed } = api.channel.obs
41 const myChannels = subscribed(myId)
42 let v = myChannels().values()
43 return [...v].includes(channel)
44 }
45}
46

Built with git-ssb-web