From 7570f001cc8ddeff88ebdab640e06a0b1b03974c Mon Sep 17 00:00:00 2001 From: cel Date: Thu, 4 May 2017 11:03:16 -1000 Subject: wip: about --- lib/about.js | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 lib/about.js (limited to 'lib/about.js') diff --git a/lib/about.js b/lib/about.js new file mode 100644 index 0000000..4f1d582 --- /dev/null +++ b/lib/about.js @@ -0,0 +1,59 @@ +var pull = require('pull-stream') +// var defer = require('pull-defer') +// var many = require('pull-many') +var multicb = require('multicb') +var u = require('./util') + +module.exports = About + +function About(sbot) { + if (!(this instanceof About)) return new About(sbot) + this.sbot = sbot +} + +About.prototype.createAboutOpStream = function (id) { + return pull( + this.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) { + return key !== 'about' + && key !== 'type' + && key !== 'recps' + }).map(function (key) { + var value = c[key] + if (u.isRef(value)) value = {link: value} + return { + id: msg.key, + author: msg.value.author, + timestamp: msg.value.timestamp, + prop: key, + value: value, + remove: value && typeof value === 'object' && value.remove, + } + }) + }), + pull.flatten() + ) +} + +About.prototype.createAboutStreams = function (id) { + var ops = this.createAboutOpStream(id) + var scalars = {/* author: {prop: value} */} + var sets = {/* author: {prop: {link}} */} + + var setsDone = multicb({pluck: 1, spread: true}) + setsDone()(null, pull.values([])) + return { + scalars: pull( + ops, + pull.unique(function (op) { + return op.author + '-' + op.prop + '-' + (op.value ? op.value.link : '') + }), + pull.filter(function (op) { + return !op.remove + }) + ), + sets: u.readNext(setsDone) + } +} -- cgit v1.2.3 From e0f8badf71a127ef2f66025af6c464abe512e8a4 Mon Sep 17 00:00:00 2001 From: cel Date: Mon, 15 May 2017 13:08:31 -1000 Subject: Use own About implementation Use most popular about info instead of most recent. Slightly prefer a feed's own about info for itself. --- lib/about.js | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++------ lib/app.js | 17 +++++++++------ package.json | 1 - 3 files changed, 72 insertions(+), 13 deletions(-) (limited to 'lib/about.js') 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", -- cgit v1.2.3 From f8b516671c8a45fb1453fd20d42d692f8040d7a5 Mon Sep 17 00:00:00 2001 From: cel Date: Mon, 15 May 2017 13:42:17 -1000 Subject: Handle description better on the about page --- lib/about.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'lib/about.js') diff --git a/lib/about.js b/lib/about.js index fde5e04..1d2c0ea 100644 --- a/lib/about.js +++ b/lib/about.js @@ -20,8 +20,8 @@ About.prototype.createAboutOpStream = function (id) { && key !== 'type' && key !== 'recps' }).map(function (key) { - var value = c[key] - if (u.isRef(value)) value = {link: value} + var value = u.linkDest(c[key]) + // if (u.isRef(value)) value = {link: value} return { id: msg.key, author: msg.value.author, @@ -47,7 +47,7 @@ About.prototype.createAboutStreams = function (id) { scalars: pull( ops, pull.unique(function (op) { - return op.author + '-' + op.prop + '-' + (op.value ? op.value.link : '') + return op.author + '-' + op.prop + '-' }), pull.filter(function (op) { return !op.remove -- cgit v1.2.3 From 2febc8b4825c6d24f5e49340f57a6e1643cabc46 Mon Sep 17 00:00:00 2001 From: cel Date: Sat, 20 May 2017 13:29:54 -1000 Subject: Fix preferring user's about choices --- lib/about.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/about.js') diff --git a/lib/about.js b/lib/about.js index 1d2c0ea..e94b7fc 100644 --- a/lib/about.js +++ b/lib/about.js @@ -103,7 +103,7 @@ About.prototype.get = function (dest, cb) { // bias the author's choices by giving them an extra vote aboutByFeed._author = aboutByFeed[dest] var about = {} - var myAbout = aboutByFeed[this.myId] || {} + var myAbout = aboutByFeed[self.myId] || {} var topAbout = computeTopAbout(aboutByFeed) for (var k in topAbout) about[k] = topAbout[k] // always prefer own choices -- cgit v1.2.3