git ssb

10+

Matt McKegg / patchwork



Tree: 05c5b4f2078be5153b1a4ff2c0f7455a930242b1

Files: 05c5b4f2078be5153b1a4ff2c0f7455a930242b1 / modules / obs-subscribed-channels.js

1217 bytesRaw
1var pull = require('pull-stream')
2var computed = require('@mmckegg/mutant/computed')
3var MutantPullReduce = require('../lib/mutant-pull-reduce')
4var plugs = require('patchbay/plugs')
5var sbot_user_feed = plugs.first(exports.sbot_user_feed = [])
6var cache = {}
7
8exports.obs_subscribed_channels = function (userId) {
9 if (cache[userId]) {
10 return cache[userId]
11 } else {
12 var stream = pull(
13 sbot_user_feed({id: userId, live: true}),
14 pull.filter((msg) => {
15 return !msg.value || msg.value.content.type === 'channel'
16 })
17 )
18
19 var result = MutantPullReduce(stream, (result, msg) => {
20 var c = msg.value.content
21 if (typeof c.channel === 'string' && c.channel) {
22 var channel = c.channel.trim()
23 if (channel) {
24 if (typeof c.subscribed === 'boolean') {
25 if (c.subscribed) {
26 result.add(channel)
27 } else {
28 result.delete(channel)
29 }
30 }
31 }
32 }
33 return result
34 }, {
35 startValue: new Set(),
36 nextTick: true
37 })
38
39 result.has = function (value) {
40 return computed(result, x => x.has(value))
41 }
42
43 cache[userId] = result
44 return result
45 }
46
47}
48

Built with git-ssb-web