Files: cf66c7398b3ea4b26de20ddc0cb1e983384b150a / lib / about.js
1968 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 | if (c.description) feedAbout.description = c.description |
28 | }, function (err) { |
29 | if (err) return cb(err) |
30 | // Use whatever properties have the most counts. |
31 | // Usually we would want to handle renames for dead feeds and such, |
32 | // but for ssb-viewer it is mostly public/archival content anyway, |
33 | // so we'll let the popular name stand. |
34 | // Except: prefer the feed's own choices slightly |
35 | var propValueCounts = {/* prop: {value: count} */} |
36 | var topValues = {/* prop: value */} |
37 | var topValueCounts = {/* prop: count */} |
38 | var about = {} |
39 | // bias the feed's own choice by 1 |
40 | aboutByFeed._author = aboutByFeed[id] || {} |
41 | for (var feed in aboutByFeed) { |
42 | var feedAbout = aboutByFeed[feed] |
43 | for (var prop in feedAbout) { |
44 | var value = feedAbout[prop] |
45 | var valueCounts = propValueCounts[prop] || (propValueCounts[prop] = {}) |
46 | var count = (valueCounts[value] || 0) + 1 |
47 | valueCounts[value] = count |
48 | if (count > (topValueCounts[prop] || 0)) { |
49 | topValueCounts[prop] = count |
50 | topValues[prop] = value |
51 | } |
52 | } |
53 | } |
54 | if (!topValues.name) topValues.name = String(id).substr(0, 10) + '…' |
55 | cb(null, topValues) |
56 | }) |
57 | ) |
58 | } |
59 |
Built with git-ssb-web