From 5119a5de3b2bba836f2847ca18227c14baf2b629 Mon Sep 17 00:00:00 2001 From: cel Date: Fri, 13 Oct 2017 13:18:56 -1000 Subject: Filter messages socially --- lib/app.js | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) (limited to 'lib/app.js') diff --git a/lib/app.js b/lib/app.js index 5cc4e62..3db24b8 100644 --- a/lib/app.js +++ b/lib/app.js @@ -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) + }) + ) +} -- cgit v1.2.3