aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/about.js67
-rw-r--r--lib/app.js17
-rw-r--r--package.json1
3 files changed, 72 insertions, 13 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)
+ })
+ )
+}
diff --git a/lib/app.js b/lib/app.js
index dfec6bd..eee6c95 100644
--- a/lib/app.js
+++ b/lib/app.js
@@ -4,7 +4,6 @@ var lru = require('hashlru')
var pkg = require('../package')
var u = require('./util')
var pull = require('pull-stream')
-var ssbAvatar = require('ssb-avatar')
var hasher = require('pull-hash/ext/ssb')
var multicb = require('multicb')
var paramap = require('pull-paramap')
@@ -32,9 +31,10 @@ function App(sbot, config) {
}
sbot.get = memo({cache: lru(100)}, sbot.get)
+ this.about = new About(this, sbot.id)
this.getMsg = memo({cache: lru(100)}, getMsgWithValue, sbot)
this.getAbout = memo({cache: this.aboutCache = lru(500)},
- getAbout.bind(this), sbot, sbot.id)
+ this._getAbout.bind(this))
this.unboxContent = memo({cache: lru(100)}, sbot.private.unbox)
this.reverseNameCache = lru(100)
@@ -199,11 +199,12 @@ function getMsgWithValue(sbot, id, cb) {
})
}
-function getAbout(sbot, src, id, cb) {
+App.prototype._getAbout = function (id, cb) {
var self = this
- ssbAvatar(sbot, src, id, function (err, about) {
+ if (!u.isRef(id)) return cb(null, {})
+ self.about.get(id, function (err, about) {
if (err) return cb(err)
- var sigil = id && id[0] || '@'
+ var sigil = id[0] || '@'
if (about.name && about.name[0] !== sigil) {
about.name = sigil + about.name
}
@@ -212,6 +213,10 @@ function getAbout(sbot, src, id, cb) {
})
}
+App.prototype.pullGetMsg = function (id) {
+ return pull.asyncMap(this.getMsg)(pull.once(id))
+}
+
App.prototype.createLogStream = function (opts) {
opts = opts || {}
return opts.sortByTimestamp
@@ -321,5 +326,5 @@ App.prototype.createContactStreams = function (id) {
}
App.prototype.createAboutStreams = function (id) {
- return new About(this.sbot).createAboutStreams(id)
+ return this.about.createAboutStreams(id)
}
diff --git a/package.json b/package.json
index 1bc5812..7b77ca5 100644
--- a/package.json
+++ b/package.json
@@ -19,7 +19,6 @@
"pull-paginate": "^1.0.0",
"pull-paramap": "^1.2.1",
"pull-stream": "^3.5.0",
- "ssb-avatar": "^0.2.0",
"ssb-contact": "^1.0.0",
"ssb-marked": "^0.7.1",
"ssb-mentions": "^0.2.0",