git ssb

16+

cel / patchfoo



Tree: d185716fb11842271c37ff499725b2b66ba2a5e3

Files: d185716fb11842271c37ff499725b2b66ba2a5e3 / lib / follows.js

1172 bytesRaw
1var pull = require('pull-stream')
2var memo = require('asyncmemo')
3var lru = require('hashlru')
4
5module.exports = Follows
6
7function 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
29Follows.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
42Follows.prototype.close = function (cb) {
43 this.sbot.close(cb)
44}
45

Built with git-ssb-web