Files: ea522127885486018de1547de0db04f3d3144eb6 / lib / follows.js
1172 bytesRaw
1 | var pull = require('pull-stream') |
2 | var memo = require('asyncmemo') |
3 | var lru = require('hashlru') |
4 | |
5 | module.exports = Follows |
6 | |
7 | function Follows(sbot, contacts) { |
8 | if (!(this instanceof Follows)) return new Follows(sbot) |
9 | |
10 | this.sbot = sbot |
11 | this.contacts = contacts |
12 | var followsCache = lru(100) |
13 | this.getFollows = memo({cache: followsCache}, this.getFollows) |
14 | |
15 | if (!sbot.messagesByType) console.error('missing messagesByType') |
16 | else pull( |
17 | sbot.messagesByType({type: 'contact', old: false}), |
18 | pull.drain(function (msg) { |
19 | var author = msg && msg.value && msg.value.author |
20 | var c = msg && msg.value && msg.value.content |
21 | var follows = author && followsCache.get(author) |
22 | if (follows && c && c.contact) follows[c.contact] = c.following |
23 | }, function (err) { |
24 | if (err) console.trace(err) |
25 | }) |
26 | ) |
27 | } |
28 | |
29 | Follows.prototype.getFollows = function (id, cb) { |
30 | var follows = {} |
31 | pull( |
32 | this.contacts.createFollowsStream(id), |
33 | pull.drain(function (feed) { |
34 | follows[feed] = true |
35 | }, function (err) { |
36 | if (err) return cb(err) |
37 | cb(null, follows) |
38 | }) |
39 | ) |
40 | } |
41 | |
42 | Follows.prototype.close = function (cb) { |
43 | this.sbot.close(cb) |
44 | } |
45 |
Built with git-ssb-web