Files: adca29179dee2177af6fefa725c91581ffef5395 / channel / obs.js
994 bytesRaw
1 | const nest = require('depnest') |
2 | const ref = require('ssb-ref') |
3 | const { computed, onceTrue } = require('mutant') |
4 | |
5 | exports.needs = nest({ |
6 | 'keys.sync.id': 'first', |
7 | 'channel.obs.subscribed': 'first', |
8 | 'sbot.obs.connection': 'first' |
9 | }) |
10 | |
11 | exports.gives = nest('channel.obs.isSubscribedTo') |
12 | |
13 | exports.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), set => { |
27 | return set.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 |
Built with git-ssb-web