Commit 44f0d0a8c4e7d354eb6730844dbfab9c48de65de
Use ssb-about
Anders Rune Jensen committed on 1/16/2018, 12:54:39 AMParent: 8c84e9ec8dfd1770ec8755f993ae9deea4aab61a
Files changed
index.js | changed |
lib/about.js | changed |
index.js | ||
---|---|---|
@@ -38,10 +38,23 @@ | ||
38 | 38 … | |
39 | 39 … | exports.init = function (sbot, config) { |
40 | 40 … | var conf = config.viewer || {} |
41 | 41 … | |
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 … | + | |
42 | 56 … | var getMsg = memo({cache: lru(100)}, getMsgWithValue, sbot) |
43 | - var getAbout = memo({cache: lru(100)}, require('./lib/about'), sbot) | |
44 | 57 … | |
45 | 58 … | var base = conf.base || '/' |
46 | 59 … | var defaultOpts = { |
47 | 60 … | base: base, |
@@ -65,8 +78,10 @@ | ||
65 | 78 … | renderer: new MdRenderer(defaultOpts) |
66 | 79 … | } |
67 | 80 … | |
68 | 81 … | exports.serveFeed = function(req, res, feedId, ext) { |
82 … | + refreshAbout() | |
83 … | + | |
69 | 84 … | console.log("serving feed: " + feedId) |
70 | 85 … | |
71 | 86 … | var showAll = req.url.endsWith("?showAll"); |
72 | 87 … | |
@@ -112,8 +127,10 @@ | ||
112 | 127 … | }) |
113 | 128 … | } |
114 | 129 … | |
115 | 130 … | exports.serveUserFeed = function(req, res, feedId) { |
131 … | + refreshAbout() | |
132 … | + | |
116 | 133 … | console.log("serving user feed: " + feedId) |
117 | 134 … | |
118 | 135 … | var following = [] |
119 | 136 … | var channelSubscriptions = [] |
@@ -187,8 +204,10 @@ | ||
187 | 204 … | ) |
188 | 205 … | } |
189 | 206 … | |
190 | 207 … | exports.serveChannel = function(req, res, channelId) { |
208 … | + refreshAbout() | |
209 … | + | |
191 | 210 … | console.log("serving channel: " + channelId) |
192 | 211 … | |
193 | 212 … | var showAll = req.url.endsWith("?showAll") |
194 | 213 … | |
@@ -214,8 +233,10 @@ | ||
214 | 233 … | ) |
215 | 234 … | } |
216 | 235 … | |
217 | 236 … | exports.serveId = function(req, res, id, ext, query) { |
237 … | + refreshAbout() | |
238 … | + | |
218 | 239 … | var q = query ? qs.parse(query) : {} |
219 | 240 … | var includeRoot = !('noroot' in q) |
220 | 241 … | var base = q.base || conf.base |
221 | 242 … | var baseToken |
lib/about.js | |||
---|---|---|---|
@@ -1,7 +1,4 @@ | |||
1 | -var pull = require('pull-stream') | ||
2 | -var sort = require('ssb-sort') | ||
3 | - | ||
4 | 1 … | function asString(val) { | |
5 | 2 … | return typeof val === 'string' && val | |
6 | 3 … | } | |
7 | 4 … | ||
@@ -14,50 +11,49 @@ | |||
14 | 11 … | } | |
15 | 12 … | ||
16 | 13 … | module.exports = function (sbot, id, cb) { | |
17 | 14 … | 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]) { | ||
28 | 20 … | 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 | ||
58 | 54 … | } | |
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) | ||
63 | 59 … | } |
Built with git-ssb-web