git ssb

9+

cel / ssb-viewer



Tree: 86801890069c72884af76b73462e38b8b85697db

Files: 86801890069c72884af76b73462e38b8b85697db / lib / about.js

2155 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
12function defalsify(val) {
13 return val === 'false' ? false : val
14}
15
16module.exports = function (sbot, id, cb) {
17 var aboutByFeed = {}
18 pull(
19 sbot.links({
20 rel: 'about',
21 dest: id,
22 values: true,
23 }),
24 pull.drain(function (msg) {
25 var author = msg.value.author
26 var c = msg.value.content
27 if (!c) return
28 var feedAbout = aboutByFeed[author] || (aboutByFeed[author] = {})
29 if (c.name) feedAbout.name = c.name.replace(/^@?/, '@')
30 if (c.image) feedAbout.image = linkDest(c.image)
31 if (c.description) feedAbout.description = c.description
32 if (c.publicWebHosting != null && author === c.about) feedAbout.publicWebHosting = defalsify(c.publicWebHosting)
33 }, function (err) {
34 if (err) return cb(err)
35 // Use whatever properties have the most counts.
36 // Usually we would want to handle renames for dead feeds and such,
37 // but for ssb-viewer it is mostly public/archival content anyway,
38 // so we'll let the popular name stand.
39 // Except: prefer the feed's own choices slightly
40 var propValueCounts = {/* prop: {value: count} */}
41 var topValues = {/* prop: value */}
42 var topValueCounts = {/* prop: count */}
43 var about = {}
44 // bias the feed's own choice by 1
45 aboutByFeed._author = aboutByFeed[id] || {}
46 for (var feed in aboutByFeed) {
47 var feedAbout = aboutByFeed[feed]
48 for (var prop in feedAbout) {
49 var value = feedAbout[prop]
50 var valueCounts = propValueCounts[prop] || (propValueCounts[prop] = {})
51 var count = (valueCounts[value] || 0) + 1
52 valueCounts[value] = count
53 if (count > (topValueCounts[prop] || 0)) {
54 topValueCounts[prop] = count
55 topValues[prop] = value
56 }
57 }
58 }
59 if (!topValues.name) topValues.name = String(id).substr(0, 10) + '…'
60 cb(null, topValues)
61 })
62 )
63}
64

Built with git-ssb-web