Files: d87029da1cdaa2cd2211d1e5ef45962db26afcb9 / lib / about.js
1758 bytesRaw
1 | var pull = require('pull-stream') |
2 | var sort = require('ssb-sort') |
3 | |
4 | function asString(val) { |
5 | return typeof val === 'string' && val |
6 | } |
7 | |
8 | function linkDest(val) { |
9 | return val ? asString(val) || asString(val.link) : null |
10 | } |
11 | |
12 | module.exports = function (sbot, id, cb) { |
13 | var aboutByFeed = {} |
14 | pull( |
15 | sbot.links({ |
16 | rel: 'about', |
17 | dest: id, |
18 | values: true, |
19 | }), |
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) { |
28 | if (err) return cb(err) |
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) |
52 | }) |
53 | ) |
54 | } |
55 |
Built with git-ssb-web