git ssb

9+

cel / ssb-viewer



Commit d87029da1cdaa2cd2211d1e5ef45962db26afcb9

Use most popular name/image

cel committed on 5/12/2017, 11:29:46 PM
Parent: 4cfbba635d8d159f21cb7f2ceb4074d6142da7c5

Files changed

lib/about.jschanged
lib/about.jsView
@@ -1,31 +1,54 @@
11 var pull = require('pull-stream')
22 var sort = require('ssb-sort')
33
4-function linkDest(val) {
5- return typeof val === 'string' ? val : val && val.link
4 +function asString(val) {
5 + return typeof val === 'string' && val
66 }
77
8-function reduceAbout(about, msg) {
9- var c = msg.value.content
10- if (!c) return about
11- if (c.name) about.name = c.name.replace(/^@?/, '@')
12- if (c.image) about.image = linkDest(c.image)
13- return about
8 +function linkDest(val) {
9 + return val ? asString(val) || asString(val.link) : null
1410 }
1511
1612 module.exports = function (sbot, id, cb) {
17- var about = {}
13 + var aboutByFeed = {}
1814 pull(
1915 sbot.links({
2016 rel: 'about',
2117 dest: id,
2218 values: true,
2319 }),
24- pull.collect(function (err, msgs) {
20 + pull.drain(function (msg) {
21 + var author = msg.value.author
22 + var c = msg.value.content
23 + if (!c) return
24 + var feedAbout = aboutByFeed[author] || (aboutByFeed[author] = {})
25 + if (c.name) feedAbout.name = c.name.replace(/^@?/, '@')
26 + if (c.image) feedAbout.image = linkDest(c.image)
27 + }, function (err) {
2528 if (err) return cb(err)
26- cb(null, sort(msgs).reduce(reduceAbout, {
27- name: String(id).substr(0, 10) + '…',
28- }))
29 + // Use whatever properties have the most counts.
30 + // Usually we would want to handle renames for dead feeds and such,
31 + // but for ssb-viewer it is mostly public/archival content anyway,
32 + // so we'll let the popular name stand.
33 + var propValueCounts = {/* prop: {value: count} */}
34 + var topValues = {/* prop: value */}
35 + var topValueCounts = {/* prop: count */}
36 + var about = {}
37 + for (var feed in aboutByFeed) {
38 + var feedAbout = aboutByFeed[feed]
39 + for (var prop in feedAbout) {
40 + var value = feedAbout[prop]
41 + var valueCounts = propValueCounts[prop] || (propValueCounts[prop] = {})
42 + var count = (valueCounts[value] || 0) + 1
43 + valueCounts[value] = count
44 + if (count > (topValueCounts[prop] || 0)) {
45 + topValueCounts[prop] = count
46 + topValues[prop] = value
47 + }
48 + }
49 + }
50 + if (!topValues.name) topValues.name = String(id).substr(0, 10) + '…'
51 + cb(null, topValues)
2952 })
3053 )
3154 }

Built with git-ssb-web