git ssb

10+

Matt McKegg / patchwork



Tree: a821b0ce884300e8ff3901678c19c6827e1189e9

Files: a821b0ce884300e8ff3901678c19c6827e1189e9 / modules / obs-following.js

1089 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_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