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