index.jsView |
---|
5 | 5 | var ssbGit = require('ssb-git-repo') |
6 | 6 | var toPull = require('stream-to-pull-stream') |
7 | 7 | var cat = require('pull-cat') |
8 | 8 | var Repo = require('pull-git-repo') |
| 9 | +var ssbAbout = require('./about') |
9 | 10 | |
10 | 11 | function parseAddr(str, def) { |
11 | 12 | if (!str) return def |
12 | 13 | var i = str.lastIndexOf(':') |
81 | 82 | heads: 'Branches' |
82 | 83 | } |
83 | 84 | |
84 | 85 | module.exports = function (listenAddr, cb) { |
85 | | - var ssb, reconnect |
| 86 | + var ssb, reconnect, myId |
| 87 | + var about = function (id, cb) { cb(null, {name: id}) } |
86 | 88 | |
87 | 89 | var addr = parseAddr(listenAddr, {host: 'localhost', port: 7718}) |
88 | 90 | http.createServer(onRequest).listen(addr.port, addr.host, onListening) |
89 | 91 | |
90 | 92 | var server = { |
91 | 93 | setSSB: function (_ssb, _reconnect) { |
92 | 94 | ssb = _ssb |
93 | 95 | reconnect = _reconnect |
| 96 | + ssb.whoami(function (err, feed) { |
| 97 | + myId = feed.id |
| 98 | + about = ssbAbout(ssb, ssb.id) |
| 99 | + }) |
94 | 100 | } |
95 | 101 | } |
96 | 102 | |
97 | 103 | function onListening() { |
190 | 196 | feedId ? ssb.createUserStream(opts) : ssb.createLogStream(opts), |
191 | 197 | pull.filter(function (msg) { |
192 | 198 | return msg.value.content.type in msgTypes |
193 | 199 | }), |
194 | | - pull.map(function (msg) { |
| 200 | + pull.asyncMap(function (msg, cb) { |
195 | 201 | 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) |
198 | 204 | } |
199 | 205 | }) |
200 | 206 | ) |
201 | 207 | } |
202 | 208 | |
203 | | - function renderRepoCreated(msg) { |
| 209 | + function renderRepoCreated(msg, cb) { |
204 | 210 | var repoLink = link([msg.key]) |
205 | 211 | 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>') |
208 | 214 | } |
209 | 215 | |
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 | + }) |
215 | 224 | } |
216 | 225 | |
217 | 226 | |
218 | 227 | |
239 | 248 | }], |
240 | 249 | '<!doctype html><html><head><meta charset=utf-8>', |
241 | 250 | '<title>git ssb</title></head><body>', |
242 | 251 | '<h1><a href="/">git ssb</a></h1>', |
243 | | - '<h2>' + feedId + '</h2>', |
244 | 252 | ]), |
| 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 | + }), |
245 | 259 | renderFeed(feedId), |
246 | 260 | pull.once('</body></html>') |
247 | 261 | ]) |
248 | 262 | } |
312 | 326 | '<!doctype html><html><head><meta charset=utf-8>' + |
313 | 327 | '<title>git ssb</title></head><body>' + |
314 | 328 | '<h1><a href="/">git ssb</a></h1>' + |
315 | 329 | '<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( |
318 | 337 | '<p>' + link([repo.id], 'Code') + ' ' + |
319 | 338 | link([repo.id, 'activity'], 'Activity') + ' ' + |
320 | 339 | link([repo.id, 'commits', branch], 'Commits') + '</p>' + |
321 | 340 | '<hr/>' |
322 | | - ]), |
| 341 | + ), |
323 | 342 | renderTry(body), |
324 | 343 | pull.once('<hr/></body></html>') |
325 | 344 | ]) |
326 | 345 | } |
356 | 375 | ) |
357 | 376 | ])) |
358 | 377 | |
359 | 378 | function renderRepoUpdate(msg) { |
360 | | - var authorLink = link([msg.value.author]) |
361 | 379 | var c = msg.value.content |
362 | 380 | |
363 | 381 | var refs = c.refs ? Object.keys(c.refs).map(function (ref) { |
364 | 382 | return {name: ref, value: c.refs[ref]} |