Files: 5a8a31c7e4523ef4de4c20e28443f9ea18961ce4 / lib / render-msg.js
42314 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 || about.description |
405 | if (name) return cb(null, name) |
406 | self.message(function (err, el) { |
407 | if (err) return cb(err) |
408 | cb(null, '%' + title(h('div', el).textContent)) |
409 | }) |
410 | }) |
411 | } |
412 | } |
413 | |
414 | RenderMsg.prototype.getAboutName = function (id, cb) { |
415 | this.app.getAbout(id, function (err, about) { |
416 | cb(err, about && about.name || (String(id).substr(0, 8) + '…')) |
417 | }) |
418 | } |
419 | |
420 | RenderMsg.prototype.link = function (link, cb) { |
421 | var self = this |
422 | var ref = u.linkDest(link) |
423 | if (!ref) return cb(null, '') |
424 | self.getName(ref, function (err, name) { |
425 | if (err) name = truncate(ref, 10) |
426 | cb(null, h('a', {href: self.toUrl(ref)}, name)) |
427 | }) |
428 | } |
429 | |
430 | RenderMsg.prototype.link1 = function (link, cb) { |
431 | var self = this |
432 | var ref = u.linkDest(link) |
433 | if (!ref) return cb(), '' |
434 | var a = h('a', {href: self.toUrl(ref)}, ref) |
435 | self.getName(ref, function (err, name) { |
436 | if (err) name = ref |
437 | a.childNodes[0].textContent = name |
438 | cb() |
439 | }) |
440 | return a |
441 | } |
442 | |
443 | RenderMsg.prototype.links = function (links, cb) { |
444 | var self = this |
445 | var done = multicb({pluck: 1}) |
446 | u.toArray(links).forEach(function (link) { |
447 | self.link(link, done()) |
448 | }) |
449 | done(cb) |
450 | } |
451 | |
452 | function dateTime(d) { |
453 | var date = new Date(d.epoch) |
454 | return date.toString() |
455 | // d.bias |
456 | // d.epoch |
457 | } |
458 | |
459 | // TODO: make more DRY |
460 | var knownAboutProps = { |
461 | type: true, |
462 | root: true, |
463 | about: true, |
464 | attendee: true, |
465 | about: true, |
466 | image: true, |
467 | description: true, |
468 | name: true, |
469 | title: true, |
470 | attendee: true, |
471 | startDateTime: true, |
472 | endDateTime: true, |
473 | location: true, |
474 | /* |
475 | rating: true, |
476 | ratingType: true, |
477 | */ |
478 | } |
479 | |
480 | RenderMsg.prototype.about = function (cb) { |
481 | var keys = Object.keys(this.c).sort().join() |
482 | var isSelf = this.c.about === this.msg.value.author |
483 | |
484 | if (keys === 'about,name,type') { |
485 | return this.wrapMini([ |
486 | isSelf ? |
487 | 'self-identifies as ' : |
488 | ['identifies ', h('a', {href: this.toUrl(this.c.about)}, truncate(this.c.about, 10)), ' as '], |
489 | h('ins', this.c.name) |
490 | ], cb) |
491 | } |
492 | |
493 | var done = multicb({pluck: 1, spread: true}) |
494 | var elCb = done() |
495 | |
496 | var isAttendingMsg = u.linkDest(this.c.attendee) === this.msg.value.author |
497 | && keys === 'about,attendee,type' |
498 | if (isAttendingMsg) { |
499 | var attending = !this.c.attendee.remove |
500 | this.wrapMini([ |
501 | attending ? ' is attending' : ' is not attending', ' ', |
502 | this.link1(this.c.about, done()) |
503 | ], elCb) |
504 | return done(cb) |
505 | } |
506 | |
507 | var extras |
508 | for (var k in this.c) { |
509 | if (this.c[k] !== null && this.c[k] !== '' && !knownAboutProps[k]) { |
510 | if (!extras) extras = {} |
511 | extras[k] = this.c[k] |
512 | } |
513 | } |
514 | |
515 | var img = u.linkDest(this.c.image) |
516 | // if there is a description, it is likely to be multi-line |
517 | var hasDescription = this.c.description != null |
518 | // if this about message gives the thing a name, show its id |
519 | var showComputedName = !isSelf && !this.c.name |
520 | |
521 | this.wrap([ |
522 | this.c.root ? h('div', |
523 | h('small', '> ', this.link1(this.c.root, done())) |
524 | ) : '', |
525 | isSelf ? 'self-describes as ' : [ |
526 | 'describes ', |
527 | !this.c.about ? '' |
528 | : showComputedName ? this.link1(this.c.about, done()) |
529 | : h('a', {href: this.toUrl(this.c.about)}, truncate(this.c.about, 10)), |
530 | ' as ' |
531 | ], |
532 | this.c.name ? [h('ins', this.c.name), ' '] : '', |
533 | this.c.description ? h('div', |
534 | {innerHTML: this.render.markdown(this.c.description)}) : '', |
535 | this.c.title ? h('h3', this.c.title) : '', |
536 | this.c.attendee ? h('div', |
537 | this.link1(this.c.attendee.link, done()), |
538 | this.c.attendee.remove ? ' is not attending' : ' is attending' |
539 | ) : '', |
540 | this.c.startDateTime ? h('div', |
541 | 'starting at ', dateTime(this.c.startDateTime)) : '', |
542 | this.c.endDateTime ? h('div', |
543 | 'ending at ', dateTime(this.c.endDateTime)) : '', |
544 | this.c.location ? h('div', 'at ', this.c.location) : '', |
545 | img ? h('a', {href: this.toUrl(img)}, |
546 | h('img.ssb-avatar-image', { |
547 | src: this.render.imageUrl(img), |
548 | alt: ' ', |
549 | })) : '', |
550 | /* |
551 | this.c.rating != null ? this.aboutRating() : '', |
552 | */ |
553 | extras ? this.valueTable(extras, 1, done()) |
554 | : '' |
555 | ], elCb) |
556 | done(cb) |
557 | } |
558 | |
559 | /* |
560 | * disabled until it's clearer how to do this -cel |
561 | RenderMsg.prototype.aboutRating = function (cb) { |
562 | var rating = Number(this.c.rating) |
563 | var type = this.c.ratingType || '★' |
564 | var text = rating + ' ' + type |
565 | if (isNaN(rating)) return 'rating: ' + text |
566 | if (rating > 5) rating = 5 |
567 | var el = h('div', {title: text}) |
568 | for (var i = 0; i < rating; i++) { |
569 | el.appendChild(h('span', |
570 | {innerHTML: unwrapP(this.render.markdown(type) + ' ')} |
571 | )) |
572 | } |
573 | return el |
574 | } |
575 | */ |
576 | |
577 | RenderMsg.prototype.contact = function (cb) { |
578 | var self = this |
579 | self.link(self.c.contact, function (err, a) { |
580 | if (err) return cb(err) |
581 | if (!a) a = "?" |
582 | self.wrapMini([ |
583 | self.c.following && self.c.autofollow ? 'follows pub' : |
584 | self.c.following && self.c.pub ? 'autofollows' : |
585 | self.c.following ? 'follows' : |
586 | self.c.blocking ? 'blocks' : |
587 | self.c.flagged ? 'flagged' : |
588 | self.c.following === false ? 'unfollows' : |
589 | self.c.blocking === false ? 'unblocks' : '', |
590 | self.c.flagged === false ? 'unflagged' : |
591 | ' ', a, |
592 | self.c.note ? [ |
593 | ' from ', |
594 | h('code', self.c.note) |
595 | ] : '', |
596 | self.c.reason ? [' because ', h('q', self.c.reason)] : '' |
597 | ], cb) |
598 | }) |
599 | } |
600 | |
601 | RenderMsg.prototype.pub = function (cb) { |
602 | var self = this |
603 | var addr = self.c.address || {} |
604 | self.link(addr.key, function (err, pubLink) { |
605 | if (err) return cb(err) |
606 | self.wrapMini([ |
607 | 'connects to ', pubLink, ' at ', |
608 | h('code', addr.host + ':' + addr.port)], cb) |
609 | }) |
610 | } |
611 | |
612 | RenderMsg.prototype.channel = function (cb) { |
613 | var chan = '#' + this.c.channel |
614 | this.wrapMini([ |
615 | this.c.subscribed ? 'subscribes to ' : |
616 | this.c.subscribed === false ? 'unsubscribes from ' : '', |
617 | h('a', {href: this.toUrl(chan)}, chan)], cb) |
618 | } |
619 | |
620 | RenderMsg.prototype.gitRepo = function (cb) { |
621 | var self = this |
622 | var id = self.msg.key |
623 | var name = self.c.name |
624 | var upstream = self.c.upstream |
625 | self.link(upstream, function (err, upstreamA) { |
626 | if (err) upstreamA = ('a', {href: self.toUrl(upstream)}, String(name)) |
627 | self.wrapMini([ |
628 | upstream ? ['forked ', upstreamA, ': '] : '', |
629 | 'git clone ', |
630 | h('code', h('small', 'ssb://' + id)), |
631 | name ? [' ', h('a', {href: self.toUrl(id)}, String(name))] : '' |
632 | ], cb) |
633 | }) |
634 | } |
635 | |
636 | RenderMsg.prototype.gitUpdate = function (cb) { |
637 | var self = this |
638 | // h('a', {href: self.toUrl(self.c.repo)}, 'ssb://' + self.c.repo), |
639 | var size = [].concat(self.c.packs, self.c.indexes) |
640 | .map(function (o) { return o && o.size }) |
641 | .reduce(function (total, s) { return total + s }) |
642 | |
643 | var done = multicb({pluck: 1, spread: true}) |
644 | self.link(self.c.repo, done()) |
645 | self.render.npmPackageMentions(self.c.mentions, done()) |
646 | self.render.npmPrebuildMentions(self.c.mentions, done()) |
647 | done(function (err, a, pkgMentionsEl, prebuildMentionsEl) { |
648 | if (err) return cb(err) |
649 | self.wrap(h('div.ssb-git-update', |
650 | 'git push ', a, ' ', |
651 | !isNaN(size) ? [self.render.formatSize(size), ' '] : '', |
652 | self.c.refs ? h('ul', Object.keys(self.c.refs).map(function (ref) { |
653 | var id = self.c.refs[ref] |
654 | var type = /^refs\/tags/.test(ref) ? 'tag' : 'commit' |
655 | var path = id && ('/git/' + type + '/' + encodeURIComponent(id) |
656 | + '?msg=' + encodeURIComponent(self.msg.key)) |
657 | return h('li', |
658 | ref.replace(/^refs\/(heads|tags)\//, ''), ': ', |
659 | id ? h('a', {href: self.render.toUrl(path)}, h('code', id)) |
660 | : h('em', 'deleted')) |
661 | })) : '', |
662 | Array.isArray(self.c.commits) ? |
663 | h('ul', self.c.commits.map(function (commit) { |
664 | var path = '/git/commit/' + encodeURIComponent(commit.sha1) |
665 | + '?msg=' + encodeURIComponent(self.msg.key) |
666 | return h('li', h('a', {href: self.render.toUrl(path)}, |
667 | h('code', String(commit.sha1).substr(0, 8))), ' ', |
668 | self.linkify(String(commit.title)), |
669 | self.render.gitCommitBody(commit.body) |
670 | ) |
671 | })) : '', |
672 | Array.isArray(self.c.tags) ? |
673 | h('ul', self.c.tags.map(function (tag) { |
674 | var path = '/git/tag/' + encodeURIComponent(tag.sha1) |
675 | + '?msg=' + encodeURIComponent(self.msg.key) |
676 | return h('li', |
677 | h('a', {href: self.render.toUrl(path)}, |
678 | h('code', String(tag.sha1).substr(0, 8))), ' ', |
679 | 'tagged ', String(tag.type), ' ', |
680 | h('code', String(tag.object).substr(0, 8)), ' ', |
681 | String(tag.tag), |
682 | tag.title ? [': ', self.linkify(String(tag.title).trim()), ' '] : '', |
683 | tag.body ? self.render.gitCommitBody(tag.body) : '' |
684 | ) |
685 | })) : '', |
686 | self.c.commits_more ? h('div', |
687 | '+ ' + self.c.commits_more + ' more commits') : '', |
688 | self.c.tags_more ? h('div', |
689 | '+ ' + self.c.tags_more + ' more tags') : '', |
690 | pkgMentionsEl, |
691 | prebuildMentionsEl |
692 | ), cb) |
693 | }) |
694 | } |
695 | |
696 | RenderMsg.prototype.gitPullRequest = function (cb) { |
697 | var self = this |
698 | var done = multicb({pluck: 1, spread: true}) |
699 | self.link(self.c.repo, done()) |
700 | self.link(self.c.head_repo, done()) |
701 | done(function (err, baseRepoLink, headRepoLink) { |
702 | if (err) return cb(err) |
703 | self.wrap(h('div.ssb-pull-request', |
704 | 'pull request ', |
705 | 'to ', baseRepoLink, ':', self.c.branch, ' ', |
706 | 'from ', headRepoLink, ':', self.c.head_branch, |
707 | self.c.title ? h('h4', self.c.title) : '', |
708 | h('div', {innerHTML: self.markdown()})), cb) |
709 | }) |
710 | } |
711 | |
712 | RenderMsg.prototype.issue = function (cb) { |
713 | var self = this |
714 | self.link(self.c.project, function (err, projectLink) { |
715 | if (err) return cb(err) |
716 | self.wrap(h('div.ssb-issue', |
717 | 'issue on ', projectLink, |
718 | self.c.title ? h('h4', self.c.title) : '', |
719 | h('div', {innerHTML: self.markdown()})), cb) |
720 | }) |
721 | } |
722 | |
723 | RenderMsg.prototype.issueEdit = function (cb) { |
724 | this.wrap('', cb) |
725 | } |
726 | |
727 | RenderMsg.prototype.object = function (cb) { |
728 | var done = multicb({pluck: 1, spread: true}) |
729 | var elCb = done() |
730 | this.wrap([ |
731 | this.valueTable(this.c, 1, done()), |
732 | ], elCb) |
733 | done(cb) |
734 | } |
735 | |
736 | RenderMsg.prototype.valueTable = function (val, depth, cb) { |
737 | var isContent = depth === 1 |
738 | var self = this |
739 | switch (typeof val) { |
740 | case 'object': |
741 | if (val === null) return cb(), '' |
742 | var done = multicb({pluck: 1, spread: true}) |
743 | var el = Array.isArray(val) |
744 | ? h('ul', val.map(function (item) { |
745 | return h('li', self.valueTable(item, depth + 1, done())) |
746 | })) |
747 | : h('table.ssb-object', Object.keys(val).map(function (key) { |
748 | if (key === 'text') { |
749 | return h('tr', |
750 | h('td', h('strong', 'text')), |
751 | h('td', h('div', { |
752 | innerHTML: self.render.markdown(val.text, val.mentions) |
753 | })) |
754 | ) |
755 | } else if (isContent && key === 'type') { |
756 | // TODO: also link to images by type, using links2 |
757 | var type = val.type |
758 | return h('tr', |
759 | h('td', h('strong', 'type')), |
760 | h('td', h('a', {href: self.toUrl('/type/' + type)}, type)) |
761 | ) |
762 | } |
763 | return h('tr', |
764 | h('td', h('strong', key)), |
765 | h('td', self.valueTable(val[key], depth + 1, done())) |
766 | ) |
767 | })) |
768 | done(cb) |
769 | return el |
770 | case 'string': |
771 | if (val[0] === '#') return cb(null, h('a', {href: self.toUrl('/channel/' + val.substr(1))}, val)) |
772 | if (u.isRef(val)) return self.link1(val, cb) |
773 | return cb(), self.linkify(val) |
774 | case 'boolean': |
775 | return cb(), h('input', { |
776 | type: 'checkbox', disabled: 'disabled', checked: val |
777 | }) |
778 | default: |
779 | return cb(), String(val) |
780 | } |
781 | } |
782 | |
783 | RenderMsg.prototype.missing = function (cb) { |
784 | this.wrapMini(h('code', 'MISSING'), cb) |
785 | } |
786 | |
787 | RenderMsg.prototype.issues = function (cb) { |
788 | var self = this |
789 | var done = multicb({pluck: 1, spread: true}) |
790 | var issues = u.toArray(self.c.issues) |
791 | if (self.c.type === 'issue-edit' && self.c.issue) { |
792 | issues.push({ |
793 | link: self.c.issue, |
794 | title: self.c.title, |
795 | open: self.c.open, |
796 | }) |
797 | } |
798 | var els = issues.map(function (issue) { |
799 | var commit = issue.object || issue.label ? [ |
800 | issue.object ? h('code', issue.object) : '', ' ', |
801 | issue.label ? h('q', issue.label) : ''] : '' |
802 | if (issue.merged === true) |
803 | return h('div', |
804 | 'merged ', self.link1(issue, done())) |
805 | if (issue.open === false) |
806 | return h('div', |
807 | 'closed ', self.link1(issue, done())) |
808 | if (issue.open === true) |
809 | return h('div', |
810 | 'reopened ', self.link1(issue, done())) |
811 | if (typeof issue.title === 'string') |
812 | return h('div', |
813 | 'renamed ', self.link1(issue, done()), ' to ', h('ins', issue.title)) |
814 | }) |
815 | done(cb) |
816 | return els.length > 0 ? [els, h('br')] : '' |
817 | } |
818 | |
819 | RenderMsg.prototype.repost = function (cb) { |
820 | var self = this |
821 | var id = u.linkDest(self.c.repost) |
822 | self.app.getMsg(id, function (err, msg) { |
823 | if (err && err.name == 'NotFoundError') |
824 | gotMsg(null, id.substring(0, 10)+'...(missing)') |
825 | else if (err) gotMsg(err) |
826 | else if (self.msg.value.private) self.app.unboxMsg(msg, gotMsg) |
827 | else gotMsg(null, msg) |
828 | }) |
829 | function gotMsg(err, msg) { |
830 | if (err) return cb(err) |
831 | var renderMsg = new RenderMsg(self.render, self.app, msg, {wrap: false}) |
832 | renderMsg.message(function (err, msgEl) { |
833 | self.wrapMini(['reposted ', |
834 | h('code.ssb-id', |
835 | h('a', {href: self.render.toUrl(id)}, id)), |
836 | h('div', err ? u.renderError(err) : msgEl || '') |
837 | ], cb) |
838 | }) |
839 | } |
840 | } |
841 | |
842 | RenderMsg.prototype.update = function (cb) { |
843 | var id = String(this.c.update) |
844 | this.wrapMini([ |
845 | h('div', 'updated ', h('code.ssb-id', |
846 | h('a', {href: this.render.toUrl(id)}, id))), |
847 | this.c.title ? h('h4.msg-title', this.c.title) : '', |
848 | this.c.description ? h('div', |
849 | {innerHTML: this.render.markdown(this.c.description)}) : '' |
850 | ], cb) |
851 | } |
852 | |
853 | function formatDuration(s) { |
854 | return Math.floor(s / 60) + ':' + ('0' + s % 60).substr(-2) |
855 | } |
856 | |
857 | RenderMsg.prototype.audio = function (cb) { |
858 | // fileName, fallbackFileName, overview |
859 | this.wrap(h('table', h('tr', |
860 | h('td', |
861 | this.c.artworkSrc |
862 | ? h('a', {href: this.render.toUrl(this.c.artworkSrc)}, h('img', { |
863 | src: this.render.imageUrl(this.c.artworkSrc), |
864 | alt: ' ', |
865 | width: 72, |
866 | height: 72, |
867 | })) |
868 | : ''), |
869 | h('td', |
870 | h('a', {href: this.render.toUrl(this.c.audioSrc)}, this.c.title), |
871 | isFinite(this.c.duration) |
872 | ? ' (' + formatDuration(this.c.duration) + ')' |
873 | : '', |
874 | this.c.description |
875 | ? h('p', {innerHTML: this.render.markdown(this.c.description)}) |
876 | : '' |
877 | ))), cb) |
878 | } |
879 | |
880 | RenderMsg.prototype.musicRelease = function (cb) { |
881 | var self = this |
882 | this.wrap([ |
883 | h('table', h('tr', |
884 | h('td', |
885 | this.c.cover |
886 | ? h('a', {href: this.render.imageUrl(this.c.cover)}, h('img', { |
887 | src: this.render.imageUrl(this.c.cover), |
888 | alt: ' ', |
889 | width: 72, |
890 | height: 72, |
891 | })) |
892 | : ''), |
893 | h('td', |
894 | h('h4.msg-title', this.c.title), |
895 | this.c.text |
896 | ? h('div', {innerHTML: this.render.markdown(this.c.text)}) |
897 | : '' |
898 | ) |
899 | )), |
900 | h('ul', u.toArray(this.c.tracks).filter(Boolean).map(function (track) { |
901 | return h('li', |
902 | h('a', {href: self.render.toUrl(track.link)}, track.fname)) |
903 | })) |
904 | ], cb) |
905 | } |
906 | |
907 | RenderMsg.prototype.dns = function (cb) { |
908 | var self = this |
909 | var record = self.c.record || {} |
910 | var done = multicb({pluck: 1, spread: true}) |
911 | var elCb = done() |
912 | self.wrap([ |
913 | h('div', |
914 | h('p', |
915 | h('ins', {title: 'name'}, record.name), ' ', |
916 | h('span', {title: 'ttl'}, record.ttl), ' ', |
917 | h('span', {title: 'class'}, record.class), ' ', |
918 | h('span', {title: 'type'}, record.type) |
919 | ), |
920 | h('pre', {title: 'data'}, |
921 | JSON.stringify(record.data || record.value, null, 2)), |
922 | !self.c.branch ? null : h('div', |
923 | 'replaces: ', u.toArray(self.c.branch).map(function (id, i) { |
924 | return [self.link1(id, done()), i === 0 ? ', ' : ''] |
925 | }) |
926 | ) |
927 | ) |
928 | ], elCb) |
929 | done(cb) |
930 | } |
931 | |
932 | RenderMsg.prototype.wifiNetwork = function (cb) { |
933 | var net = this.c.network || {} |
934 | this.wrap([ |
935 | h('div', 'wifi network'), |
936 | h('table', |
937 | Object.keys(net).map(function (key) { |
938 | return h('tr', |
939 | h('td', key), |
940 | h('td', h('pre', JSON.stringify(net[key])))) |
941 | }) |
942 | ), |
943 | ], cb) |
944 | } |
945 | |
946 | RenderMsg.prototype.mutualCredit = function (cb) { |
947 | var self = this |
948 | var currency = String(self.c.currency) |
949 | self.link(self.c.account, function (err, a) { |
950 | if (err) return cb(err) |
951 | self.wrapMini([ |
952 | 'credits ', a || '?', ' ', |
953 | h('code', self.c.amount), ' ', |
954 | currency[0] === '#' |
955 | ? h('a', {href: self.toUrl(currency)}, currency) |
956 | : h('ins', currency), |
957 | self.c.memo ? [' for ', h('q', self.c.memo)] : '' |
958 | ], cb) |
959 | }) |
960 | } |
961 | |
962 | RenderMsg.prototype.mutualAccount = function (cb) { |
963 | return this.object(cb) |
964 | } |
965 | |
966 | RenderMsg.prototype.gathering = function (cb) { |
967 | this.wrapMini('gathering', cb) |
968 | } |
969 | |
970 | function unwrapP(html) { |
971 | return String(html).replace(/^<p>(.*)<\/p>\s*$/, function ($0, $1) { |
972 | return $1 |
973 | }) |
974 | } |
975 | |
976 | RenderMsg.prototype.micro = function (cb) { |
977 | var el = h('span', {innerHTML: unwrapP(this.markdown())}) |
978 | this.wrapMini(el, cb) |
979 | } |
980 | |
981 | function hJoin(els, seperator, lastSeparator) { |
982 | return els.map(function (el, i) { |
983 | return [i === 0 ? '' : i === els.length-1 ? lastSeparator : seperator, el] |
984 | }) |
985 | } |
986 | |
987 | function asNpmReadme(readme) { |
988 | if (!readme || readme === 'ERROR: No README data found!') return |
989 | return u.ifString(readme) |
990 | } |
991 | |
992 | function singleValue(obj) { |
993 | if (!obj || typeof obj !== 'object') return obj |
994 | var keys = Object.keys(obj) |
995 | if (keys.length === 1) return obj[keys[0]] |
996 | } |
997 | |
998 | function ifDifferent(obj, value) { |
999 | if (singleValue(obj) !== value) return obj |
1000 | } |
1001 | |
1002 | RenderMsg.prototype.npmPublish = function (cb) { |
1003 | var self = this |
1004 | var render = self.render |
1005 | var pkg = self.c.meta || {} |
1006 | var pkgReadme = asNpmReadme(pkg.readme) |
1007 | var pkgDescription = u.ifString(pkg.description) |
1008 | |
1009 | var versions = Object.keys(pkg.versions || {}) |
1010 | var singleVersion = versions.length === 1 ? versions[0] : null |
1011 | var singleRelease = singleVersion && pkg.versions[singleVersion] |
1012 | var singleReadme = singleRelease && asNpmReadme(singleRelease.readme) |
1013 | |
1014 | var distTags = pkg['dist-tags'] || {} |
1015 | var distTagged = {} |
1016 | for (var distTag in distTags) |
1017 | if (distTag !== 'latest') |
1018 | distTagged[distTags[distTag]] = distTag |
1019 | |
1020 | self.links(self.c.previousPublish, function (err, prevLinks) { |
1021 | if (err) return cb(err) |
1022 | self.wrap([ |
1023 | h('div', |
1024 | 'published ', |
1025 | h('u', pkg.name), ' ', |
1026 | hJoin(versions.map(function (version) { |
1027 | var distTag = distTagged[version] |
1028 | return [h('b', version), distTag ? [' (', h('i', distTag), ')'] : ''] |
1029 | }), ', ') |
1030 | ), |
1031 | pkgDescription ? h('div', |
1032 | // TODO: make mdInline use custom emojis |
1033 | h('q', {innerHTML: unwrapP(render.markdown(pkgDescription))})) : '', |
1034 | prevLinks.length ? h('div', 'previous: ', prevLinks) : '', |
1035 | pkgReadme && pkgReadme !== singleReadme ? |
1036 | h('blockquote', {innerHTML: render.markdown(pkgReadme)}) : '', |
1037 | versions.map(function (version, i) { |
1038 | var release = pkg.versions[version] || {} |
1039 | var license = u.ifString(release.license) |
1040 | var author = ifDifferent(release.author, self.msg.value.author) |
1041 | var description = u.ifString(release.description) |
1042 | var readme = asNpmReadme(release.readme) |
1043 | var keywords = u.toArray(release.keywords).map(u.ifString) |
1044 | var dist = release.dist || {} |
1045 | var size = u.ifNumber(dist.size) |
1046 | return [ |
1047 | h > 0 ? h('br') : '', |
1048 | version !== singleVersion ? h('div', 'version: ', version) : '', |
1049 | author ? h('div', 'author: ', render.npmAuthorLink(author)) : '', |
1050 | license ? h('div', 'license: ', h('code', license)) : '', |
1051 | keywords.length ? h('div', 'keywords: ', keywords.join(', ')) : '', |
1052 | size ? h('div', 'size: ', render.formatSize(size)) : '', |
1053 | description && description !== pkgDescription ? |
1054 | h('div', h('q', {innerHTML: render.markdown(description)})) : '', |
1055 | readme ? h('blockquote', {innerHTML: render.markdown(readme)}) : '' |
1056 | ] |
1057 | }) |
1058 | ], cb) |
1059 | }) |
1060 | } |
1061 | |
1062 | RenderMsg.prototype.npmPackages = function (cb) { |
1063 | var self = this |
1064 | self.render.npmPackageMentions(self.c.mentions, function (err, el) { |
1065 | if (err) return cb(err) |
1066 | self.wrap(el, cb) |
1067 | }) |
1068 | } |
1069 | |
1070 | RenderMsg.prototype.npmPrebuilds = function (cb) { |
1071 | var self = this |
1072 | self.render.npmPrebuildMentions(self.c.mentions, function (err, el) { |
1073 | if (err) return cb(err) |
1074 | self.wrap(el, cb) |
1075 | }) |
1076 | } |
1077 | |
1078 | RenderMsg.prototype.npmPublishTitle = function (cb) { |
1079 | var pkg = this.c.meta || {} |
1080 | var name = pkg.name || pkg._id || '?' |
1081 | |
1082 | var taggedVersions = {} |
1083 | for (var version in pkg.versions || {}) |
1084 | taggedVersions[version] = [] |
1085 | |
1086 | var distTags = pkg['dist-tags'] || {} |
1087 | for (var distTag in distTags) { |
1088 | if (distTag === 'latest') continue |
1089 | var version = distTags[distTag] || '?' |
1090 | var tags = taggedVersions[version] || (taggedVersions[version] = []) |
1091 | tags.push(distTag) |
1092 | } |
1093 | |
1094 | cb(null, name + '@' + Object.keys(taggedVersions).map(function (version) { |
1095 | var tags = taggedVersions[version] |
1096 | return (tags.length ? tags.join(',') + ':' : '') + version |
1097 | }).join(',')) |
1098 | } |
1099 | |
1100 | function expandDigitToSpaces(n) { |
1101 | return ' '.substr(-n) |
1102 | } |
1103 | |
1104 | function parseFenRank (line) { |
1105 | return line.replace(/\d/g, expandDigitToSpaces).split('') |
1106 | } |
1107 | |
1108 | function parseChess(fen) { |
1109 | var fields = String(fen).split(/\s+/) |
1110 | var ranks = fields[0].split('/') |
1111 | var f2 = fields[2] || '' |
1112 | return { |
1113 | board: ranks.map(parseFenRank), |
1114 | /* |
1115 | nextMove: fields[1] === 'b' ? 'black' |
1116 | : fields[1] === 'w' ? 'white' : 'unknown', |
1117 | castling: f2 === '-' ? {} : { |
1118 | w: { |
1119 | k: 0 < f2.indexOf('K'), |
1120 | q: 0 < f2.indexOf('Q'), |
1121 | }, |
1122 | b: { |
1123 | k: 0 < f2.indexOf('k'), |
1124 | q: 0 < f2.indexOf('q'), |
1125 | } |
1126 | }, |
1127 | enpassantTarget: fields[3] === '-' ? null : fields[3], |
1128 | halfmoves: Number(fields[4]), |
1129 | fullmoves: Number(fields[5]), |
1130 | */ |
1131 | } |
1132 | } |
1133 | |
1134 | var chessSymbols = { |
1135 | ' ': [' ', ''], |
1136 | P: ['♙', 'white', 'pawn'], |
1137 | N: ['♘', 'white', 'knight'], |
1138 | B: ['♗', 'white', 'bishop'], |
1139 | R: ['♖', 'white', 'rook'], |
1140 | Q: ['♕', 'white', 'queen'], |
1141 | K: ['♔', 'white', 'king'], |
1142 | p: ['♟', 'black', 'pawn'], |
1143 | n: ['♞', 'black', 'knight'], |
1144 | b: ['♝', 'black', 'bishop'], |
1145 | r: ['♜', 'black', 'rook'], |
1146 | q: ['♛', 'black', 'queen'], |
1147 | k: ['♚', 'black', 'king'], |
1148 | } |
1149 | |
1150 | function chessPieceName(c) { |
1151 | return chessSymbols[c] && chessSymbols[c][2] || '?' |
1152 | } |
1153 | |
1154 | function renderChessSymbol(c, loc) { |
1155 | var info = chessSymbols[c] || ['?', '', 'unknown'] |
1156 | return h('span.symbol', { |
1157 | title: info[1] + ' ' + info[2] + (loc ? ' at ' + loc : '') |
1158 | }, info[0]) |
1159 | } |
1160 | |
1161 | function chessLocToIdxs(loc) { |
1162 | var m = /^([a-h])([1-8])$/.exec(loc) |
1163 | if (m) return [8 - m[2], m[1].charCodeAt(0) - 97] |
1164 | } |
1165 | |
1166 | function lookupPiece(board, loc) { |
1167 | var idxs = chessLocToIdxs(loc) |
1168 | return idxs && board[idxs[0]] && board[idxs[0]][idxs[1]] |
1169 | } |
1170 | |
1171 | function chessIdxsToLoc(i, j) { |
1172 | return 'abcdefgh'[j] + (8-i) |
1173 | } |
1174 | |
1175 | RenderMsg.prototype.chessBoard = function (board) { |
1176 | if (!board) return '' |
1177 | return h('table.chess-board', |
1178 | board.map(function (rank, i) { |
1179 | return h('tr', rank.map(function (piece, j) { |
1180 | var dark = (i ^ j) & 1 |
1181 | return h('td', { |
1182 | class: 'chess-square chess-square-' + (dark ? 'dark' : 'light'), |
1183 | }, renderChessSymbol(piece, chessIdxsToLoc(i, j))) |
1184 | })) |
1185 | }) |
1186 | ) |
1187 | } |
1188 | |
1189 | RenderMsg.prototype.chessMove = function (cb) { |
1190 | var self = this |
1191 | var c = self.c |
1192 | var fen = c.fen && c.fen.length === 2 ? c.pgnMove : c.fen |
1193 | var game = parseChess(fen) |
1194 | var piece = game && lookupPiece(game.board, c.dest) |
1195 | self.link(self.c.root, function (err, rootLink) { |
1196 | if (err) return cb(err) |
1197 | self.wrap([ |
1198 | h('div', h('small', '> ', rootLink)), |
1199 | h('p', |
1200 | // 'player ', (c.ply || ''), ' ', |
1201 | 'moved ', (piece ? renderChessSymbol(piece) : ''), ' ', |
1202 | 'from ', c.orig, ' ', |
1203 | 'to ', c.dest |
1204 | ), |
1205 | self.chessBoard(game.board) |
1206 | ], cb) |
1207 | }) |
1208 | } |
1209 | |
1210 | RenderMsg.prototype.chessInvite = function (cb) { |
1211 | var self = this |
1212 | var myColor = self.c.myColor |
1213 | self.link(self.c.inviting, function (err, link) { |
1214 | if (err) return cb(err) |
1215 | self.wrap([ |
1216 | 'invites ', link, ' to play chess', |
1217 | // myColor ? h('p', 'my color is ' + myColor) : '' |
1218 | ], cb) |
1219 | }) |
1220 | } |
1221 | |
1222 | RenderMsg.prototype.chessInviteTitle = function (cb) { |
1223 | var self = this |
1224 | var done = multicb({pluck: 1, spread: true}) |
1225 | self.getName(self.c.inviting, done()) |
1226 | self.getName(self.msg.value.author, done()) |
1227 | done(function (err, inviteeLink, inviterLink) { |
1228 | if (err) return cb(err) |
1229 | self.wrap([ |
1230 | 'chess: ', inviterLink, ' vs. ', inviteeLink |
1231 | ], cb) |
1232 | }) |
1233 | } |
1234 | |
1235 | RenderMsg.prototype.chessInviteAccept = function (cb) { |
1236 | var self = this |
1237 | self.link(self.c.root, function (err, rootLink) { |
1238 | if (err) return cb(err) |
1239 | self.wrap([ |
1240 | h('div', h('small', '> ', rootLink)), |
1241 | h('p', 'accepts invitation to play chess') |
1242 | ], cb) |
1243 | }) |
1244 | } |
1245 | |
1246 | RenderMsg.prototype.chessGameEnd = function (cb) { |
1247 | var self = this |
1248 | var c = self.c |
1249 | if (c.status === 'resigned') return self.link(self.c.root, function (err, rootLink) { |
1250 | if (err) return cb(err) |
1251 | self.wrap([ |
1252 | h('div', h('small', '> ', rootLink)), |
1253 | h('p', h('strong', 'resigned')) |
1254 | ], cb) |
1255 | }) |
1256 | |
1257 | var fen = c.fen && c.fen.length === 2 ? c.pgnMove : c.fen |
1258 | var game = parseChess(fen) |
1259 | var piece = game && lookupPiece(game.board, c.dest) |
1260 | var done = multicb({pluck: 1, spread: true}) |
1261 | self.link(self.c.root, done()) |
1262 | self.link(self.c.winner, done()) |
1263 | done(function (err, rootLink, winnerLink) { |
1264 | if (err) return cb(err) |
1265 | self.wrap([ |
1266 | h('div', h('small', '> ', rootLink)), |
1267 | h('p', |
1268 | 'moved ', (piece ? renderChessSymbol(piece) : ''), ' ', |
1269 | 'from ', c.orig, ' ', |
1270 | 'to ', c.dest |
1271 | ), |
1272 | h('p', |
1273 | h('strong', self.c.status), '. winner: ', h('strong', winnerLink)), |
1274 | self.chessBoard(game.board) |
1275 | ], cb) |
1276 | }) |
1277 | } |
1278 | |
1279 | RenderMsg.prototype.chessChat = function (cb) { |
1280 | var self = this |
1281 | self.link(self.c.root, function (err, rootLink) { |
1282 | if (err) return cb(err) |
1283 | self.wrap([ |
1284 | h('div', h('small', '> ', rootLink)), |
1285 | h('p', self.c.msg) |
1286 | ], cb) |
1287 | }) |
1288 | } |
1289 | |
1290 | RenderMsg.prototype.chessMove = function (cb) { |
1291 | if (this.opts.full) return this.chessMoveFull(cb) |
1292 | return this.chessMoveMini(cb) |
1293 | } |
1294 | |
1295 | RenderMsg.prototype.chessMoveFull = function (cb) { |
1296 | var self = this |
1297 | var c = self.c |
1298 | var fen = c.fen && c.fen.length === 2 ? c.pgnMove : c.fen |
1299 | var game = parseChess(fen) |
1300 | var piece = game && lookupPiece(game.board, c.dest) |
1301 | self.link(self.c.root, function (err, rootLink) { |
1302 | if (err) return cb(err) |
1303 | self.wrap([ |
1304 | h('div', h('small', '> ', rootLink)), |
1305 | h('p', |
1306 | // 'player ', (c.ply || ''), ' ', |
1307 | 'moved ', (piece ? renderChessSymbol(piece) : ''), ' ', |
1308 | 'from ', c.orig, ' ', |
1309 | 'to ', c.dest |
1310 | ), |
1311 | self.chessBoard(game.board) |
1312 | ], cb) |
1313 | }) |
1314 | } |
1315 | |
1316 | RenderMsg.prototype.chessMoveMini = function (cb) { |
1317 | var self = this |
1318 | var c = self.c |
1319 | var fen = c.fen && c.fen.length === 2 ? c.pgnMove : c.fen |
1320 | var game = parseChess(fen) |
1321 | var piece = game && lookupPiece(game.board, c.dest) |
1322 | self.link(self.c.root, function (err, rootLink) { |
1323 | if (err) return cb(err) |
1324 | self.wrapMini([ |
1325 | 'moved ', chessPieceName(piece), ' ', |
1326 | 'to ', c.dest |
1327 | ], cb) |
1328 | }) |
1329 | } |
1330 | |
1331 | RenderMsg.prototype.acmeChallengesHttp01 = function (cb) { |
1332 | var self = this |
1333 | self.wrapMini(h('span', |
1334 | 'serves ', |
1335 | hJoin(u.toArray(self.c.challenges).map(function (challenge) { |
1336 | return h('a', { |
1337 | href: 'http://' + challenge.domain + |
1338 | '/.well-known/acme-challenge/' + challenge.token, |
1339 | title: challenge.keyAuthorization, |
1340 | }, challenge.domain) |
1341 | }), ', ', ', and ') |
1342 | ), cb) |
1343 | } |
1344 | |
1345 | RenderMsg.prototype.bookclub = function (cb) { |
1346 | var self = this |
1347 | var props = self.c.common || self.c |
1348 | var images = u.toLinkArray(props.image || props.images) |
1349 | self.wrap(h('table', h('tr', |
1350 | h('td', |
1351 | images.map(function (image) { |
1352 | return h('a', {href: self.render.toUrl(image.link)}, h('img', { |
1353 | src: self.render.imageUrl(image.link), |
1354 | alt: image.name || ' ', |
1355 | width: 180, |
1356 | })) |
1357 | })), |
1358 | h('td', |
1359 | h('h4', props.title), |
1360 | props.authors ? |
1361 | h('p', h('em', props.authors)) |
1362 | : '', |
1363 | props.description |
1364 | ? h('div', {innerHTML: self.render.markdown(props.description)}) |
1365 | : '' |
1366 | ) |
1367 | )), cb) |
1368 | } |
1369 | |
1370 | RenderMsg.prototype.bookclubTitle = function (cb) { |
1371 | var props = this.c.common || this.c |
1372 | cb(null, props.title || 'book') |
1373 | } |
1374 | |
1375 | RenderMsg.prototype.sombrioPosition = function () { |
1376 | return h('span', '[' + this.c.position + ']') |
1377 | } |
1378 | |
1379 | RenderMsg.prototype.sombrioWall = function (cb) { |
1380 | var self = this |
1381 | self.wrapMini(h('span', |
1382 | self.sombrioPosition(), |
1383 | ' wall' |
1384 | ), cb) |
1385 | } |
1386 | |
1387 | RenderMsg.prototype.sombrioTombstone = function (cb) { |
1388 | var self = this |
1389 | self.wrapMini(h('span', |
1390 | self.sombrioPosition(), |
1391 | ' tombstone' |
1392 | ), cb) |
1393 | } |
1394 | |
1395 | RenderMsg.prototype.sombrioScore = function (cb) { |
1396 | var self = this |
1397 | self.wrapMini(h('span', |
1398 | 'scored ', |
1399 | h('ins', self.c.score) |
1400 | ), cb) |
1401 | } |
1402 |
Built with git-ssb-web