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