diff options
-rw-r--r-- | lib/app.js | 36 | ||||
-rw-r--r-- | lib/follows.js | 43 |
2 files changed, 47 insertions, 32 deletions
@@ -8,6 +8,7 @@ var multicb = require('multicb') var paramap = require('pull-paramap') var Contacts = require('ssb-contact') var About = require('./about') +var Follows = require('./follows') var Serve = require('./serve') var Render = require('./render') var Git = require('./git') @@ -49,7 +50,6 @@ function App(sbot, config) { this.reverseEmojiNameCache = lru(500) this.getBlobSize = memo({cache: this.blobSizeCache = lru(100)}, sbot.blobs.size.bind(sbot.blobs)) - this.getFollows = memo(this._getFollows.bind(this)) this.getVotes = memo({cache: lru(100)}, this._getVotes.bind(this)) this.unboxMsg = this.unboxMsg.bind(this) @@ -57,6 +57,7 @@ function App(sbot, config) { this.render = new Render(this, this.opts) this.git = new Git(this) this.contacts = new Contacts(this.sbot) + this.follows = new Follows(this.sbot, this.contacts) this.monitorBlobWants() } @@ -690,7 +691,7 @@ App.prototype.filterMsg = function (msg, opts, cb) { || author === myId || author === opts.feed || msg.key === opts.msgId) return cb(null, show) - self.getFollows(myId, function (err, follows) { + self.follows.getFollows(myId, function (err, follows) { if (err) return cb(err) if (follows[author]) return cb(null, show) self.getVotes(msg.key, function (err, votes) { @@ -707,41 +708,12 @@ App.prototype.filterMsg = function (msg, opts, cb) { App.prototype.isFollowing = function (src, dest, cb) { var self = this - self.getFollows(src, function (err, follows) { + self.follows.getFollows(src, function (err, follows) { if (err) return cb(err) return cb(null, follows[dest]) }) } -App.prototype._getFollows = function (id, cb) { - var follows = {} - function ready(err) { - if (!cb) return - var _cb = cb - cb = null - _cb(err, follows) - } - pull( - this.sbot.links2.read({ - live: true, - query: [ - {$filter: { - source: id, - rel: [{$prefix: 'contact'}] - }}, - {$map: { - following: ['rel', 1], - feed: 'dest' - }} - ] - }), - pull.drain(function (link) { - if (link.sync) return ready() - follows[link.feed] = link.following - }, ready) - ) -} - App.prototype._getVotes = function (id, cb) { var votes = {} pull( diff --git a/lib/follows.js b/lib/follows.js new file mode 100644 index 0000000..b262520 --- /dev/null +++ b/lib/follows.js @@ -0,0 +1,43 @@ +var pull = require('pull-stream') +var memo = require('asyncmemo') +var lru = require('hashlru') + +module.exports = Follows + +function Follows(sbot, contacts) { + if (!(this instanceof Follows)) return new Follows(sbot) + + this.sbot = sbot + this.contacts = contacts + var followsCache = lru(100) + this.getFollows = memo({cache: followsCache}, this.getFollows) + + pull( + sbot.messagesByType({type: 'contact', old: false}), + pull.drain(function (msg) { + var author = msg && msg.value && msg.value.author + var c = msg && msg.value && msg.value.content + var follows = author && followsCache.get(author) + if (follows && c && c.contact) follows[c.contact] = c.following + }, function (err) { + if (err) console.trace(err) + }) + ) +} + +Follows.prototype.getFollows = function (id, cb) { + var follows = {} + pull( + this.contacts.createFollowsStream(id), + pull.drain(function (feed) { + follows[feed] = true + }, function (err) { + if (err) return cb(err) + cb(null, follows) + }) + ) +} + +Follows.prototype.close = function (cb) { + this.sbot.close(cb) +} |