Files: c329eae0e6ff3d36d7f21349980d51d9dd587faa / lib / follows.js
1099 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 | pull( |
16 | sbot.messagesByType({type: 'contact', old: false}), |
17 | pull.drain(function (msg) { |
18 | var author = msg && msg.value && msg.value.author |
19 | var c = msg && msg.value && msg.value.content |
20 | var follows = author && followsCache.get(author) |
21 | if (follows && c && c.contact) follows[c.contact] = c.following |
22 | }, function (err) { |
23 | if (err) console.trace(err) |
24 | }) |
25 | ) |
26 | } |
27 | |
28 | Follows.prototype.getFollows = function (id, cb) { |
29 | var follows = {} |
30 | pull( |
31 | this.contacts.createFollowsStream(id), |
32 | pull.drain(function (feed) { |
33 | follows[feed] = true |
34 | }, function (err) { |
35 | if (err) return cb(err) |
36 | cb(null, follows) |
37 | }) |
38 | ) |
39 | } |
40 | |
41 | Follows.prototype.close = function (cb) { |
42 | this.sbot.close(cb) |
43 | } |
44 |
Built with git-ssb-web