aboutsummaryrefslogtreecommitdiff
path: root/lib/app.js
diff options
context:
space:
mode:
authorcel <cel@f/6sQ6d2CMxRUhLpspgGIulDxDCwYD7DzFzPNr7u5AU=.ed25519>2017-06-06 23:25:30 -1000
committercel <cel@f/6sQ6d2CMxRUhLpspgGIulDxDCwYD7DzFzPNr7u5AU=.ed25519>2017-06-06 23:25:30 -1000
commita3dca9e424477c2745c59f482342a1580ed807f0 (patch)
tree14712ccb963a037ae9ceec1f6e9a8f4246d58592 /lib/app.js
parente9b1ee71a12fcd6c15a2f0b06671bc819da98303 (diff)
parent0e9903642e2e2c9cfb6f89f80a0256fa6f53ef5c (diff)
downloadpatchfoo-a3dca9e424477c2745c59f482342a1580ed807f0.tar.gz
patchfoo-a3dca9e424477c2745c59f482342a1580ed807f0.zip
Merge branch 'votes'
Diffstat (limited to 'lib/app.js')
-rw-r--r--lib/app.js60
1 files changed, 60 insertions, 0 deletions
diff --git a/lib/app.js b/lib/app.js
index b9e1924..d1f76f3 100644
--- a/lib/app.js
+++ b/lib/app.js
@@ -389,6 +389,66 @@ App.prototype.createContactStreams = function (id) {
return new Contacts(this.sbot).createContactStreams(id)
}
+function compareVoted(a, b) {
+ return b.value - a.value
+}
+
+App.prototype.getVoted = function (_opts, cb) {
+ if (isNaN(_opts.limit)) return pull.error(new Error('missing limit'))
+ var self = this
+ var opts = {
+ type: 'vote',
+ limit: _opts.limit * 100,
+ reverse: !!_opts.reverse,
+ gt: _opts.gt || undefined,
+ lt: _opts.lt || undefined,
+ }
+
+ var votedObj = {}
+ var votedArray = []
+ var numItems = 0
+ var firstTimestamp, lastTimestamp
+ pull(
+ self.sbot.messagesByType(opts),
+ self.unboxMessages(),
+ pull.take(function () {
+ return numItems < _opts.limit
+ }),
+ pull.drain(function (msg) {
+ if (!firstTimestamp) firstTimestamp = msg.timestamp
+ lastTimestamp = msg.timestamp
+ var vote = msg.value.content.vote
+ if (!vote) return
+ var target = u.linkDest(vote)
+ var votes = votedObj[target]
+ if (!votes) {
+ numItems++
+ votes = {id: target, value: 0, feedsObj: {}, feeds: []}
+ votedObj[target] = votes
+ votedArray.push(votes)
+ }
+ if (msg.value.author in votes.feedsObj) {
+ if (!opts.reverse) return // leave latest vote value as-is
+ // remove old vote value
+ votes.value -= votes.feedsObj[msg.value.author]
+ } else {
+ votes.feeds.push(msg.value.author)
+ }
+ var value = vote.value > 0 ? 1 : vote.value < 0 ? -1 : 0
+ votes.feedsObj[msg.value.author] = value
+ votes.value += value
+ }, function (err) {
+ if (err) return cb(err)
+ var items = votedArray
+ if (opts.reverse) items.reverse()
+ items.sort(compareVoted)
+ cb(null, {items: items,
+ firstTimestamp: firstTimestamp,
+ lastTimestamp: lastTimestamp})
+ })
+ )
+}
+
App.prototype.createAboutStreams = function (id) {
return this.about.createAboutStreams(id)
}