Commit d87029da1cdaa2cd2211d1e5ef45962db26afcb9
Use most popular name/image
cel committed on 5/12/2017, 11:29:46 PMParent: 4cfbba635d8d159f21cb7f2ceb4074d6142da7c5
Files changed
lib/about.js | changed |
lib/about.js | ||
---|---|---|
@@ -1,31 +1,54 @@ | ||
1 | 1 … | var pull = require('pull-stream') |
2 | 2 … | var sort = require('ssb-sort') |
3 | 3 … | |
4 | -function linkDest(val) { | |
5 | - return typeof val === 'string' ? val : val && val.link | |
4 … | +function asString(val) { | |
5 … | + return typeof val === 'string' && val | |
6 | 6 … | } |
7 | 7 … | |
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 | |
14 | 10 … | } |
15 | 11 … | |
16 | 12 … | module.exports = function (sbot, id, cb) { |
17 | - var about = {} | |
13 … | + var aboutByFeed = {} | |
18 | 14 … | pull( |
19 | 15 … | sbot.links({ |
20 | 16 … | rel: 'about', |
21 | 17 … | dest: id, |
22 | 18 … | values: true, |
23 | 19 … | }), |
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) { | |
25 | 28 … | 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) | |
29 | 52 … | }) |
30 | 53 … | ) |
31 | 54 … | } |
Built with git-ssb-web