git ssb

9+

cel / ssb-viewer



Tree: afabd2eacb34221e1b60ed77cce59a621e8c8022

Files: afabd2eacb34221e1b60ed77cce59a621e8c8022 / lib / about.js

2294 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 (typeof c.name == 'string') 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.title) feedAbout.title = c.title
33 if (c.startDateTime) feedAbout.startDateTime = c.startDateTime
34 if (c.publicWebHosting !== undefined && author === c.about) feedAbout.publicWebHosting = defalsify(c.publicWebHosting)
35 }, function (err) {
36 if (err) return cb(err)
37 // Use whatever properties have the most counts.
38 // Usually we would want to handle renames for dead feeds and such,
39 // but for ssb-viewer it is mostly public/archival content anyway,
40 // so we'll let the popular name stand.
41 // Except: prefer the feed's own choices slightly
42 var propValueCounts = {/* prop: {value: count} */}
43 var topValues = {/* prop: value */}
44 var topValueCounts = {/* prop: count */}
45 var about = {}
46 // bias the feed's own choice by 1
47 aboutByFeed._author = aboutByFeed[id] || {}
48 for (var feed in aboutByFeed) {
49 var feedAbout = aboutByFeed[feed]
50 for (var prop in feedAbout) {
51 var value = feedAbout[prop]
52 var valueCounts = propValueCounts[prop] || (propValueCounts[prop] = {})
53 var count = (valueCounts[value] || 0) + 1
54 valueCounts[value] = count
55 if (count > (topValueCounts[prop] || 0)) {
56 topValueCounts[prop] = count
57 topValues[prop] = value
58 }
59 }
60 }
61 if (!topValues.name) topValues.name = String(id).substr(0, 10) + '…'
62 cb(null, topValues)
63 })
64 )
65}
66

Built with git-ssb-web