git ssb

9+

cel / ssb-viewer



Tree: ff87105bd1d53bd820af183f22245f91f965e9e2

Files: ff87105bd1d53bd820af183f22245f91f965e9e2 / lib / about.js

2067 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 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 if (c.publicWebHosting && author == c.about) feedAbout.publicWebHosting = c.publicWebHosting
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 // Except: prefer the feed's own choices slightly
36 var propValueCounts = {/* prop: {value: count} */}
37 var topValues = {/* prop: value */}
38 var topValueCounts = {/* prop: count */}
39 var about = {}
40 // bias the feed's own choice by 1
41 aboutByFeed._author = aboutByFeed[id] || {}
42 for (var feed in aboutByFeed) {
43 var feedAbout = aboutByFeed[feed]
44 for (var prop in feedAbout) {
45 var value = feedAbout[prop]
46 var valueCounts = propValueCounts[prop] || (propValueCounts[prop] = {})
47 var count = (valueCounts[value] || 0) + 1
48 valueCounts[value] = count
49 if (count > (topValueCounts[prop] || 0)) {
50 topValueCounts[prop] = count
51 topValues[prop] = value
52 }
53 }
54 }
55 if (!topValues.name) topValues.name = String(id).substr(0, 10) + '…'
56 cb(null, topValues)
57 })
58 )
59}
60

Built with git-ssb-web