git ssb

9+

cel / ssb-viewer



Commit 44f0d0a8c4e7d354eb6730844dbfab9c48de65de

Use ssb-about

Anders Rune Jensen committed on 1/16/2018, 12:54:39 AM
Parent: 8c84e9ec8dfd1770ec8755f993ae9deea4aab61a

Files changed

index.jschanged
lib/about.jschanged
index.jsView
@@ -38,10 +38,23 @@
3838
3939 exports.init = function (sbot, config) {
4040 var conf = config.viewer || {}
4141
42 + var about = require('./lib/about')
43 +
44 + var getAbout = memo({cache: lru(100)}, about, sbot)
45 +
46 + var lastRefresh = Date.now()
47 +
48 + function refreshAbout() {
49 + if (about.allAbouts && (lastRefresh - Date.now()) / 1000 < 30 * 60) return
50 +
51 + sbot.about.get((err, allAbouts) => {
52 + about.allAbouts = allAbouts
53 + })
54 + }
55 +
4256 var getMsg = memo({cache: lru(100)}, getMsgWithValue, sbot)
43- var getAbout = memo({cache: lru(100)}, require('./lib/about'), sbot)
4457
4558 var base = conf.base || '/'
4659 var defaultOpts = {
4760 base: base,
@@ -65,8 +78,10 @@
6578 renderer: new MdRenderer(defaultOpts)
6679 }
6780
6881 exports.serveFeed = function(req, res, feedId, ext) {
82 + refreshAbout()
83 +
6984 console.log("serving feed: " + feedId)
7085
7186 var showAll = req.url.endsWith("?showAll");
7287
@@ -112,8 +127,10 @@
112127 })
113128 }
114129
115130 exports.serveUserFeed = function(req, res, feedId) {
131 + refreshAbout()
132 +
116133 console.log("serving user feed: " + feedId)
117134
118135 var following = []
119136 var channelSubscriptions = []
@@ -187,8 +204,10 @@
187204 )
188205 }
189206
190207 exports.serveChannel = function(req, res, channelId) {
208 + refreshAbout()
209 +
191210 console.log("serving channel: " + channelId)
192211
193212 var showAll = req.url.endsWith("?showAll")
194213
@@ -214,8 +233,10 @@
214233 )
215234 }
216235
217236 exports.serveId = function(req, res, id, ext, query) {
237 + refreshAbout()
238 +
218239 var q = query ? qs.parse(query) : {}
219240 var includeRoot = !('noroot' in q)
220241 var base = q.base || conf.base
221242 var baseToken
lib/about.jsView
@@ -1,7 +1,4 @@
1-var pull = require('pull-stream')
2-var sort = require('ssb-sort')
3-
41 function asString(val) {
52 return typeof val === 'string' && val
63 }
74
@@ -14,50 +11,49 @@
1411 }
1512
1613 module.exports = function (sbot, id, cb) {
1714 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
15 +
16 + let values = module.exports.allAbouts[id]
17 +
18 + for (var key in values) {
19 + for (var author in values[key]) {
2820 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- }
21 +
22 + var v = values[key][author][0]
23 +
24 + if (!v) break;
25 +
26 + if (key == 'name') feedAbout.name = v.replace(/^@?/, '@')
27 + if (key == 'image') feedAbout.image = linkDest(v)
28 + if (key == 'description') feedAbout.description = v
29 + if (key == 'publicWebHosting' && author === id) feedAbout.publicWebHosting = defalsify(v)
30 + }
31 + }
32 +
33 + // Use whatever properties have the most counts.
34 + // Usually we would want to handle renames for dead feeds and such,
35 + // but for ssb-viewer it is mostly public/archival content anyway,
36 + // so we'll let the popular name stand.
37 + // Except: prefer the feed's own choices slightly
38 + var propValueCounts = {/* prop: {value: count} */}
39 + var topValues = {/* prop: value */}
40 + var topValueCounts = {/* prop: count */}
41 + var about = {}
42 + // bias the feed's own choice by 1
43 + aboutByFeed._author = aboutByFeed[id] || {}
44 + for (var feed in aboutByFeed) {
45 + var feedAbout = aboutByFeed[feed]
46 + for (var prop in feedAbout) {
47 + var value = feedAbout[prop]
48 + var valueCounts = propValueCounts[prop] || (propValueCounts[prop] = {})
49 + var count = (valueCounts[value] || 0) + 1
50 + valueCounts[value] = count
51 + if (count > (topValueCounts[prop] || 0)) {
52 + topValueCounts[prop] = count
53 + topValues[prop] = value
5854 }
59- if (!topValues.name) topValues.name = String(id).substr(0, 10) + '…'
60- cb(null, topValues)
61- })
62- )
55 + }
56 + }
57 + if (!topValues.name) topValues.name = String(id).substr(0, 10) + '…'
58 + cb(null, topValues)
6359 }

Built with git-ssb-web