diff options
author | cel <cel@f/6sQ6d2CMxRUhLpspgGIulDxDCwYD7DzFzPNr7u5AU=.ed25519> | 2017-05-15 13:08:31 -1000 |
---|---|---|
committer | cel <cel@f/6sQ6d2CMxRUhLpspgGIulDxDCwYD7DzFzPNr7u5AU=.ed25519> | 2017-05-15 13:08:31 -1000 |
commit | e0f8badf71a127ef2f66025af6c464abe512e8a4 (patch) | |
tree | 577de8c1881f348ebd7ed2a62aea6f4de7d6cb28 /lib/about.js | |
parent | 58b40d18c7d49ef071ece70af5e6d9e1d12301e2 (diff) | |
download | patchfoo-e0f8badf71a127ef2f66025af6c464abe512e8a4.tar.gz patchfoo-e0f8badf71a127ef2f66025af6c464abe512e8a4.zip |
Use own About implementation
Use most popular about info instead of most recent.
Slightly prefer a feed's own about info for itself.
Diffstat (limited to 'lib/about.js')
-rw-r--r-- | lib/about.js | 67 |
1 files changed, 61 insertions, 6 deletions
diff --git a/lib/about.js b/lib/about.js index 4f1d582..fde5e04 100644 --- a/lib/about.js +++ b/lib/about.js @@ -1,19 +1,18 @@ var pull = require('pull-stream') -// var defer = require('pull-defer') -// var many = require('pull-many') var multicb = require('multicb') +var cat = require('pull-cat') var u = require('./util') module.exports = About -function About(sbot) { - if (!(this instanceof About)) return new About(sbot) - this.sbot = sbot +function About(app, myId) { + this.app = app + this.myId = myId } About.prototype.createAboutOpStream = function (id) { return pull( - this.sbot.links({dest: id, rel: 'about', values: true, reverse: true}), + this.app.sbot.links({dest: id, rel: 'about', values: true, reverse: true}), pull.map(function (msg) { var c = msg.value.content || {} return Object.keys(c).filter(function (key) { @@ -57,3 +56,59 @@ About.prototype.createAboutStreams = function (id) { sets: u.readNext(setsDone) } } + +function computeTopAbout(aboutByFeed) { + var propValueCounts = {/* prop: {value: count} */} + var topValues = {/* prop: value */} + var topValueCounts = {/* prop: count */} + for (var feed in aboutByFeed) { + var feedAbout = aboutByFeed[feed] + for (var prop in feedAbout) { + var value = feedAbout[prop] + var valueCounts = propValueCounts[prop] || (propValueCounts[prop] = {}) + var count = (valueCounts[value] || 0) + 1 + valueCounts[value] = count + if (count > (topValueCounts[prop] || 0)) { + topValueCounts[prop] = count + topValues[prop] = value + } + } + } + return topValues +} + +About.prototype.get = function (dest, cb) { + var self = this + var aboutByFeed = {} + pull( + cat([ + dest[0] === '%' && self.app.pullGetMsg(dest), + self.app.sbot.links({ + rel: 'about', + dest: dest, + values: true, + }) + ]), + self.app.unboxMessages(), + pull.drain(function (msg) { + var author = msg.value.author + var c = msg.value.content + if (!c) return + var about = aboutByFeed[author] || (aboutByFeed[author] = {}) + if (c.name) about.name = c.name + if (c.image) about.image = u.linkDest(c.image) + if (c.description) about.description = c.description + }, function (err) { + if (err) return cb(err) + // bias the author's choices by giving them an extra vote + aboutByFeed._author = aboutByFeed[dest] + var about = {} + var myAbout = aboutByFeed[this.myId] || {} + var topAbout = computeTopAbout(aboutByFeed) + for (var k in topAbout) about[k] = topAbout[k] + // always prefer own choices + for (var k in myAbout) about[k] = myAbout[k] + cb(null, about) + }) + ) +} |