Files: eade830580a16b2792fa592db3fbe6fc4a12473b / lib / render-msg.js
42369 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 | self.render.npmPackageMentions(self.c.mentions, function (err, el) { |
1066 | if (err) return cb(err) |
1067 | self.wrap(el, cb) |
1068 | }) |
1069 | } |
1070 | |
1071 | RenderMsg.prototype.npmPrebuilds = function (cb) { |
1072 | var self = this |
1073 | self.render.npmPrebuildMentions(self.c.mentions, function (err, el) { |
1074 | if (err) return cb(err) |
1075 | self.wrap(el, cb) |
1076 | }) |
1077 | } |
1078 | |
1079 | RenderMsg.prototype.npmPublishTitle = function (cb) { |
1080 | var pkg = this.c.meta || {} |
1081 | var name = pkg.name || pkg._id || '?' |
1082 | |
1083 | var taggedVersions = {} |
1084 | for (var version in pkg.versions || {}) |
1085 | taggedVersions[version] = [] |
1086 | |
1087 | var distTags = pkg['dist-tags'] || {} |
1088 | for (var distTag in distTags) { |
1089 | if (distTag === 'latest') continue |
1090 | var version = distTags[distTag] || '?' |
1091 | var tags = taggedVersions[version] || (taggedVersions[version] = []) |
1092 | tags.push(distTag) |
1093 | } |
1094 | |
1095 | cb(null, name + '@' + Object.keys(taggedVersions).map(function (version) { |
1096 | var tags = taggedVersions[version] |
1097 | return (tags.length ? tags.join(',') + ':' : '') + version |
1098 | }).join(',')) |
1099 | } |
1100 | |
1101 | function expandDigitToSpaces(n) { |
1102 | return ' '.substr(-n) |
1103 | } |
1104 | |
1105 | function parseFenRank (line) { |
1106 | return line.replace(/\d/g, expandDigitToSpaces).split('') |
1107 | } |
1108 | |
1109 | function parseChess(fen) { |
1110 | var fields = String(fen).split(/\s+/) |
1111 | var ranks = fields[0].split('/') |
1112 | var f2 = fields[2] || '' |
1113 | return { |
1114 | board: ranks.map(parseFenRank), |
1115 | /* |
1116 | nextMove: fields[1] === 'b' ? 'black' |
1117 | : fields[1] === 'w' ? 'white' : 'unknown', |
1118 | castling: f2 === '-' ? {} : { |
1119 | w: { |
1120 | k: 0 < f2.indexOf('K'), |
1121 | q: 0 < f2.indexOf('Q'), |
1122 | }, |
1123 | b: { |
1124 | k: 0 < f2.indexOf('k'), |
1125 | q: 0 < f2.indexOf('q'), |
1126 | } |
1127 | }, |
1128 | enpassantTarget: fields[3] === '-' ? null : fields[3], |
1129 | halfmoves: Number(fields[4]), |
1130 | fullmoves: Number(fields[5]), |
1131 | */ |
1132 | } |
1133 | } |
1134 | |
1135 | var chessSymbols = { |
1136 | ' ': [' ', ''], |
1137 | P: ['♙', 'white', 'pawn'], |
1138 | N: ['♘', 'white', 'knight'], |
1139 | B: ['♗', 'white', 'bishop'], |
1140 | R: ['♖', 'white', 'rook'], |
1141 | Q: ['♕', 'white', 'queen'], |
1142 | K: ['♔', 'white', 'king'], |
1143 | p: ['♟', 'black', 'pawn'], |
1144 | n: ['♞', 'black', 'knight'], |
1145 | b: ['♝', 'black', 'bishop'], |
1146 | r: ['♜', 'black', 'rook'], |
1147 | q: ['♛', 'black', 'queen'], |
1148 | k: ['♚', 'black', 'king'], |
1149 | } |
1150 | |
1151 | function chessPieceName(c) { |
1152 | return chessSymbols[c] && chessSymbols[c][2] || '?' |
1153 | } |
1154 | |
1155 | function renderChessSymbol(c, loc) { |
1156 | var info = chessSymbols[c] || ['?', '', 'unknown'] |
1157 | return h('span.symbol', { |
1158 | title: info[1] + ' ' + info[2] + (loc ? ' at ' + loc : '') |
1159 | }, info[0]) |
1160 | } |
1161 | |
1162 | function chessLocToIdxs(loc) { |
1163 | var m = /^([a-h])([1-8])$/.exec(loc) |
1164 | if (m) return [8 - m[2], m[1].charCodeAt(0) - 97] |
1165 | } |
1166 | |
1167 | function lookupPiece(board, loc) { |
1168 | var idxs = chessLocToIdxs(loc) |
1169 | return idxs && board[idxs[0]] && board[idxs[0]][idxs[1]] |
1170 | } |
1171 | |
1172 | function chessIdxsToLoc(i, j) { |
1173 | return 'abcdefgh'[j] + (8-i) |
1174 | } |
1175 | |
1176 | RenderMsg.prototype.chessBoard = function (board) { |
1177 | if (!board) return '' |
1178 | return h('table.chess-board', |
1179 | board.map(function (rank, i) { |
1180 | return h('tr', rank.map(function (piece, j) { |
1181 | var dark = (i ^ j) & 1 |
1182 | return h('td', { |
1183 | class: 'chess-square chess-square-' + (dark ? 'dark' : 'light'), |
1184 | }, renderChessSymbol(piece, chessIdxsToLoc(i, j))) |
1185 | })) |
1186 | }) |
1187 | ) |
1188 | } |
1189 | |
1190 | RenderMsg.prototype.chessMove = function (cb) { |
1191 | var self = this |
1192 | var c = self.c |
1193 | var fen = c.fen && c.fen.length === 2 ? c.pgnMove : c.fen |
1194 | var game = parseChess(fen) |
1195 | var piece = game && lookupPiece(game.board, c.dest) |
1196 | self.link(self.c.root, function (err, rootLink) { |
1197 | if (err) return cb(err) |
1198 | self.wrap([ |
1199 | h('div', h('small', '> ', rootLink)), |
1200 | h('p', |
1201 | // 'player ', (c.ply || ''), ' ', |
1202 | 'moved ', (piece ? renderChessSymbol(piece) : ''), ' ', |
1203 | 'from ', c.orig, ' ', |
1204 | 'to ', c.dest |
1205 | ), |
1206 | self.chessBoard(game.board) |
1207 | ], cb) |
1208 | }) |
1209 | } |
1210 | |
1211 | RenderMsg.prototype.chessInvite = function (cb) { |
1212 | var self = this |
1213 | var myColor = self.c.myColor |
1214 | self.link(self.c.inviting, function (err, link) { |
1215 | if (err) return cb(err) |
1216 | self.wrap([ |
1217 | 'invites ', link, ' to play chess', |
1218 | // myColor ? h('p', 'my color is ' + myColor) : '' |
1219 | ], cb) |
1220 | }) |
1221 | } |
1222 | |
1223 | RenderMsg.prototype.chessInviteTitle = function (cb) { |
1224 | var self = this |
1225 | var done = multicb({pluck: 1, spread: true}) |
1226 | self.getName(self.c.inviting, done()) |
1227 | self.getName(self.msg.value.author, done()) |
1228 | done(function (err, inviteeLink, inviterLink) { |
1229 | if (err) return cb(err) |
1230 | self.wrap([ |
1231 | 'chess: ', inviterLink, ' vs. ', inviteeLink |
1232 | ], cb) |
1233 | }) |
1234 | } |
1235 | |
1236 | RenderMsg.prototype.chessInviteAccept = function (cb) { |
1237 | var self = this |
1238 | self.link(self.c.root, function (err, rootLink) { |
1239 | if (err) return cb(err) |
1240 | self.wrap([ |
1241 | h('div', h('small', '> ', rootLink)), |
1242 | h('p', 'accepts invitation to play chess') |
1243 | ], cb) |
1244 | }) |
1245 | } |
1246 | |
1247 | RenderMsg.prototype.chessGameEnd = function (cb) { |
1248 | var self = this |
1249 | var c = self.c |
1250 | if (c.status === 'resigned') return self.link(self.c.root, function (err, rootLink) { |
1251 | if (err) return cb(err) |
1252 | self.wrap([ |
1253 | h('div', h('small', '> ', rootLink)), |
1254 | h('p', h('strong', 'resigned')) |
1255 | ], cb) |
1256 | }) |
1257 | |
1258 | var fen = c.fen && c.fen.length === 2 ? c.pgnMove : c.fen |
1259 | var game = parseChess(fen) |
1260 | var piece = game && lookupPiece(game.board, c.dest) |
1261 | var done = multicb({pluck: 1, spread: true}) |
1262 | self.link(self.c.root, done()) |
1263 | self.link(self.c.winner, done()) |
1264 | done(function (err, rootLink, winnerLink) { |
1265 | if (err) return cb(err) |
1266 | self.wrap([ |
1267 | h('div', h('small', '> ', rootLink)), |
1268 | h('p', |
1269 | 'moved ', (piece ? renderChessSymbol(piece) : ''), ' ', |
1270 | 'from ', c.orig, ' ', |
1271 | 'to ', c.dest |
1272 | ), |
1273 | h('p', |
1274 | h('strong', self.c.status), '. winner: ', h('strong', winnerLink)), |
1275 | self.chessBoard(game.board) |
1276 | ], cb) |
1277 | }) |
1278 | } |
1279 | |
1280 | RenderMsg.prototype.chessChat = function (cb) { |
1281 | var self = this |
1282 | self.link(self.c.root, function (err, rootLink) { |
1283 | if (err) return cb(err) |
1284 | self.wrap([ |
1285 | h('div', h('small', '> ', rootLink)), |
1286 | h('p', self.c.msg) |
1287 | ], cb) |
1288 | }) |
1289 | } |
1290 | |
1291 | RenderMsg.prototype.chessMove = function (cb) { |
1292 | if (this.opts.full) return this.chessMoveFull(cb) |
1293 | return this.chessMoveMini(cb) |
1294 | } |
1295 | |
1296 | RenderMsg.prototype.chessMoveFull = function (cb) { |
1297 | var self = this |
1298 | var c = self.c |
1299 | var fen = c.fen && c.fen.length === 2 ? c.pgnMove : c.fen |
1300 | var game = parseChess(fen) |
1301 | var piece = game && lookupPiece(game.board, c.dest) |
1302 | self.link(self.c.root, function (err, rootLink) { |
1303 | if (err) return cb(err) |
1304 | self.wrap([ |
1305 | h('div', h('small', '> ', rootLink)), |
1306 | h('p', |
1307 | // 'player ', (c.ply || ''), ' ', |
1308 | 'moved ', (piece ? renderChessSymbol(piece) : ''), ' ', |
1309 | 'from ', c.orig, ' ', |
1310 | 'to ', c.dest |
1311 | ), |
1312 | self.chessBoard(game.board) |
1313 | ], cb) |
1314 | }) |
1315 | } |
1316 | |
1317 | RenderMsg.prototype.chessMoveMini = function (cb) { |
1318 | var self = this |
1319 | var c = self.c |
1320 | var fen = c.fen && c.fen.length === 2 ? c.pgnMove : c.fen |
1321 | var game = parseChess(fen) |
1322 | var piece = game && lookupPiece(game.board, c.dest) |
1323 | self.link(self.c.root, function (err, rootLink) { |
1324 | if (err) return cb(err) |
1325 | self.wrapMini([ |
1326 | 'moved ', chessPieceName(piece), ' ', |
1327 | 'to ', c.dest |
1328 | ], cb) |
1329 | }) |
1330 | } |
1331 | |
1332 | RenderMsg.prototype.acmeChallengesHttp01 = function (cb) { |
1333 | var self = this |
1334 | self.wrapMini(h('span', |
1335 | 'serves ', |
1336 | hJoin(u.toArray(self.c.challenges).map(function (challenge) { |
1337 | return h('a', { |
1338 | href: 'http://' + challenge.domain + |
1339 | '/.well-known/acme-challenge/' + challenge.token, |
1340 | title: challenge.keyAuthorization, |
1341 | }, challenge.domain) |
1342 | }), ', ', ', and ') |
1343 | ), cb) |
1344 | } |
1345 | |
1346 | RenderMsg.prototype.bookclub = function (cb) { |
1347 | var self = this |
1348 | var props = self.c.common || self.c |
1349 | var images = u.toLinkArray(props.image || props.images) |
1350 | self.wrap(h('table', h('tr', |
1351 | h('td', |
1352 | images.map(function (image) { |
1353 | return h('a', {href: self.render.toUrl(image.link)}, h('img', { |
1354 | src: self.render.imageUrl(image.link), |
1355 | alt: image.name || ' ', |
1356 | width: 180, |
1357 | })) |
1358 | })), |
1359 | h('td', |
1360 | h('h4', props.title), |
1361 | props.authors ? |
1362 | h('p', h('em', props.authors)) |
1363 | : '', |
1364 | props.description |
1365 | ? h('div', {innerHTML: self.render.markdown(props.description)}) |
1366 | : '' |
1367 | ) |
1368 | )), cb) |
1369 | } |
1370 | |
1371 | RenderMsg.prototype.bookclubTitle = function (cb) { |
1372 | var props = this.c.common || this.c |
1373 | cb(null, props.title || 'book') |
1374 | } |
1375 | |
1376 | RenderMsg.prototype.sombrioPosition = function () { |
1377 | return h('span', '[' + this.c.position + ']') |
1378 | } |
1379 | |
1380 | RenderMsg.prototype.sombrioWall = function (cb) { |
1381 | var self = this |
1382 | self.wrapMini(h('span', |
1383 | self.sombrioPosition(), |
1384 | ' wall' |
1385 | ), cb) |
1386 | } |
1387 | |
1388 | RenderMsg.prototype.sombrioTombstone = function (cb) { |
1389 | var self = this |
1390 | self.wrapMini(h('span', |
1391 | self.sombrioPosition(), |
1392 | ' tombstone' |
1393 | ), cb) |
1394 | } |
1395 | |
1396 | RenderMsg.prototype.sombrioScore = function (cb) { |
1397 | var self = this |
1398 | self.wrapMini(h('span', |
1399 | 'scored ', |
1400 | h('ins', self.c.score) |
1401 | ), cb) |
1402 | } |
1403 |
Built with git-ssb-web