git ssb

30+

cel / git-ssb-web



Commit 88eec92c154cb3894b4a853ba05d69069267687a

Show user names

Charles Lehner committed on 2/28/2016, 4:41:15 AM
Parent: eb2d02088a944635c4774b64c891c01568b6fca6

Files changed

index.jschanged
about.jsadded
index.jsView
@@ -5,8 +5,9 @@
55 var ssbGit = require('ssb-git-repo')
66 var toPull = require('stream-to-pull-stream')
77 var cat = require('pull-cat')
88 var Repo = require('pull-git-repo')
9+var ssbAbout = require('./about')
910
1011 function parseAddr(str, def) {
1112 if (!str) return def
1213 var i = str.lastIndexOf(':')
@@ -81,17 +82,22 @@
8182 heads: 'Branches'
8283 }
8384
8485 module.exports = function (listenAddr, cb) {
85- var ssb, reconnect
86+ var ssb, reconnect, myId
87+ var about = function (id, cb) { cb(null, {name: id}) }
8688
8789 var addr = parseAddr(listenAddr, {host: 'localhost', port: 7718})
8890 http.createServer(onRequest).listen(addr.port, addr.host, onListening)
8991
9092 var server = {
9193 setSSB: function (_ssb, _reconnect) {
9294 ssb = _ssb
9395 reconnect = _reconnect
96+ ssb.whoami(function (err, feed) {
97+ myId = feed.id
98+ about = ssbAbout(ssb, ssb.id)
99+ })
94100 }
95101 }
96102
97103 function onListening() {
@@ -190,29 +196,32 @@
190196 feedId ? ssb.createUserStream(opts) : ssb.createLogStream(opts),
191197 pull.filter(function (msg) {
192198 return msg.value.content.type in msgTypes
193199 }),
194- pull.map(function (msg) {
200+ pull.asyncMap(function (msg, cb) {
195201 switch (msg.value.content.type) {
196- case 'git-repo': return renderRepoCreated(msg)
197- case 'git-update': return renderUpdate(msg)
202+ case 'git-repo': return renderRepoCreated(msg, cb)
203+ case 'git-update': return renderUpdate(msg, cb)
198204 }
199205 })
200206 )
201207 }
202208
203- function renderRepoCreated(msg) {
209+ function renderRepoCreated(msg, cb) {
204210 var repoLink = link([msg.key])
205211 var authorLink = link([msg.value.author])
206- return '<p>' + timestamp(msg.value.timestamp) + '<br>' +
207- authorLink + ' created repo ' + repoLink + '</p>'
212+ cb(null, '<p>' + timestamp(msg.value.timestamp) + '<br>' +
213+ authorLink + ' created repo ' + repoLink + '</p>')
208214 }
209215
210- function renderUpdate(msg) {
211- var repoLink = link([msg.value.content.repo])
212- var authorLink = link([msg.value.author])
213- return '<p>' + timestamp(msg.value.timestamp) + '<br>' +
214- authorLink + ' pushed to ' + repoLink + '</p>'
216+ function renderUpdate(msg, cb) {
217+ about.getName(msg.value.author, function (err, name) {
218+ if (err) return cb(err)
219+ var repoLink = link([msg.value.content.repo])
220+ var authorLink = link([msg.value.author], name)
221+ cb(null, '<p>' + timestamp(msg.value.timestamp) + '<br>' +
222+ authorLink + ' pushed to ' + repoLink + '</p>')
223+ })
215224 }
216225
217226 /* Index */
218227
@@ -239,10 +248,15 @@
239248 }],
240249 '<!doctype html><html><head><meta charset=utf-8>',
241250 '<title>git ssb</title></head><body>',
242251 '<h1><a href="/">git ssb</a></h1>',
243- '<h2>' + feedId + '</h2>',
244252 ]),
253+ readOnce(function (cb) {
254+ about.getName(feedId, function (err, name) {
255+ cb(null, '<h2>' + link([feedId], name) + '</h2>' +
256+ '<p><small><code>' + feedId + '</code></small></p>')
257+ })
258+ }),
245259 renderFeed(feedId),
246260 pull.once('</body></html>')
247261 ])
248262 }
@@ -312,15 +326,20 @@
312326 '<!doctype html><html><head><meta charset=utf-8>' +
313327 '<title>git ssb</title></head><body>' +
314328 '<h1><a href="/">git ssb</a></h1>' +
315329 '<h2>' + link([repo.id]) + '</h2>' +
316- '<p>git URL: ' + gitLink + '</p>' +
317- '<p>Author: ' + link([repo.feed]) + '</p>' +
330+ '<p>git URL: ' + gitLink + '</p>']),
331+ readOnce(function (cb) {
332+ about.getName(repo.feed, function (err, name) {
333+ cb(null, '<p>Author: ' + link([repo.feed], name) + '</p>')
334+ })
335+ }),
336+ pull.once(
318337 '<p>' + link([repo.id], 'Code') + ' ' +
319338 link([repo.id, 'activity'], 'Activity') + ' ' +
320339 link([repo.id, 'commits', branch], 'Commits') + '</p>' +
321340 '<hr/>'
322- ]),
341+ ),
323342 renderTry(body),
324343 pull.once('<hr/></body></html>')
325344 ])
326345 }
@@ -356,9 +375,8 @@
356375 )
357376 ]))
358377
359378 function renderRepoUpdate(msg) {
360- var authorLink = link([msg.value.author])
361379 var c = msg.value.content
362380
363381 var refs = c.refs ? Object.keys(c.refs).map(function (ref) {
364382 return {name: ref, value: c.refs[ref]}
about.jsView
@@ -1,0 +1,90 @@
1+/* ssb-about
2+ * factored out of ssb-notifier
3+ *
4+ * TODO:
5+ * - publish as own module
6+ * - handle live updates and reconnecting
7+ * - deprecate when ssb-names is used in scuttlebot
8+ */
9+
10+var pull = require('pull-stream')
11+var cat = require('pull-cat')
12+
13+module.exports = function (sbot, myId) {
14+ var cache = {/* id: about */}
15+ var callbacks = {/* id: [callback] */}
16+
17+ function getAbout(id, cb) {
18+ if (id in cache)
19+ return cb(null, cache[id])
20+ if (id in callbacks)
21+ return callbacks[id].push(cb)
22+ var cbs = callbacks[id] = [cb]
23+ getAboutFull(sbot, myId, id, function (err, about) {
24+ var about = !err && (cache[id] = about)
25+ while (cbs.length)
26+ cbs.pop()(err, about)
27+ })
28+ }
29+
30+ getAbout.getName = function (id, cb) {
31+ getAbout(id, function (err, about) {
32+ cb(err, about && about.name)
33+ })
34+ }
35+
36+ getAbout.getImage = function (id, cb) {
37+ getAbout(id, function (err, about) {
38+ cb(err, about && about.image)
39+ })
40+ }
41+
42+ return getAbout
43+}
44+
45+function truncate(str, len) {
46+ str = String(str)
47+ return str.length < len ? str : str.substr(0, len-1) + '…'
48+}
49+
50+// Get About info (name and icon) for a feed.
51+function getAboutFull(sbot, source, dest, cb) {
52+ var name, image
53+ pull(
54+ cat([
55+ // First get About info that we gave them.
56+ sbot.links({
57+ source: source,
58+ dest: dest,
59+ rel: 'about',
60+ values: true,
61+ reverse: true
62+ }),
63+ // If that isn't enough, then get About info that they gave themselves.
64+ sbot.links({
65+ source: dest,
66+ dest: dest,
67+ rel: 'about',
68+ values: true,
69+ reverse: true
70+ }),
71+ ]),
72+ pull.filter(function (msg) {
73+ return msg && msg.value.content && (!name || !image)
74+ }),
75+ pull.drain(function (msg) {
76+ var c = msg.value.content
77+ if (!name) {
78+ name = c.name
79+ }
80+ if (!image) {
81+ image = c.image ? c.image.link : c.image
82+ // var imgLink = mlib.link(c.image, 'blob')
83+ // image = imgLink && imgLink.link
84+ }
85+ }, function (err) {
86+ if (err) return cb(err)
87+ cb(null, {name: name || truncate(id, 8), image: image})
88+ })
89+ )
90+}

Built with git-ssb-web