Files: 8bd323651fb1b0b76a2af4d86690309d221f6ab1 / modules / obs-following.js
1089 bytesRaw
1 | var pull = require('pull-stream') |
2 | var computed = require('@mmckegg/mutant/computed') |
3 | var MutantPullReduce = require('../lib/mutant-pull-reduce') |
4 | var plugs = require('patchbay/plugs') |
5 | var sbot_user_feed = plugs.first(exports.sbot_user_feed = []) |
6 | var cache = {} |
7 | |
8 | exports.obs_following = 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 === 'contact' |
16 | }) |
17 | ) |
18 | |
19 | var result = MutantPullReduce(stream, (result, msg) => { |
20 | var c = msg.value.content |
21 | if (c.contact) { |
22 | if (typeof c.following === 'boolean') { |
23 | if (c.following) { |
24 | result.add(c.contact) |
25 | } else { |
26 | result.delete(c.contact) |
27 | } |
28 | } |
29 | } |
30 | return result |
31 | }, { |
32 | startValue: new Set(), |
33 | nextTick: true |
34 | }) |
35 | |
36 | result.has = function (value) { |
37 | return computed(result, x => x.has(value)) |
38 | } |
39 | |
40 | cache[userId] = result |
41 | return result |
42 | } |
43 | } |
44 |
Built with git-ssb-web