Files: 856d88afbef69775a9d80aca2652a45f598a0163 / modules / profile / obs / following.js
1401 bytesRaw
1 | var pull = require('pull-stream') |
2 | var computed = require('mutant/computed') |
3 | var MutantPullReduce = require('mutant-pull-reduce') |
4 | var throttle = require('mutant/throttle') |
5 | var nest = require('depnest') |
6 | |
7 | exports.needs = nest({ |
8 | 'sbot.pull.userFeed': 'first' |
9 | }) |
10 | |
11 | exports.gives = nest({ |
12 | 'profile.obs': ['following'] |
13 | }) |
14 | |
15 | exports.create = function (api) { |
16 | var cache = {} |
17 | |
18 | return nest({ |
19 | 'profile.obs': {following} |
20 | }) |
21 | |
22 | function following (userId) { |
23 | if (cache[userId]) { |
24 | return cache[userId] |
25 | } else { |
26 | var stream = pull( |
27 | api.sbot.pull.userFeed({id: userId, live: true}), |
28 | pull.filter((msg) => { |
29 | return !msg.value || msg.value.content.type === 'contact' |
30 | }) |
31 | ) |
32 | |
33 | var result = MutantPullReduce(stream, (result, msg) => { |
34 | var c = msg.value.content |
35 | if (c.contact) { |
36 | if (typeof c.following === 'boolean') { |
37 | if (c.following) { |
38 | result.add(c.contact) |
39 | } else { |
40 | result.delete(c.contact) |
41 | } |
42 | } |
43 | } |
44 | return result |
45 | }, { |
46 | startValue: new Set(), |
47 | nextTick: true |
48 | }) |
49 | |
50 | var instance = throttle(result, 2000) |
51 | instance.sync = result.sync |
52 | |
53 | instance.has = function (value) { |
54 | return computed(instance, x => x.has(value)) |
55 | } |
56 | |
57 | cache[userId] = instance |
58 | return instance |
59 | } |
60 | } |
61 | } |
62 |
Built with git-ssb-web