Files: a448b261d6bffa420d1f0c4d57ebe852146001bf / lib / render-msg.js
52408 bytesRaw
1 | var h = require('hyperscript') |
2 | var htime = require('human-time') |
3 | var multicb = require('multicb') |
4 | var u = require('./util') |
5 | var mdInline = require('./markdown-inline') |
6 | |
7 | module.exports = RenderMsg |
8 | |
9 | function RenderMsg(render, app, msg, opts) { |
10 | this.render = render |
11 | this.app = app |
12 | this.msg = msg |
13 | this.serve = opts.serve |
14 | this.value = msg && msg.value || {} |
15 | var content = this.value.content |
16 | this.c = content || {} |
17 | this.isMissing = !content |
18 | |
19 | if (typeof opts === 'boolean') opts = {raw: opts} |
20 | this.opts = opts || {} |
21 | this.shouldWrap = this.opts.wrap !== false |
22 | } |
23 | |
24 | RenderMsg.prototype.getMsg = function (id, cb) { |
25 | if (!id) return cb() |
26 | return this.serve |
27 | ? this.serve.getMsgDecryptedMaybeOoo(id, cb) |
28 | : this.app.getMsgDecryptedOoo(id, cb) |
29 | } |
30 | |
31 | RenderMsg.prototype.toUrl = function (href) { |
32 | return this.render.toUrl(href) |
33 | } |
34 | |
35 | RenderMsg.prototype.linkify = function (text) { |
36 | return this.render.linkify(text) |
37 | } |
38 | |
39 | RenderMsg.prototype.raw = function (cb) { |
40 | // linkify various things in the JSON. TODO: abstract this better |
41 | |
42 | // clone the message for linkifying |
43 | var m = {}, k |
44 | for (k in this.msg) m[k] = this.msg[k] |
45 | m.value = {} |
46 | for (k in this.msg.value) m.value[k] = this.msg.value[k] |
47 | var tokens = {} |
48 | |
49 | // link to feed starting from this message |
50 | if (m.value.sequence) { |
51 | var tok = u.token() |
52 | tokens[tok] = h('a', {href: |
53 | this.toUrl(m.value.author + '?gt=' + (m.value.sequence-1))}, |
54 | m.value.sequence) |
55 | m.value.sequence = tok |
56 | } |
57 | |
58 | if (typeof m.value.content === 'object' && m.value.content != null) { |
59 | var c = m.value.content = {} |
60 | for (k in this.c) c[k] = this.c[k] |
61 | |
62 | // link to messages of same type |
63 | tok = u.token() |
64 | tokens[tok] = h('a', {href: this.toUrl('/type/' + c.type)}, c.type) |
65 | c.type = tok |
66 | |
67 | // link to channel |
68 | if (c.channel) { |
69 | tok = u.token() |
70 | tokens[tok] = h('a', {href: this.toUrl('#' + c.channel)}, c.channel) |
71 | c.channel = tok |
72 | } |
73 | |
74 | // link to hashtags |
75 | // TODO: recurse |
76 | for (var k in c) { |
77 | if (!c[k] || c[k][0] !== '#') continue |
78 | tok = u.token() |
79 | tokens[tok] = h('a', {href: this.toUrl(c[k])}, c[k]) |
80 | c[k] = tok |
81 | } |
82 | } |
83 | |
84 | // link refs |
85 | var els = this.linkify(JSON.stringify(m, 0, 2)) |
86 | |
87 | // stitch it all together |
88 | for (var i = 0; i < els.length; i++) { |
89 | if (typeof els[i] === 'string') { |
90 | for (var tok in tokens) { |
91 | if (els[i].indexOf(tok) !== -1) { |
92 | var parts = els[i].split(tok) |
93 | els.splice(i, 1, parts[0], tokens[tok], parts[1]) |
94 | continue |
95 | } |
96 | } |
97 | } |
98 | } |
99 | this.wrap(h('pre', els), cb) |
100 | } |
101 | |
102 | RenderMsg.prototype.wrap = function (content, cb) { |
103 | if (!this.shouldWrap) return cb(null, content) |
104 | var date = new Date(this.msg.value.timestamp) |
105 | var self = this |
106 | var channel = this.c.channel ? '#' + this.c.channel : '' |
107 | var done = multicb({pluck: 1, spread: true}) |
108 | done()(null, [h('tr.msg-row', |
109 | h('td.msg-left', |
110 | h('div', this.render.avatarImage(this.msg.value.author, done())), |
111 | h('div', this.render.idLink(this.msg.value.author, done())), |
112 | this.recpsLine(done()) |
113 | ), |
114 | h('td.msg-main', |
115 | h('div.msg-header', |
116 | h('a.ssb-timestamp', { |
117 | title: date.toLocaleString(), |
118 | href: this.msg.key ? this.toUrl(this.msg.key) : undefined |
119 | }, htime(date)), ' ', |
120 | h('code', h('a.ssb-id', |
121 | {href: this.toUrl(this.msg.key)}, this.msg.key)), |
122 | channel ? [' ', h('a', {href: this.toUrl(channel)}, channel)] : '')), |
123 | h('td.msg-right', this.actions()) |
124 | ), h('tr', |
125 | h('td.msg-content', {colspan: 3}, |
126 | this.issues(done()), |
127 | content) |
128 | )]) |
129 | done(cb) |
130 | } |
131 | |
132 | RenderMsg.prototype.wrapMini = function (content, cb) { |
133 | if (!this.shouldWrap) return cb(null, content) |
134 | var date = new Date(this.value.timestamp) |
135 | var self = this |
136 | var channel = this.c.channel ? '#' + this.c.channel : '' |
137 | var done = multicb({pluck: 1, spread: true}) |
138 | done()(null, h('tr.msg-row', |
139 | h('td.msg-left', |
140 | this.render.idLink(this.value.author, done()), ' ', |
141 | this.recpsLine(done()), |
142 | channel ? [h('a', {href: this.toUrl(channel)}, channel), ' '] : ''), |
143 | h('td.msg-main', |
144 | h('a.ssb-timestamp', { |
145 | title: date.toLocaleString(), |
146 | href: this.msg.key ? this.toUrl(this.msg.key) : undefined |
147 | }, htime(date)), ' ', |
148 | this.issues(done()), |
149 | content), |
150 | h('td.msg-right', this.actions()) |
151 | )) |
152 | done(cb) |
153 | } |
154 | |
155 | RenderMsg.prototype.actions = function () { |
156 | return this.msg.key ? |
157 | h('form', {method: 'post', action: ''}, |
158 | this.msg.rel ? [this.msg.rel, ' '] : '', |
159 | this.opts.withGt && this.msg.timestamp ? [ |
160 | h('a', {href: '?gt=' + this.msg.timestamp}, '↓'), ' '] : '', |
161 | this.c.type === 'gathering' ? [ |
162 | h('a', {href: this.render.toUrl('/about/' + encodeURIComponent(this.msg.key))}, 'about'), ' '] : '', |
163 | /^(ssb_)?chess_/.test(this.c.type) ? [ |
164 | h('a', {href: this.toUrl(this.msg.key) + '?full', |
165 | title: 'view full game board'}, 'full'), ' '] : '', |
166 | typeof this.c.text === 'string' ? [ |
167 | h('a', {href: this.toUrl(this.msg.key) + '?raw=md', |
168 | title: 'view markdown source'}, 'md'), ' '] : '', |
169 | h('a', {href: this.toUrl(this.msg.key) + '?raw', |
170 | title: 'view raw message'}, 'raw'), ' ', |
171 | this.buttonsCommon(), |
172 | this.c.type === 'gathering' ? [this.attendButton(), ' '] : '', |
173 | this.voteButton('dig') |
174 | ) : [ |
175 | this.msg.rel ? [this.msg.rel, ' '] : '' |
176 | ] |
177 | } |
178 | |
179 | RenderMsg.prototype.sync = function (cb) { |
180 | cb(null, h('tr.msg-row', h('td', {colspan: 3}, |
181 | h('hr') |
182 | ))) |
183 | } |
184 | |
185 | RenderMsg.prototype.recpsLine = function (cb) { |
186 | if (!this.value.private) return cb(), '' |
187 | var author = this.value.author |
188 | var recpsNotSelf = u.toArray(this.c.recps).filter(function (link) { |
189 | return u.linkDest(link) !== author |
190 | }) |
191 | return this.render.privateLine(recpsNotSelf, cb) |
192 | } |
193 | |
194 | RenderMsg.prototype.recpsIds = function () { |
195 | return this.value.private |
196 | ? u.toArray(this.c.recps).map(u.linkDest) |
197 | : [] |
198 | } |
199 | |
200 | RenderMsg.prototype.buttonsCommon = function () { |
201 | var chan = this.msg.value.content.channel |
202 | var recps = this.recpsIds() |
203 | return [ |
204 | chan ? h('input', {type: 'hidden', name: 'channel', value: chan}) : '', |
205 | h('input', {type: 'hidden', name: 'link', value: this.msg.key}), |
206 | h('input', {type: 'hidden', name: 'recps', value: recps.join(',')}) |
207 | ] |
208 | } |
209 | |
210 | RenderMsg.prototype.voteButton = function (expression) { |
211 | var chan = this.msg.value.content.channel |
212 | return [ |
213 | h('input', {type: 'hidden', name: 'vote_value', value: 1}), |
214 | h('input', {type: 'hidden', name: 'vote_expression', value: expression}), |
215 | h('input', {type: 'submit', name: 'action_vote', value: expression})] |
216 | } |
217 | |
218 | RenderMsg.prototype.attendButton = function () { |
219 | var chan = this.msg.value.content.channel |
220 | return [ |
221 | h('input', {type: 'submit', name: 'action_attend', value: 'attend'}) |
222 | ] |
223 | } |
224 | |
225 | RenderMsg.prototype.message = function (cb) { |
226 | if (this.opts.raw) return this.raw(cb) |
227 | if (this.msg.sync) return this.sync(cb) |
228 | if (typeof this.c === 'string') return this.encrypted(cb) |
229 | if (this.isMissing) return this.missing(cb) |
230 | switch (this.c.type) { |
231 | case 'post': return this.post(cb) |
232 | case 'ferment/like': |
233 | case 'robeson/like': |
234 | case 'vote': return this.vote(cb) |
235 | case 'about': return this.about(cb) |
236 | case 'contact': return this.contact(cb) |
237 | case 'pub': return this.pub(cb) |
238 | case 'channel': return this.channel(cb) |
239 | case 'git-repo': return this.gitRepo(cb) |
240 | case 'git-update': return this.gitUpdate(cb) |
241 | case 'pull-request': return this.gitPullRequest(cb) |
242 | case 'issue': return this.issue(cb) |
243 | case 'issue-edit': return this.issueEdit(cb) |
244 | case 'music-release-cc': return this.musicRelease(cb) |
245 | case 'ssb-dns': return this.dns(cb) |
246 | case 'gathering': return this.gathering(cb) |
247 | case 'micro': return this.micro(cb) |
248 | case 'ferment/audio': |
249 | case 'robeson/audio': |
250 | return this.audio(cb) |
251 | case 'ferment/repost': |
252 | case 'robeson/repost': |
253 | return this.repost(cb) |
254 | case 'ferment/update': |
255 | case 'robeson/update': |
256 | return this.update(cb) |
257 | case 'chess_invite': |
258 | case 'ssb_chess_invite': |
259 | return this.chessInvite(cb) |
260 | case 'chess_invite_accept': |
261 | case 'ssb_chess_invite_accept': |
262 | return this.chessInviteAccept(cb) |
263 | case 'chess_move': |
264 | case 'ssb_chess_move': |
265 | return this.chessMove(cb) |
266 | case 'chess_game_end': |
267 | case 'ssb_chess_game_end': |
268 | return this.chessGameEnd(cb) |
269 | case 'chess_chat': |
270 | return this.chessChat(cb) |
271 | case 'wifi-network': return this.wifiNetwork(cb) |
272 | case 'mutual/credit': return this.mutualCredit(cb) |
273 | case 'mutual/account': return this.mutualAccount(cb) |
274 | case 'npm-publish': return this.npmPublish(cb) |
275 | case 'npm-packages': return this.npmPackages(cb) |
276 | case 'npm-prebuilds': return this.npmPrebuilds(cb) |
277 | case 'acme-challenges-http-01': return this.acmeChallengesHttp01(cb) |
278 | case 'bookclub': return this.bookclub(cb) |
279 | case 'macaco_maluco-sombrio-wall': return this.sombrioWall(cb) |
280 | case 'macaco_maluco-sombrio-tombstone': return this.sombrioTombstone(cb) |
281 | case 'macaco_maluco-sombrio-score': return this.sombrioScore(cb) |
282 | case 'blog': return this.blog(cb) |
283 | case 'image-map': return this.imageMap(cb) |
284 | case 'talenet-identity-skill_assignment': return this.identitySkillAssign(cb) |
285 | case 'talenet-idea-skill_assignment': return this.ideaSkillAssign(cb) |
286 | case 'talenet-idea-create': return this.ideaCreate(cb) |
287 | case 'talenet-idea-association': return this.ideaAssocate(cb) |
288 | case 'talenet-skill-create': return this.skillCreate(cb) |
289 | case 'talenet-idea-hat': return this.ideaHat(cb) |
290 | case 'talenet-idea-update': return this.ideaUpdate(cb) |
291 | case 'talenet-idea-comment': |
292 | case 'talenet-idea-comment_reply': return this.ideaComment(cb) |
293 | case 'about-resource': return this.aboutResource(cb) |
294 | case 'line-comment': return this.lineComment(cb) |
295 | case 'web-init': return this.webInit(cb) |
296 | case 'web-root': return this.webRoot(cb) |
297 | default: return this.object(cb) |
298 | } |
299 | } |
300 | |
301 | RenderMsg.prototype.encrypted = function (cb) { |
302 | this.wrapMini(this.render.lockIcon(), cb) |
303 | } |
304 | |
305 | RenderMsg.prototype.markdown = function (cb) { |
306 | if (this.opts.markdownSource) |
307 | return this.markdownSource(this.c.text, this.c.mentions) |
308 | return this.render.markdown(this.c.text, this.c.mentions) |
309 | } |
310 | |
311 | RenderMsg.prototype.markdownSource = function (text, mentions) { |
312 | return h('div', |
313 | h('pre', String(text)), |
314 | mentions ? [ |
315 | h('div', h('em', 'mentions:')), |
316 | this.valueTable(mentions, 2, function () {}) |
317 | ] : '' |
318 | ).innerHTML |
319 | } |
320 | |
321 | RenderMsg.prototype.post = function (cb) { |
322 | var self = this |
323 | var done = multicb({pluck: 1, spread: true}) |
324 | if (self.c.root === self.c.branch) done()() |
325 | else self.link(self.c.root, done()) |
326 | self.links(self.c.branch, done()) |
327 | self.links(self.c.fork, done()) |
328 | done(function (err, rootLink, branchLinks, forkLinks) { |
329 | if (err) return self.wrap(u.renderError(err), cb) |
330 | self.wrap(h('div.ssb-post', |
331 | rootLink ? h('div', h('small', h('span.symbol', '→'), ' ', rootLink)) : '', |
332 | branchLinks.map(function (a, i) { |
333 | return h('div', h('small', h('span.symbol', ' ↳'), ' ', a)) |
334 | }), |
335 | forkLinks.map(function (a, i) { |
336 | return h('div', h('small', h('span.symbol', '⑂'), ' ', a)) |
337 | }), |
338 | h('div.ssb-post-text', {innerHTML: self.markdown()}) |
339 | ), cb) |
340 | }) |
341 | } |
342 | |
343 | RenderMsg.prototype.vote = function (cb) { |
344 | var self = this |
345 | var v = self.c.vote || self.c.like || {} |
346 | self.link(v, function (err, a) { |
347 | if (err) return cb(err) |
348 | self.wrapMini([ |
349 | v.value > 0 ? 'dug' : v.value < 0 ? 'downvoted' : 'undug', |
350 | ' ', a, |
351 | v.reason ? [' as ', h('q', v.reason)] : '' |
352 | ], cb) |
353 | }) |
354 | } |
355 | |
356 | RenderMsg.prototype.getName = function (id, cb) { |
357 | switch (id && id[0]) { |
358 | case '%': return this.getMsgName(id, cb) |
359 | case '@': // fallthrough |
360 | case '&': return this.getAboutName(id, cb) |
361 | default: return cb(null, String(id)) |
362 | } |
363 | } |
364 | |
365 | RenderMsg.prototype.getMsgName = function (id, cb) { |
366 | var self = this |
367 | self.app.getMsg(id, function (err, msg) { |
368 | if (err && err.name == 'NotFoundError') |
369 | cb(null, id.substring(0, 10)+'...(missing)') |
370 | else if (err) cb(err) |
371 | // preserve security: only decrypt the linked message if we decrypted |
372 | // this message |
373 | else if (self.msg.value.private) self.app.unboxMsg(msg, gotMsg) |
374 | else gotMsg(null, msg) |
375 | }) |
376 | function gotMsg(err, msg) { |
377 | if (err) return cb(err) |
378 | new RenderMsg(self.render, self.app, msg, {wrap: false}).title(cb) |
379 | } |
380 | } |
381 | |
382 | function truncate(str, len) { |
383 | str = String(str) |
384 | return str.length > len ? str.substr(0, len) + '...' : str |
385 | } |
386 | |
387 | function title(str) { |
388 | return truncate(mdInline(str), 72) |
389 | } |
390 | |
391 | RenderMsg.prototype.title = function (cb) { |
392 | var self = this |
393 | self.app.filterMsg(self.msg, self.opts, function (err, show) { |
394 | if (err) return cb(err) |
395 | if (show) self.title1(cb) |
396 | else cb(null, '[…]') |
397 | }) |
398 | } |
399 | |
400 | RenderMsg.prototype.title1 = function (cb) { |
401 | var self = this |
402 | if (!self.c || typeof self.c !== 'object') { |
403 | cb(null, self.msg.key) |
404 | } else if (typeof self.c.text === 'string') { |
405 | if (self.c.type === 'post') |
406 | cb(null, title(self.c.text)) |
407 | else |
408 | cb(null, '%' + self.c.type + ': ' + (self.c.title || title(self.c.text))) |
409 | } else { |
410 | if (self.c.type === 'ssb-dns') |
411 | cb(null, self.c.record && JSON.stringify(self.c.record.data) || self.msg.key) |
412 | else if (self.c.type === 'npm-publish') |
413 | self.npmPublishTitle(cb) |
414 | else if (self.c.type === 'chess_chat') |
415 | cb(null, title(self.c.msg)) |
416 | else if (self.c.type === 'chess_invite') |
417 | self.chessInviteTitle(cb) |
418 | else if (self.c.type === 'bookclub') |
419 | self.bookclubTitle(cb) |
420 | else if (self.c.type === 'talenet-skill-create' && self.c.name) |
421 | cb(null, self.c.name) |
422 | else if (self.c.type === 'talenet-idea-create') |
423 | self.app.getIdeaTitle(self.msg.key, cb) |
424 | else |
425 | self.app.getAbout(self.msg.key, function (err, about) { |
426 | if (err) return cb(err) |
427 | var name = about.name || about.title |
428 | || (about.description && mdInline(about.description)) |
429 | if (name) return cb(null, truncate(name, 72)) |
430 | self.message(function (err, el) { |
431 | if (err) return cb(err) |
432 | cb(null, '%' + title(h('div', el).textContent)) |
433 | }) |
434 | }) |
435 | } |
436 | } |
437 | |
438 | RenderMsg.prototype.getAboutName = function (id, cb) { |
439 | this.app.getAbout(id, function (err, about) { |
440 | cb(err, about && about.name || (String(id).substr(0, 8) + '…')) |
441 | }) |
442 | } |
443 | |
444 | RenderMsg.prototype.link = function (link, cb) { |
445 | var self = this |
446 | var ref = u.linkDest(link) |
447 | if (!ref) return cb(null, '') |
448 | self.getName(ref, function (err, name) { |
449 | if (err) name = truncate(ref, 10) |
450 | cb(null, h('a', {href: self.toUrl(ref)}, name)) |
451 | }) |
452 | } |
453 | |
454 | RenderMsg.prototype.link1 = function (link, cb) { |
455 | var self = this |
456 | var ref = u.linkDest(link) |
457 | if (!ref) return cb(), '' |
458 | var a = h('a', {href: self.toUrl(ref)}, ref) |
459 | self.getName(ref, function (err, name) { |
460 | if (err) name = ref |
461 | a.childNodes[0].textContent = name |
462 | cb() |
463 | }) |
464 | return a |
465 | } |
466 | |
467 | RenderMsg.prototype.links = function (links, cb) { |
468 | var self = this |
469 | var done = multicb({pluck: 1}) |
470 | u.toArray(links).forEach(function (link) { |
471 | self.link(link, done()) |
472 | }) |
473 | done(cb) |
474 | } |
475 | |
476 | function dateTime(d) { |
477 | var date = new Date(d.epoch) |
478 | return date.toString() |
479 | // d.bias |
480 | // d.epoch |
481 | } |
482 | |
483 | // TODO: make more DRY |
484 | var knownAboutProps = { |
485 | type: true, |
486 | root: true, |
487 | about: true, |
488 | attendee: true, |
489 | about: true, |
490 | image: true, |
491 | description: true, |
492 | name: true, |
493 | title: true, |
494 | attendee: true, |
495 | startDateTime: true, |
496 | endDateTime: true, |
497 | location: true, |
498 | /* |
499 | rating: true, |
500 | ratingType: true, |
501 | */ |
502 | 'talenet-version': true, |
503 | } |
504 | |
505 | RenderMsg.prototype.about = function (cb) { |
506 | var keys = Object.keys(this.c).sort().join() |
507 | var isSelf = this.c.about === this.msg.value.author |
508 | |
509 | if (keys === 'about,name,type') { |
510 | return this.wrapMini([ |
511 | isSelf ? |
512 | 'self-identifies as ' : |
513 | ['identifies ', h('a', {href: this.toUrl(this.c.about)}, truncate(this.c.about, 10)), ' as '], |
514 | h('ins', this.c.name) |
515 | ], cb) |
516 | } |
517 | |
518 | if (keys === 'about,publicWebHosting,type') { |
519 | var public = this.c.publicWebHosting && this.c.publicWebHosting !== 'false' |
520 | return this.wrapMini([ |
521 | isSelf ? |
522 | public ? 'is okay with being hosted publicly' |
523 | : 'wishes to not to be hosted publicly' |
524 | : public ? ['thinks ', h('a', {href: this.toUrl(this.c.about)}, truncate(this.c.about, 10)), |
525 | ' should be hosted publicly '] |
526 | : ['wishes ', h('a', {href: this.toUrl(this.c.about)}, truncate(this.c.about, 10)), |
527 | ' to not be hosted publicly'] |
528 | ], cb) |
529 | } |
530 | |
531 | var done = multicb({pluck: 1, spread: true}) |
532 | var elCb = done() |
533 | |
534 | var isAttendingMsg = u.linkDest(this.c.attendee) === this.msg.value.author |
535 | && keys === 'about,attendee,type' |
536 | if (isAttendingMsg) { |
537 | var attending = !this.c.attendee.remove |
538 | this.wrapMini([ |
539 | attending ? ' is attending' : ' is not attending', ' ', |
540 | this.link1(this.c.about, done()) |
541 | ], elCb) |
542 | return done(cb) |
543 | } |
544 | |
545 | var extras |
546 | for (var k in this.c) { |
547 | if (this.c[k] !== null && this.c[k] !== '' && !knownAboutProps[k]) { |
548 | if (!extras) extras = {} |
549 | extras[k] = this.c[k] |
550 | } |
551 | } |
552 | |
553 | var img = u.linkDest(this.c.image) |
554 | // if there is a description, it is likely to be multi-line |
555 | var hasDescription = this.c.description != null |
556 | // if this about message gives the thing a name, show its id |
557 | var showComputedName = !isSelf && !this.c.name |
558 | |
559 | this.wrap([ |
560 | this.c.root ? h('div', |
561 | h('small', '> ', this.link1(this.c.root, done())) |
562 | ) : '', |
563 | isSelf ? 'self-describes as ' : [ |
564 | 'describes ', |
565 | !this.c.about ? '' |
566 | : showComputedName ? this.link1(this.c.about, done()) |
567 | : h('a', {href: this.toUrl(this.c.about)}, truncate(this.c.about, 10)), |
568 | ' as ' |
569 | ], |
570 | this.c.name ? [h('ins', this.c.name), ' '] : '', |
571 | this.c.description ? h('div', |
572 | {innerHTML: this.render.markdown(this.c.description)}) : '', |
573 | this.c.title ? h('h3', this.c.title) : '', |
574 | this.c.attendee ? h('div', |
575 | this.link1(this.c.attendee.link, done()), |
576 | this.c.attendee.remove ? ' is not attending' : ' is attending' |
577 | ) : '', |
578 | this.c.startDateTime ? h('div', |
579 | 'starting at ', dateTime(this.c.startDateTime)) : '', |
580 | this.c.endDateTime ? h('div', |
581 | 'ending at ', dateTime(this.c.endDateTime)) : '', |
582 | this.c.location ? h('div', 'at ', this.c.location) : '', |
583 | img ? h('a', {href: this.toUrl(img)}, |
584 | h('img.ssb-avatar-image', { |
585 | src: this.render.imageUrl(img), |
586 | alt: ' ', |
587 | })) : '', |
588 | /* |
589 | this.c.rating != null ? this.aboutRating() : '', |
590 | */ |
591 | extras ? this.valueTable(extras, 1, done()) |
592 | : '' |
593 | ], elCb) |
594 | done(cb) |
595 | } |
596 | |
597 | /* |
598 | * disabled until it's clearer how to do this -cel |
599 | RenderMsg.prototype.aboutRating = function (cb) { |
600 | var rating = Number(this.c.rating) |
601 | var type = this.c.ratingType || '★' |
602 | var text = rating + ' ' + type |
603 | if (isNaN(rating)) return 'rating: ' + text |
604 | if (rating > 5) rating = 5 |
605 | var el = h('div', {title: text}) |
606 | for (var i = 0; i < rating; i++) { |
607 | el.appendChild(h('span', |
608 | {innerHTML: unwrapP(this.render.markdown(type) + ' ')} |
609 | )) |
610 | } |
611 | return el |
612 | } |
613 | */ |
614 | |
615 | RenderMsg.prototype.contact = function (cb) { |
616 | var self = this |
617 | self.link(self.c.contact, function (err, a) { |
618 | if (err) return cb(err) |
619 | if (!a) a = "?" |
620 | self.wrapMini([ |
621 | self.c.following && self.c.autofollow ? 'follows pub' : |
622 | self.c.following && self.c.pub ? 'autofollows' : |
623 | self.c.following ? 'follows' : |
624 | self.c.blocking ? 'blocks' : |
625 | self.c.flagged ? 'flagged' : |
626 | self.c.following === false ? 'unfollows' : |
627 | self.c.blocking === false ? 'unblocks' : '', |
628 | self.c.flagged === false ? 'unflagged' : |
629 | ' ', a, |
630 | self.c.note ? [ |
631 | ' from ', |
632 | h('code', self.c.note) |
633 | ] : '', |
634 | self.c.reason ? [' because ', h('q', self.c.reason)] : '' |
635 | ], cb) |
636 | }) |
637 | } |
638 | |
639 | RenderMsg.prototype.pub = function (cb) { |
640 | var self = this |
641 | var addr = self.c.address || {} |
642 | self.link(addr.key, function (err, pubLink) { |
643 | if (err) return cb(err) |
644 | self.wrapMini([ |
645 | 'connects to ', pubLink, ' at ', |
646 | h('code', addr.host + ':' + addr.port)], cb) |
647 | }) |
648 | } |
649 | |
650 | RenderMsg.prototype.channel = function (cb) { |
651 | var chan = '#' + this.c.channel |
652 | this.wrapMini([ |
653 | this.c.subscribed ? 'subscribes to ' : |
654 | this.c.subscribed === false ? 'unsubscribes from ' : '', |
655 | h('a', {href: this.toUrl(chan)}, chan)], cb) |
656 | } |
657 | |
658 | RenderMsg.prototype.gitRepo = function (cb) { |
659 | var self = this |
660 | var id = self.msg.key |
661 | var name = self.c.name |
662 | var upstream = self.c.upstream |
663 | self.link(upstream, function (err, upstreamA) { |
664 | if (err) upstreamA = ('a', {href: self.toUrl(upstream)}, String(name)) |
665 | self.wrapMini([ |
666 | upstream ? ['forked ', upstreamA, ': '] : '', |
667 | 'git clone ', |
668 | h('code', h('small', 'ssb://' + id)), |
669 | name ? [' ', h('a', {href: self.toUrl(id)}, String(name))] : '' |
670 | ], cb) |
671 | }) |
672 | } |
673 | |
674 | RenderMsg.prototype.gitUpdate = function (cb) { |
675 | var self = this |
676 | // h('a', {href: self.toUrl(self.c.repo)}, 'ssb://' + self.c.repo), |
677 | var size = [].concat(self.c.packs, self.c.indexes) |
678 | .map(function (o) { return o && o.size }) |
679 | .reduce(function (total, s) { return total + s }) |
680 | |
681 | var done = multicb({pluck: 1, spread: true}) |
682 | self.link(self.c.repo, done()) |
683 | self.render.npmPackageMentions(self.c.mentions, done()) |
684 | self.render.npmPrebuildMentions(self.c.mentions, done()) |
685 | done(function (err, a, pkgMentionsEl, prebuildMentionsEl) { |
686 | if (err) return cb(err) |
687 | self.wrap(h('div.ssb-git-update', |
688 | 'git push ', a, ' ', |
689 | !isNaN(size) ? [self.render.formatSize(size), ' '] : '', |
690 | self.c.refs ? h('ul', Object.keys(self.c.refs).map(function (ref) { |
691 | var id = self.c.refs[ref] |
692 | var type = /^refs\/tags/.test(ref) ? 'tag' : 'commit' |
693 | var path = id && ('/git/' + type + '/' + encodeURIComponent(id) |
694 | + '?msg=' + encodeURIComponent(self.msg.key)) |
695 | + '&search=1' |
696 | return h('li', |
697 | ref.replace(/^refs\/(heads|tags)\//, ''), ': ', |
698 | id ? h('a', {href: self.render.toUrl(path)}, h('code', id)) |
699 | : h('em', 'deleted')) |
700 | })) : '', |
701 | Array.isArray(self.c.commits) ? |
702 | h('ul', self.c.commits.map(function (commit) { |
703 | var path = '/git/commit/' + encodeURIComponent(commit.sha1) |
704 | + '?msg=' + encodeURIComponent(self.msg.key) |
705 | return h('li', h('a', {href: self.render.toUrl(path)}, |
706 | h('code', String(commit.sha1).substr(0, 8))), ' ', |
707 | self.linkify(String(commit.title)), |
708 | self.render.gitCommitBody(commit.body) |
709 | ) |
710 | })) : '', |
711 | Array.isArray(self.c.tags) ? |
712 | h('ul', self.c.tags.map(function (tag) { |
713 | var path = '/git/tag/' + encodeURIComponent(tag.sha1) |
714 | + '?msg=' + encodeURIComponent(self.msg.key) |
715 | return h('li', |
716 | h('a', {href: self.render.toUrl(path)}, |
717 | h('code', String(tag.sha1).substr(0, 8))), ' ', |
718 | 'tagged ', String(tag.type), ' ', |
719 | h('code', String(tag.object).substr(0, 8)), ' ', |
720 | String(tag.tag), |
721 | tag.title ? [': ', self.linkify(String(tag.title).trim()), ' '] : '', |
722 | tag.body ? self.render.gitCommitBody(tag.body) : '' |
723 | ) |
724 | })) : '', |
725 | self.c.commits_more ? h('div', |
726 | '+ ' + self.c.commits_more + ' more commits') : '', |
727 | self.c.tags_more ? h('div', |
728 | '+ ' + self.c.tags_more + ' more tags') : '', |
729 | pkgMentionsEl, |
730 | prebuildMentionsEl |
731 | ), cb) |
732 | }) |
733 | } |
734 | |
735 | RenderMsg.prototype.gitPullRequest = function (cb) { |
736 | var self = this |
737 | var done = multicb({pluck: 1, spread: true}) |
738 | self.link(self.c.repo, done()) |
739 | self.link(self.c.head_repo, done()) |
740 | done(function (err, baseRepoLink, headRepoLink) { |
741 | if (err) return cb(err) |
742 | self.wrap(h('div.ssb-pull-request', |
743 | 'pull request ', |
744 | 'to ', baseRepoLink, ':', self.c.branch, ' ', |
745 | 'from ', headRepoLink, ':', self.c.head_branch, |
746 | self.c.title ? h('h4', self.c.title) : '', |
747 | h('div', {innerHTML: self.markdown()})), cb) |
748 | }) |
749 | } |
750 | |
751 | RenderMsg.prototype.issue = function (cb) { |
752 | var self = this |
753 | self.link(self.c.project, function (err, projectLink) { |
754 | if (err) return cb(err) |
755 | self.wrap(h('div.ssb-issue', |
756 | 'issue on ', projectLink, |
757 | self.c.title ? h('h4', self.c.title) : '', |
758 | h('div', {innerHTML: self.markdown()})), cb) |
759 | }) |
760 | } |
761 | |
762 | RenderMsg.prototype.issueEdit = function (cb) { |
763 | this.wrap('', cb) |
764 | } |
765 | |
766 | RenderMsg.prototype.object = function (cb) { |
767 | var done = multicb({pluck: 1, spread: true}) |
768 | var elCb = done() |
769 | this.wrap([ |
770 | this.valueTable(this.c, 1, done()), |
771 | ], elCb) |
772 | done(cb) |
773 | } |
774 | |
775 | RenderMsg.prototype.valueTable = function (val, depth, cb) { |
776 | var isContent = depth === 1 |
777 | var self = this |
778 | switch (typeof val) { |
779 | case 'object': |
780 | if (val === null) return cb(), '' |
781 | var done = multicb({pluck: 1, spread: true}) |
782 | var el = Array.isArray(val) |
783 | ? h('ul', val.map(function (item) { |
784 | return h('li', self.valueTable(item, depth + 1, done())) |
785 | })) |
786 | : h('table.ssb-object', Object.keys(val).map(function (key) { |
787 | if (key === 'text') { |
788 | return h('tr', |
789 | h('td', h('strong', 'text')), |
790 | h('td', h('div', { |
791 | innerHTML: self.render.markdown(val.text, val.mentions) |
792 | })) |
793 | ) |
794 | } else if (isContent && key === 'type') { |
795 | // TODO: also link to images by type, using links2 |
796 | var type = val.type |
797 | return h('tr', |
798 | h('td', h('strong', 'type')), |
799 | h('td', h('a', {href: self.toUrl('/type/' + type)}, type)) |
800 | ) |
801 | } |
802 | return h('tr', |
803 | h('td', h('strong', key)), |
804 | h('td', self.valueTable(val[key], depth + 1, done())) |
805 | ) |
806 | })) |
807 | done(cb) |
808 | return el |
809 | case 'string': |
810 | if (val[0] === '#') return cb(null, h('a', {href: self.toUrl('/channel/' + val.substr(1))}, val)) |
811 | if (u.isRef(val)) return self.link1(val, cb) |
812 | if (/^ssb-blob:\/\//.test(val)) return cb(), h('a', {href: self.toUrl(val)}, val) |
813 | return cb(), self.linkify(val) |
814 | case 'boolean': |
815 | return cb(), h('input', { |
816 | type: 'checkbox', disabled: 'disabled', checked: val |
817 | }) |
818 | default: |
819 | return cb(), String(val) |
820 | } |
821 | } |
822 | |
823 | RenderMsg.prototype.missing = function (cb) { |
824 | this.wrapMini([ |
825 | h('code', 'MISSING'), ' ', |
826 | h('a', {href: '?ooo=1'}, 'fetch') |
827 | ], cb) |
828 | } |
829 | |
830 | RenderMsg.prototype.issues = function (cb) { |
831 | var self = this |
832 | var done = multicb({pluck: 1, spread: true}) |
833 | var issues = u.toArray(self.c.issues) |
834 | if (self.c.type === 'issue-edit' && self.c.issue) { |
835 | issues.push({ |
836 | link: self.c.issue, |
837 | title: self.c.title, |
838 | open: self.c.open, |
839 | }) |
840 | } |
841 | var els = issues.map(function (issue) { |
842 | var commit = issue.object || issue.label ? [ |
843 | issue.object ? h('code', issue.object) : '', ' ', |
844 | issue.label ? h('q', issue.label) : ''] : '' |
845 | if (issue.merged === true) |
846 | return h('div', |
847 | 'merged ', self.link1(issue, done())) |
848 | if (issue.open === false) |
849 | return h('div', |
850 | 'closed ', self.link1(issue, done())) |
851 | if (issue.open === true) |
852 | return h('div', |
853 | 'reopened ', self.link1(issue, done())) |
854 | if (typeof issue.title === 'string') |
855 | return h('div', |
856 | 'renamed ', self.link1(issue, done()), ' to ', h('ins', issue.title)) |
857 | }) |
858 | done(cb) |
859 | return els.length > 0 ? [els, h('br')] : '' |
860 | } |
861 | |
862 | RenderMsg.prototype.repost = function (cb) { |
863 | var self = this |
864 | var id = u.linkDest(self.c.repost) |
865 | self.app.getMsg(id, function (err, msg) { |
866 | if (err && err.name == 'NotFoundError') |
867 | gotMsg(null, id.substring(0, 10)+'...(missing)') |
868 | else if (err) gotMsg(err) |
869 | else if (self.msg.value.private) self.app.unboxMsg(msg, gotMsg) |
870 | else gotMsg(null, msg) |
871 | }) |
872 | function gotMsg(err, msg) { |
873 | if (err) return cb(err) |
874 | var renderMsg = new RenderMsg(self.render, self.app, msg, {wrap: false}) |
875 | renderMsg.message(function (err, msgEl) { |
876 | self.wrapMini(['reposted ', |
877 | h('code.ssb-id', |
878 | h('a', {href: self.render.toUrl(id)}, id)), |
879 | h('div', err ? u.renderError(err) : msgEl || '') |
880 | ], cb) |
881 | }) |
882 | } |
883 | } |
884 | |
885 | RenderMsg.prototype.update = function (cb) { |
886 | var id = String(this.c.update) |
887 | this.wrapMini([ |
888 | h('div', 'updated ', h('code.ssb-id', |
889 | h('a', {href: this.render.toUrl(id)}, id))), |
890 | this.c.title ? h('h4.msg-title', this.c.title) : '', |
891 | this.c.description ? h('div', |
892 | {innerHTML: this.render.markdown(this.c.description)}) : '' |
893 | ], cb) |
894 | } |
895 | |
896 | function formatDuration(s) { |
897 | return Math.floor(s / 60) + ':' + ('0' + s % 60).substr(-2) |
898 | } |
899 | |
900 | RenderMsg.prototype.audio = function (cb) { |
901 | // fileName, fallbackFileName, overview |
902 | this.wrap(h('table', h('tr', |
903 | h('td', |
904 | this.c.artworkSrc |
905 | ? h('a', {href: this.render.toUrl(this.c.artworkSrc)}, h('img', { |
906 | src: this.render.imageUrl(this.c.artworkSrc), |
907 | alt: ' ', |
908 | width: 72, |
909 | height: 72, |
910 | })) |
911 | : ''), |
912 | h('td', |
913 | h('a', {href: this.render.toUrl(this.c.audioSrc)}, this.c.title), |
914 | isFinite(this.c.duration) |
915 | ? ' (' + formatDuration(this.c.duration) + ')' |
916 | : '', |
917 | this.c.description |
918 | ? h('p', {innerHTML: this.render.markdown(this.c.description)}) |
919 | : '' |
920 | ))), cb) |
921 | } |
922 | |
923 | RenderMsg.prototype.musicRelease = function (cb) { |
924 | var self = this |
925 | this.wrap([ |
926 | h('table', h('tr', |
927 | h('td', |
928 | this.c.cover |
929 | ? h('a', {href: this.render.imageUrl(this.c.cover)}, h('img', { |
930 | src: this.render.imageUrl(this.c.cover), |
931 | alt: ' ', |
932 | width: 72, |
933 | height: 72, |
934 | })) |
935 | : ''), |
936 | h('td', |
937 | h('h4.msg-title', this.c.title), |
938 | this.c.text |
939 | ? h('div', {innerHTML: this.render.markdown(this.c.text)}) |
940 | : '' |
941 | ) |
942 | )), |
943 | h('ul', u.toArray(this.c.tracks).filter(Boolean).map(function (track) { |
944 | return h('li', |
945 | h('a', {href: self.render.toUrl(track.link)}, track.fname)) |
946 | })) |
947 | ], cb) |
948 | } |
949 | |
950 | RenderMsg.prototype.dns = function (cb) { |
951 | var self = this |
952 | var record = self.c.record || {} |
953 | var done = multicb({pluck: 1, spread: true}) |
954 | var elCb = done() |
955 | self.wrap([ |
956 | h('div', |
957 | h('p', |
958 | h('ins', {title: 'name'}, record.name), ' ', |
959 | h('span', {title: 'ttl'}, record.ttl), ' ', |
960 | h('span', {title: 'class'}, record.class), ' ', |
961 | h('span', {title: 'type'}, record.type) |
962 | ), |
963 | h('pre', {title: 'data'}, |
964 | JSON.stringify(record.data || record.value, null, 2)), |
965 | !self.c.branch ? null : h('div', |
966 | 'replaces: ', u.toArray(self.c.branch).map(function (id, i) { |
967 | return [self.link1(id, done()), i === 0 ? ', ' : ''] |
968 | }) |
969 | ) |
970 | ) |
971 | ], elCb) |
972 | done(cb) |
973 | } |
974 | |
975 | RenderMsg.prototype.wifiNetwork = function (cb) { |
976 | var net = this.c.network || {} |
977 | this.wrap([ |
978 | h('div', 'wifi network'), |
979 | h('table', |
980 | Object.keys(net).map(function (key) { |
981 | return h('tr', |
982 | h('td', key), |
983 | h('td', h('pre', JSON.stringify(net[key])))) |
984 | }) |
985 | ), |
986 | ], cb) |
987 | } |
988 | |
989 | RenderMsg.prototype.mutualCredit = function (cb) { |
990 | var self = this |
991 | var currency = String(self.c.currency) |
992 | self.link(self.c.account, function (err, a) { |
993 | if (err) return cb(err) |
994 | self.wrapMini([ |
995 | 'credits ', a || '?', ' ', |
996 | h('code', self.c.amount), ' ', |
997 | currency[0] === '#' |
998 | ? h('a', {href: self.toUrl(currency)}, currency) |
999 | : h('ins', currency), |
1000 | self.c.memo ? [' for ', h('q', self.c.memo)] : '' |
1001 | ], cb) |
1002 | }) |
1003 | } |
1004 | |
1005 | RenderMsg.prototype.mutualAccount = function (cb) { |
1006 | return this.object(cb) |
1007 | } |
1008 | |
1009 | RenderMsg.prototype.gathering = function (cb) { |
1010 | this.wrapMini('gathering', cb) |
1011 | } |
1012 | |
1013 | function unwrapP(html) { |
1014 | return String(html).replace(/^<p>(.*)<\/p>\s*$/, function ($0, $1) { |
1015 | return $1 |
1016 | }) |
1017 | } |
1018 | |
1019 | RenderMsg.prototype.micro = function (cb) { |
1020 | var el = h('span', {innerHTML: unwrapP(this.markdown())}) |
1021 | this.wrapMini(el, cb) |
1022 | } |
1023 | |
1024 | function hJoin(els, seperator, lastSeparator) { |
1025 | return els.map(function (el, i) { |
1026 | return [i === 0 ? '' : i === els.length-1 ? lastSeparator : seperator, el] |
1027 | }) |
1028 | } |
1029 | |
1030 | function asNpmReadme(readme) { |
1031 | if (!readme || readme === 'ERROR: No README data found!') return |
1032 | return u.ifString(readme) |
1033 | } |
1034 | |
1035 | function singleValue(obj) { |
1036 | if (!obj || typeof obj !== 'object') return obj |
1037 | var keys = Object.keys(obj) |
1038 | if (keys.length === 1) return obj[keys[0]] |
1039 | } |
1040 | |
1041 | function ifDifferent(obj, value) { |
1042 | if (singleValue(obj) !== value) return obj |
1043 | } |
1044 | |
1045 | RenderMsg.prototype.npmPublish = function (cb) { |
1046 | var self = this |
1047 | var render = self.render |
1048 | var pkg = self.c.meta || {} |
1049 | var pkgReadme = asNpmReadme(pkg.readme) |
1050 | var pkgDescription = u.ifString(pkg.description) |
1051 | |
1052 | var versions = Object.keys(pkg.versions || {}) |
1053 | var singleVersion = versions.length === 1 ? versions[0] : null |
1054 | var singleRelease = singleVersion && pkg.versions[singleVersion] |
1055 | var singleReadme = singleRelease && asNpmReadme(singleRelease.readme) |
1056 | |
1057 | var distTags = pkg['dist-tags'] || {} |
1058 | var distTagged = {} |
1059 | for (var distTag in distTags) |
1060 | if (distTag !== 'latest') |
1061 | distTagged[distTags[distTag]] = distTag |
1062 | |
1063 | self.links(self.c.previousPublish, function (err, prevLinks) { |
1064 | if (err) return cb(err) |
1065 | self.wrap([ |
1066 | h('div', |
1067 | 'published ', |
1068 | h('u', pkg.name), ' ', |
1069 | hJoin(versions.map(function (version) { |
1070 | var distTag = distTagged[version] |
1071 | return [h('b', version), distTag ? [' (', h('i', distTag), ')'] : ''] |
1072 | }), ', ') |
1073 | ), |
1074 | pkgDescription ? h('div', |
1075 | // TODO: make mdInline use custom emojis |
1076 | h('q', {innerHTML: unwrapP(render.markdown(pkgDescription))})) : '', |
1077 | prevLinks.length ? h('div', 'previous: ', prevLinks) : '', |
1078 | pkgReadme && pkgReadme !== singleReadme ? |
1079 | h('blockquote', {innerHTML: render.markdown(pkgReadme)}) : '', |
1080 | versions.map(function (version, i) { |
1081 | var release = pkg.versions[version] || {} |
1082 | var license = u.ifString(release.license) |
1083 | var author = ifDifferent(release.author, self.msg.value.author) |
1084 | var description = u.ifString(release.description) |
1085 | var readme = asNpmReadme(release.readme) |
1086 | var keywords = u.toArray(release.keywords).map(u.ifString) |
1087 | var dist = release.dist || {} |
1088 | var size = u.ifNumber(dist.size) |
1089 | return [ |
1090 | h > 0 ? h('br') : '', |
1091 | version !== singleVersion ? h('div', 'version: ', version) : '', |
1092 | author ? h('div', 'author: ', render.npmAuthorLink(author)) : '', |
1093 | license ? h('div', 'license: ', h('code', license)) : '', |
1094 | keywords.length ? h('div', 'keywords: ', keywords.join(', ')) : '', |
1095 | size ? h('div', 'size: ', render.formatSize(size)) : '', |
1096 | description && description !== pkgDescription ? |
1097 | h('div', h('q', {innerHTML: render.markdown(description)})) : '', |
1098 | readme ? h('blockquote', {innerHTML: render.markdown(readme)}) : '' |
1099 | ] |
1100 | }) |
1101 | ], cb) |
1102 | }) |
1103 | } |
1104 | |
1105 | RenderMsg.prototype.npmPackages = function (cb) { |
1106 | var self = this |
1107 | var done = multicb({pluck: 1, spread: true}) |
1108 | var elCb = done() |
1109 | function renderIdLink(id) { |
1110 | return [h('a', {href: self.toUrl(id)}, truncate(id, 8)), ' '] |
1111 | } |
1112 | var singlePkg = self.c.mentions |
1113 | && self.c.mentions.length === 1 |
1114 | && self.c.mentions[0] |
1115 | var m = singlePkg && /^npm:(.*?):(.*?):/.exec(singlePkg.name) |
1116 | var singlePkgSpec = m && (m[1] + (m[2] ? '@' + m[2] : '')) |
1117 | self.render.npmPackageMentions(self.c.mentions, function (err, el) { |
1118 | if (err) return cb(err) |
1119 | var dependencyLinks = u.toArray(self.c.dependencyBranch) |
1120 | var versionLinks = u.toArray(self.c.versionBranch) |
1121 | self.wrap(h('div', [ |
1122 | el, |
1123 | singlePkg ? h('p', |
1124 | h('code', |
1125 | 'npm install --registry=' + |
1126 | 'http://' + self.app.host + ':' + self.app.port + |
1127 | '/npm-registry/' + encodeURIComponent(self.msg.key) + ' ' + |
1128 | singlePkgSpec), |
1129 | ) : '', |
1130 | dependencyLinks.length ? h('div', |
1131 | 'dependencies via: ', dependencyLinks.map(renderIdLink) |
1132 | ) : '', |
1133 | versionLinks.length ? h('div', |
1134 | 'previous versions: ', versionLinks.map(renderIdLink) |
1135 | ) : '' |
1136 | ]), elCb) |
1137 | return done(cb) |
1138 | }) |
1139 | } |
1140 | |
1141 | RenderMsg.prototype.npmPrebuilds = function (cb) { |
1142 | var self = this |
1143 | self.render.npmPrebuildMentions(self.c.mentions, function (err, el) { |
1144 | if (err) return cb(err) |
1145 | self.wrap(el, cb) |
1146 | }) |
1147 | } |
1148 | |
1149 | RenderMsg.prototype.npmPublishTitle = function (cb) { |
1150 | var pkg = this.c.meta || {} |
1151 | var name = pkg.name || pkg._id || '?' |
1152 | |
1153 | var taggedVersions = {} |
1154 | for (var version in pkg.versions || {}) |
1155 | taggedVersions[version] = [] |
1156 | |
1157 | var distTags = pkg['dist-tags'] || {} |
1158 | for (var distTag in distTags) { |
1159 | if (distTag === 'latest') continue |
1160 | var version = distTags[distTag] || '?' |
1161 | var tags = taggedVersions[version] || (taggedVersions[version] = []) |
1162 | tags.push(distTag) |
1163 | } |
1164 | |
1165 | cb(null, name + '@' + Object.keys(taggedVersions).map(function (version) { |
1166 | var tags = taggedVersions[version] |
1167 | return (tags.length ? tags.join(',') + ':' : '') + version |
1168 | }).join(',')) |
1169 | } |
1170 | |
1171 | function expandDigitToSpaces(n) { |
1172 | return ' '.substr(-n) |
1173 | } |
1174 | |
1175 | function parseFenRank (line) { |
1176 | return line.replace(/\d/g, expandDigitToSpaces).split('') |
1177 | } |
1178 | |
1179 | function parseChess(fen) { |
1180 | var fields = String(fen).split(/\s+/) |
1181 | var ranks = fields[0].split('/') |
1182 | var f2 = fields[2] || '' |
1183 | return { |
1184 | board: ranks.map(parseFenRank), |
1185 | /* |
1186 | nextMove: fields[1] === 'b' ? 'black' |
1187 | : fields[1] === 'w' ? 'white' : 'unknown', |
1188 | castling: f2 === '-' ? {} : { |
1189 | w: { |
1190 | k: 0 < f2.indexOf('K'), |
1191 | q: 0 < f2.indexOf('Q'), |
1192 | }, |
1193 | b: { |
1194 | k: 0 < f2.indexOf('k'), |
1195 | q: 0 < f2.indexOf('q'), |
1196 | } |
1197 | }, |
1198 | enpassantTarget: fields[3] === '-' ? null : fields[3], |
1199 | halfmoves: Number(fields[4]), |
1200 | fullmoves: Number(fields[5]), |
1201 | */ |
1202 | } |
1203 | } |
1204 | |
1205 | var chessSymbols = { |
1206 | ' ': [' ', ''], |
1207 | P: ['♙', 'white', 'pawn'], |
1208 | N: ['♘', 'white', 'knight'], |
1209 | B: ['♗', 'white', 'bishop'], |
1210 | R: ['♖', 'white', 'rook'], |
1211 | Q: ['♕', 'white', 'queen'], |
1212 | K: ['♔', 'white', 'king'], |
1213 | p: ['♟', 'black', 'pawn'], |
1214 | n: ['♞', 'black', 'knight'], |
1215 | b: ['♝', 'black', 'bishop'], |
1216 | r: ['♜', 'black', 'rook'], |
1217 | q: ['♛', 'black', 'queen'], |
1218 | k: ['♚', 'black', 'king'], |
1219 | } |
1220 | |
1221 | function chessPieceName(c) { |
1222 | return chessSymbols[c] && chessSymbols[c][2] || '?' |
1223 | } |
1224 | |
1225 | function renderChessSymbol(c, loc) { |
1226 | var info = chessSymbols[c] || ['?', '', 'unknown'] |
1227 | return h('span.symbol', { |
1228 | title: info[1] + ' ' + info[2] + (loc ? ' at ' + loc : '') |
1229 | }, info[0]) |
1230 | } |
1231 | |
1232 | function chessLocToIdxs(loc) { |
1233 | var m = /^([a-h])([1-8])$/.exec(loc) |
1234 | if (m) return [8 - m[2], m[1].charCodeAt(0) - 97] |
1235 | } |
1236 | |
1237 | function lookupPiece(board, loc) { |
1238 | var idxs = chessLocToIdxs(loc) |
1239 | return idxs && board[idxs[0]] && board[idxs[0]][idxs[1]] |
1240 | } |
1241 | |
1242 | function chessIdxsToLoc(i, j) { |
1243 | return 'abcdefgh'[j] + (8-i) |
1244 | } |
1245 | |
1246 | RenderMsg.prototype.chessBoard = function (board) { |
1247 | if (!board) return '' |
1248 | return h('table.chess-board', |
1249 | board.map(function (rank, i) { |
1250 | return h('tr', rank.map(function (piece, j) { |
1251 | var dark = (i ^ j) & 1 |
1252 | return h('td', { |
1253 | class: 'chess-square chess-square-' + (dark ? 'dark' : 'light'), |
1254 | }, renderChessSymbol(piece, chessIdxsToLoc(i, j))) |
1255 | })) |
1256 | }) |
1257 | ) |
1258 | } |
1259 | |
1260 | RenderMsg.prototype.chessMove = function (cb) { |
1261 | var self = this |
1262 | var c = self.c |
1263 | var fen = c.fen && c.fen.length === 2 ? c.pgnMove : c.fen |
1264 | var game = parseChess(fen) |
1265 | var piece = game && lookupPiece(game.board, c.dest) |
1266 | self.link(self.c.root, function (err, rootLink) { |
1267 | if (err) return cb(err) |
1268 | self.wrap([ |
1269 | h('div', h('small', '> ', rootLink)), |
1270 | h('p', |
1271 | // 'player ', (c.ply || ''), ' ', |
1272 | 'moved ', (piece ? renderChessSymbol(piece) : ''), ' ', |
1273 | 'from ', c.orig, ' ', |
1274 | 'to ', c.dest |
1275 | ), |
1276 | self.chessBoard(game.board) |
1277 | ], cb) |
1278 | }) |
1279 | } |
1280 | |
1281 | RenderMsg.prototype.chessInvite = function (cb) { |
1282 | var self = this |
1283 | var myColor = self.c.myColor |
1284 | self.link(self.c.inviting, function (err, link) { |
1285 | if (err) return cb(err) |
1286 | self.wrap([ |
1287 | 'invites ', link, ' to play chess', |
1288 | // myColor ? h('p', 'my color is ' + myColor) : '' |
1289 | ], cb) |
1290 | }) |
1291 | } |
1292 | |
1293 | RenderMsg.prototype.chessInviteTitle = function (cb) { |
1294 | var self = this |
1295 | var done = multicb({pluck: 1, spread: true}) |
1296 | self.getName(self.c.inviting, done()) |
1297 | self.getName(self.msg.value.author, done()) |
1298 | done(function (err, inviteeLink, inviterLink) { |
1299 | if (err) return cb(err) |
1300 | self.wrap([ |
1301 | 'chess: ', inviterLink, ' vs. ', inviteeLink |
1302 | ], cb) |
1303 | }) |
1304 | } |
1305 | |
1306 | RenderMsg.prototype.chessInviteAccept = function (cb) { |
1307 | var self = this |
1308 | self.link(self.c.root, function (err, rootLink) { |
1309 | if (err) return cb(err) |
1310 | self.wrap([ |
1311 | h('div', h('small', '> ', rootLink)), |
1312 | h('p', 'accepts invitation to play chess') |
1313 | ], cb) |
1314 | }) |
1315 | } |
1316 | |
1317 | RenderMsg.prototype.chessGameEnd = function (cb) { |
1318 | var self = this |
1319 | var c = self.c |
1320 | if (c.status === 'resigned') return self.link(self.c.root, function (err, rootLink) { |
1321 | if (err) return cb(err) |
1322 | self.wrap([ |
1323 | h('div', h('small', '> ', rootLink)), |
1324 | h('p', h('strong', 'resigned')) |
1325 | ], cb) |
1326 | }) |
1327 | |
1328 | var fen = c.fen && c.fen.length === 2 ? c.pgnMove : c.fen |
1329 | var game = parseChess(fen) |
1330 | var piece = game && lookupPiece(game.board, c.dest) |
1331 | var done = multicb({pluck: 1, spread: true}) |
1332 | self.link(self.c.root, done()) |
1333 | self.link(self.c.winner, done()) |
1334 | done(function (err, rootLink, winnerLink) { |
1335 | if (err) return cb(err) |
1336 | self.wrap([ |
1337 | h('div', h('small', '> ', rootLink)), |
1338 | h('p', |
1339 | 'moved ', (piece ? renderChessSymbol(piece) : ''), ' ', |
1340 | 'from ', c.orig, ' ', |
1341 | 'to ', c.dest |
1342 | ), |
1343 | h('p', |
1344 | h('strong', self.c.status), '. winner: ', h('strong', winnerLink)), |
1345 | self.chessBoard(game.board) |
1346 | ], cb) |
1347 | }) |
1348 | } |
1349 | |
1350 | RenderMsg.prototype.chessChat = function (cb) { |
1351 | var self = this |
1352 | self.link(self.c.root, function (err, rootLink) { |
1353 | if (err) return cb(err) |
1354 | self.wrap([ |
1355 | h('div', h('small', '> ', rootLink)), |
1356 | h('p', self.c.msg) |
1357 | ], cb) |
1358 | }) |
1359 | } |
1360 | |
1361 | RenderMsg.prototype.chessMove = function (cb) { |
1362 | if (this.opts.full) return this.chessMoveFull(cb) |
1363 | return this.chessMoveMini(cb) |
1364 | } |
1365 | |
1366 | RenderMsg.prototype.chessMoveFull = function (cb) { |
1367 | var self = this |
1368 | var c = self.c |
1369 | var fen = c.fen && c.fen.length === 2 ? c.pgnMove : c.fen |
1370 | var game = parseChess(fen) |
1371 | var piece = game && lookupPiece(game.board, c.dest) |
1372 | self.link(self.c.root, function (err, rootLink) { |
1373 | if (err) return cb(err) |
1374 | self.wrap([ |
1375 | h('div', h('small', '> ', rootLink)), |
1376 | h('p', |
1377 | // 'player ', (c.ply || ''), ' ', |
1378 | 'moved ', (piece ? renderChessSymbol(piece) : ''), ' ', |
1379 | 'from ', c.orig, ' ', |
1380 | 'to ', c.dest |
1381 | ), |
1382 | self.chessBoard(game.board) |
1383 | ], cb) |
1384 | }) |
1385 | } |
1386 | |
1387 | RenderMsg.prototype.chessMoveMini = function (cb) { |
1388 | var self = this |
1389 | var c = self.c |
1390 | var fen = c.fen && c.fen.length === 2 ? c.pgnMove : c.fen |
1391 | var game = parseChess(fen) |
1392 | var piece = game && lookupPiece(game.board, c.dest) |
1393 | self.link(self.c.root, function (err, rootLink) { |
1394 | if (err) return cb(err) |
1395 | self.wrapMini([ |
1396 | 'moved ', chessPieceName(piece), ' ', |
1397 | 'to ', c.dest |
1398 | ], cb) |
1399 | }) |
1400 | } |
1401 | |
1402 | RenderMsg.prototype.acmeChallengesHttp01 = function (cb) { |
1403 | var self = this |
1404 | self.wrapMini(h('span', |
1405 | 'serves ', |
1406 | hJoin(u.toArray(self.c.challenges).map(function (challenge) { |
1407 | return h('a', { |
1408 | href: 'http://' + challenge.domain + |
1409 | '/.well-known/acme-challenge/' + challenge.token, |
1410 | title: challenge.keyAuthorization, |
1411 | }, challenge.domain) |
1412 | }), ', ', ', and ') |
1413 | ), cb) |
1414 | } |
1415 | |
1416 | RenderMsg.prototype.bookclub = function (cb) { |
1417 | var self = this |
1418 | var props = self.c.common || self.c |
1419 | var images = u.toLinkArray(props.image || props.images) |
1420 | self.wrap(h('table', h('tr', |
1421 | h('td', |
1422 | images.map(function (image) { |
1423 | return h('a', {href: self.render.toUrl(image.link)}, h('img', { |
1424 | src: self.render.imageUrl(image.link), |
1425 | alt: image.name || ' ', |
1426 | width: 180, |
1427 | })) |
1428 | })), |
1429 | h('td', |
1430 | h('h4', props.title), |
1431 | props.authors ? |
1432 | h('p', h('em', props.authors)) |
1433 | : '', |
1434 | props.description |
1435 | ? h('div', {innerHTML: self.render.markdown(props.description)}) |
1436 | : '' |
1437 | ) |
1438 | )), cb) |
1439 | } |
1440 | |
1441 | RenderMsg.prototype.bookclubTitle = function (cb) { |
1442 | var props = this.c.common || this.c |
1443 | cb(null, props.title || 'book') |
1444 | } |
1445 | |
1446 | RenderMsg.prototype.sombrioPosition = function () { |
1447 | return h('span', '[' + this.c.position + ']') |
1448 | } |
1449 | |
1450 | RenderMsg.prototype.sombrioWall = function (cb) { |
1451 | var self = this |
1452 | self.wrapMini(h('span', |
1453 | self.sombrioPosition(), |
1454 | ' wall' |
1455 | ), cb) |
1456 | } |
1457 | |
1458 | RenderMsg.prototype.sombrioTombstone = function (cb) { |
1459 | var self = this |
1460 | self.wrapMini(h('span', |
1461 | self.sombrioPosition(), |
1462 | ' tombstone' |
1463 | ), cb) |
1464 | } |
1465 | |
1466 | RenderMsg.prototype.sombrioScore = function (cb) { |
1467 | var self = this |
1468 | self.wrapMini(h('span', |
1469 | 'scored ', |
1470 | h('ins', self.c.score) |
1471 | ), cb) |
1472 | } |
1473 | |
1474 | RenderMsg.prototype.blog = function (cb) { |
1475 | var self = this |
1476 | var blogId = u.linkDest(self.c.blog) |
1477 | var imgId = u.linkDest(self.c.thumbnail) |
1478 | var imgLink = imgId ? u.toLinkArray(self.c.mentions).filter(function (link) { |
1479 | return link.link === imgId |
1480 | })[0] || u.toLink(self.c.thumbnail) : null |
1481 | self.wrapMini(h('table', h('tr', |
1482 | h('td', |
1483 | imgId ? h('img', { |
1484 | src: self.render.imageUrl(imgId), |
1485 | alt: (imgLink.name || '') |
1486 | + (imgLink.size != null ? ' (' + self.render.formatSize(imgLink.size) + ')' : ''), |
1487 | width: 180, |
1488 | }) : 'blog'), |
1489 | h('td', |
1490 | blogId ? h('h3', h('a', {href: self.render.toUrl('/markdown/' + blogId)}, |
1491 | self.c.title || self.msg.key)) : '', |
1492 | self.c.summary || '') |
1493 | )), cb) |
1494 | } |
1495 | |
1496 | RenderMsg.prototype.imageMap = function (cb) { |
1497 | var self = this |
1498 | var imgLink = u.toLink(self.c.image) |
1499 | var imgRef = imgLink && imgLink.link |
1500 | var mapName = 'map' + u.token() |
1501 | self.wrap(h('div', [ |
1502 | h('map', {name: mapName}, |
1503 | u.toArray(self.c.areas).map(function (areaLink) { |
1504 | var href = areaLink && self.toUrl(areaLink.link) |
1505 | return href ? h('area', { |
1506 | shape: String(areaLink.shape), |
1507 | coords: String(areaLink.coords), |
1508 | href: href, |
1509 | }) : '' |
1510 | }) |
1511 | ), |
1512 | imgRef && imgRef[0] === '&' ? h('img', { |
1513 | src: self.render.imageUrl(imgRef), |
1514 | width: Number(imgLink.width) || undefined, |
1515 | height: Number(imgLink.height) || undefined, |
1516 | alt: String(imgLink.name || ''), |
1517 | usemap: '#' + mapName, |
1518 | }) : '' |
1519 | ]), cb) |
1520 | } |
1521 | |
1522 | RenderMsg.prototype.skillCreate = function (cb) { |
1523 | var self = this |
1524 | self.wrapMini(h('span', |
1525 | ' created skill ', |
1526 | h('ins', self.c.name) |
1527 | ), cb) |
1528 | } |
1529 | |
1530 | RenderMsg.prototype.ideaCreate = function (cb) { |
1531 | var self = this |
1532 | self.wrapMini(h('span', |
1533 | ' has an idea' |
1534 | ), cb) |
1535 | } |
1536 | |
1537 | RenderMsg.prototype.identitySkillAssign = function (cb) { |
1538 | var self = this |
1539 | self.link(self.c.skillKey, function (err, a) { |
1540 | self.wrapMini(h('span', |
1541 | self.c.action === 'assign' ? 'assigns ' |
1542 | : self.c.action === 'unassign' ? 'unassigns ' |
1543 | : h('code', self.c.action), ' ', |
1544 | 'skill ', a |
1545 | ), cb) |
1546 | }) |
1547 | } |
1548 | |
1549 | RenderMsg.prototype.ideaSkillAssign = function (cb) { |
1550 | var self = this |
1551 | var done = multicb({pluck: 1, spread: true}) |
1552 | self.link(self.c.skillKey, done()) |
1553 | self.link(self.c.ideaKey, done()) |
1554 | done(function (err, skillA, ideaA) { |
1555 | self.wrapMini(h('span', |
1556 | self.c.action === 'assign' ? 'assigns ' |
1557 | : self.c.action === 'unassign' ? 'unassigns ' |
1558 | : h('code', self.c.action), ' ', |
1559 | 'skill ', skillA, |
1560 | ' to idea ', |
1561 | ideaA |
1562 | ), cb) |
1563 | }) |
1564 | } |
1565 | |
1566 | RenderMsg.prototype.ideaAssocate = function (cb) { |
1567 | var self = this |
1568 | self.link(self.c.ideaKey, function (err, a) { |
1569 | self.wrapMini(h('span', |
1570 | self.c.action === 'associate' ? 'associates with ' |
1571 | : self.c.action === 'disassociate' ? 'disassociates with ' |
1572 | : h('code', self.c.action), ' ', |
1573 | 'idea ', a |
1574 | ), cb) |
1575 | }) |
1576 | } |
1577 | |
1578 | RenderMsg.prototype.ideaHat = function (cb) { |
1579 | var self = this |
1580 | self.link(self.c.ideaKey, function (err, a) { |
1581 | self.wrapMini(h('span', |
1582 | self.c.action === 'take' ? 'takes ' |
1583 | : self.c.action === 'discard' ? 'discards ' |
1584 | : h('code', self.c.action), ' ', |
1585 | 'idea ', a |
1586 | ), cb) |
1587 | }) |
1588 | } |
1589 | |
1590 | RenderMsg.prototype.ideaUpdate = function (cb) { |
1591 | var self = this |
1592 | var done = multicb({pluck: 1, spread: true}) |
1593 | var props = {} |
1594 | for (var k in self.c) { |
1595 | if (k !== 'ideaKey' && k !== 'type' && k !== 'talenet-version') { |
1596 | props[k] = self.c[k] |
1597 | } |
1598 | } |
1599 | var keys = Object.keys(props).sort().join() |
1600 | |
1601 | if (keys === 'title') { |
1602 | return self.wrapMini(h('span', |
1603 | 'titles idea ', |
1604 | h('a', {href: self.toUrl(self.c.ideaKey)}, props.title) |
1605 | ), cb) |
1606 | } |
1607 | |
1608 | if (keys === 'description') { |
1609 | return self.link(self.c.ideaKey, function (err, a) { |
1610 | self.wrap(h('div', |
1611 | 'describes idea ', a, ':', |
1612 | h('blockquote', {innerHTML: self.render.markdown(props.description)}) |
1613 | ), cb) |
1614 | }) |
1615 | } |
1616 | |
1617 | if (keys === 'description,title') { |
1618 | return self.wrap(h('div', |
1619 | 'describes idea ', |
1620 | h('a', {href: self.toUrl(self.c.ideaKey)}, props.title), |
1621 | ':', |
1622 | h('blockquote', {innerHTML: self.render.markdown(props.description)}) |
1623 | ), cb) |
1624 | } |
1625 | |
1626 | self.link(self.c.ideaKey, done()) |
1627 | var table = self.valueTable(props, 1, done()) |
1628 | done(function (err, ideaA) { |
1629 | self.wrap(h('div', [ |
1630 | 'updates idea ', ideaA, |
1631 | table |
1632 | ]), cb) |
1633 | }) |
1634 | } |
1635 | |
1636 | RenderMsg.prototype.ideaComment = function (cb) { |
1637 | var self = this |
1638 | var done = multicb({pluck: 1, spread: true}) |
1639 | self.link(self.c.ideaKey, done()) |
1640 | self.link(self.c.commentKey, done()) |
1641 | done(function (err, ideaLink, commentLink) { |
1642 | if (err) return self.wrap(u.renderError(err), cb) |
1643 | self.wrap(h('div', [ |
1644 | ideaLink ? h('div', h('small', h('span.symbol', '→'), ' idea ', ideaLink)) : '', |
1645 | commentLink ? h('div', h('small', h('span.symbol', '↳'), ' comment ', commentLink)) : '', |
1646 | self.c.text ? |
1647 | h('div', {innerHTML: self.render.markdown(self.c.text)}) : '' |
1648 | ]), cb) |
1649 | }) |
1650 | } |
1651 | |
1652 | RenderMsg.prototype.aboutResource = function (cb) { |
1653 | var self = this |
1654 | return self.wrap(h('div', |
1655 | 'describes resource ', |
1656 | h('a', {href: self.toUrl(self.c.about)}, self.c.name), |
1657 | ':', |
1658 | h('blockquote', {innerHTML: self.render.markdown(self.c.description)}) |
1659 | ), cb) |
1660 | } |
1661 | |
1662 | RenderMsg.prototype.lineComment = function (cb) { |
1663 | var self = this |
1664 | var done = multicb({pluck: 1, spread: true}) |
1665 | self.link(self.c.repo, done()) |
1666 | self.getMsg(self.c.updateId, done()) |
1667 | done(function (err, repoLink, updateMsg) { |
1668 | if (err) return cb(err) |
1669 | return self.wrap(h('div', |
1670 | h('div', h('small', '> ', |
1671 | repoLink, ' ', |
1672 | h('a', { |
1673 | href: self.toUrl(self.c.updateId) |
1674 | }, |
1675 | updateMsg |
1676 | ? htime(new Date(updateMsg.value.timestamp)) |
1677 | : String(self.c.updateId) |
1678 | ), ' ', |
1679 | h('a', { |
1680 | href: self.toUrl('/git/commit/' + self.c.commitId + '?msg=' + encodeURIComponent(self.c.updateId)) |
1681 | }, String(self.c.commitId).substr(0, 8)), ' ', |
1682 | h('a', { |
1683 | href: self.toUrl('/git/line-comment/' + |
1684 | encodeURIComponent(self.msg.key || JSON.stringify(self.msg))) |
1685 | }, h('code', self.c.filePath + ':' + self.c.line)) |
1686 | )), |
1687 | self.c.text ? |
1688 | h('div', {innerHTML: self.markdown()}) : ''), cb) |
1689 | }) |
1690 | } |
1691 | |
1692 | RenderMsg.prototype.webInit = function (cb) { |
1693 | var self = this |
1694 | var url = '/web/' + encodeURIComponent(this.msg.key) |
1695 | self.wrapMini(h('a', |
1696 | {href: this.toUrl(url)}, |
1697 | 'website' |
1698 | ), cb) |
1699 | } |
1700 | |
1701 | RenderMsg.prototype.webRoot = function (cb) { |
1702 | var self = this |
1703 | var site = u.isRef(this.c.site) && this.c.site |
1704 | var root = u.isRef(this.c.root) && this.c.root |
1705 | self.wrapMini(h('span', |
1706 | 'updated website ', |
1707 | site ? [ |
1708 | h('a', {href: this.toUrl('/web/' + encodeURIComponent(site))}, site.substr(0, 8) + '…'), ' ' |
1709 | ] : '', |
1710 | root ? [ |
1711 | 'to ', h('a', {href: this.toUrl(root)}, root.substr(0, 8) + '…') |
1712 | ] : '' |
1713 | ), cb) |
1714 | } |
1715 |
Built with git-ssb-web