From 6f41c9179b21ade843d0e446342cef6b241c5615 Mon Sep 17 00:00:00 2001 From: cel Date: Sun, 18 Nov 2018 18:42:45 -1000 Subject: Add ssb-links fallback for votes calculation --- lib/app.js | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 55 insertions(+), 12 deletions(-) (limited to 'lib') diff --git a/lib/app.js b/lib/app.js index fa26b25..d895fa7 100644 --- a/lib/app.js +++ b/lib/app.js @@ -803,21 +803,64 @@ App.prototype.isFollowing = function (src, dest, cb) { }) } +App.prototype.getVotesStream = function (id) { + var links2 = this.sbot.links2 + if (links2 && links2.read) return links2.read({ + query: [ + {$filter: { + dest: id, + rel: [{$prefix: 'vote'}] + }}, + {$map: { + value: ['rel', 1], + author: 'source' + }} + ] + }) + + var backlinks = this.sbot.backlinks + if (backlinks && backlinks.read) return backlinks.read({ + query: [ + {$filter: { + dest: id, + value: { + content: { + type: 'vote', + vote: { + link: id + } + } + } + }}, + {$map: { + author: ['value', 'author'], + value: ['value', 'content', 'vote', 'value'] + }} + ] + }) + + return pull( + this.sbot.links({ + dest: id, + rel: 'vote', + keys: false, + meta: false, + values: true + }), + pull.map(function (value) { + var vote = value && value.content && value.content.vote + return { + author: value && value.author, + vote: vote && vote.value + } + }) + ) +} + 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' - }} - ] - }), + this.getVotesStream(), pull.drain(function (vote) { votes[vote.author] = vote.value }, function (err) { -- cgit v1.2.3