diff options
Diffstat (limited to 'lib/app.js')
-rw-r--r-- | lib/app.js | 86 |
1 files changed, 86 insertions, 0 deletions
@@ -48,6 +48,8 @@ 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) @@ -671,3 +673,87 @@ App.prototype.getNpmReadme = function (tarballId, cb) { cb(null, text, true) }) } + +App.prototype.filterMsg = function (msg, opts, cb) { + var self = this + var myId = self.sbot.id + var author = msg.value && msg.value.author + var show = (opts.filter !== 'invert') + if (opts.filter === 'all' + || author === myId + || author === opts.feed + || msg.key === opts.msgId) return cb(null, show) + self.getFollows(myId, function (err, follows) { + if (err) return cb(err) + if (follows[author]) return cb(null, show) + self.getVotes(msg.key, function (err, votes) { + if (err) return cb(err) + for (var author in votes) { + if (follows[author] && votes[author] > 0) { + return cb(null, show) + } + } + return cb(null, !show) + }) + }) +} + +App.prototype.isFollowing = function (src, dest, cb) { + var self = this + self.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( + this.sbot.links2.read({ + query: [ + {$filter: { + dest: id, + rel: [{$prefix: 'vote'}] + }}, + {$map: { + value: ['rel', 1], + author: 'source' + }} + ] + }), + pull.drain(function (vote) { + votes[vote.author] = vote.value + }, function (err) { + cb(err, votes) + }) + ) +} |