git ssb

9+

cel / ssb-viewer



Tree: 445c5735109436be4d2b29ffafa9437cd6c2475f

Files: 445c5735109436be4d2b29ffafa9437cd6c2475f / lib / about.js

1839 bytesRaw
1var pull = require('pull-stream')
2var sort = require('ssb-sort')
3
4function asString(val) {
5 return typeof val === 'string' && val
6}
7
8function linkDest(val) {
9 return val ? asString(val) || asString(val.link) : null
10}
11
12module.exports = function (sbot, id, cb) {
13 var aboutByFeed = {}
14 pull(
15 sbot.links({
16 rel: 'about',
17 source: id,
18 dest: id,
19 values: true,
20 }),
21 pull.drain(function (msg) {
22 var author = msg.value.author
23 var c = msg.value.content
24 if (!c) return
25 var feedAbout = aboutByFeed[author] || (aboutByFeed[author] = {})
26 if (c.name) feedAbout.name = c.name.replace(/^@?/, '@')
27 if (c.image) feedAbout.image = linkDest(c.image)
28 if (c.description) feedAbout.description = c.description
29 }, function (err) {
30 if (err) return cb(err)
31 // Use whatever properties have the most counts.
32 // Usually we would want to handle renames for dead feeds and such,
33 // but for ssb-viewer it is mostly public/archival content anyway,
34 // so we'll let the popular name stand.
35 var propValueCounts = {/* prop: {value: count} */}
36 var topValues = {/* prop: value */}
37 var topValueCounts = {/* prop: count */}
38 var about = {}
39 for (var feed in aboutByFeed) {
40 var feedAbout = aboutByFeed[feed]
41 for (var prop in feedAbout) {
42 var value = feedAbout[prop]
43 var valueCounts = propValueCounts[prop] || (propValueCounts[prop] = {})
44 var count = (valueCounts[value] || 0) + 1
45 valueCounts[value] = count
46 if (count > (topValueCounts[prop] || 0)) {
47 topValueCounts[prop] = count
48 topValues[prop] = value
49 }
50 }
51 }
52 if (!topValues.name) topValues.name = String(id).substr(0, 10) + '…'
53 cb(null, topValues)
54 })
55 )
56}
57

Built with git-ssb-web