Files: 981d98437a12081fd3da461d2ec661d2a0f0933f / render.js
3697 bytesRaw
1 | var h = require('hyperscript') |
2 | var pull = require('pull-stream') |
3 | var human = require('human-time') |
4 | |
5 | var sbot = require('./scuttlebot') |
6 | var composer = require('./compose') |
7 | var tools = require('./tools') |
8 | |
9 | var config = require('./config')() |
10 | var id = require('./keys').id |
11 | |
12 | |
13 | module.exports = function (msg) { |
14 | var message = h('div.message#' + msg.key.substring(0, 44)) |
15 | if (msg.value.content.type == 'post') { |
16 | var opts = { |
17 | type: 'post', |
18 | branch: msg.key |
19 | } |
20 | var fallback = {} |
21 | |
22 | |
23 | if (msg.value.content.root) |
24 | opts.root = msg.value.content.root |
25 | else |
26 | opts.root = msg.key |
27 | |
28 | message.appendChild(tools.header(msg)) |
29 | |
30 | if (msg.value.content.root) |
31 | message.appendChild(h('span', 're: ', tools.messageLink(msg.value.content.root))) |
32 | |
33 | message.appendChild(h('div.message__body', tools.markdown(msg.value.content.text))) |
34 | |
35 | |
36 | pull( |
37 | sbot.query({query: [{$filter: {value: {content: {type: 'edit', original: msg.key}}}}], limit: 100, live: true}), |
38 | pull.drain(function (update) { |
39 | if (update.sync) { |
40 | //console.log('Waiting for new edits.') |
41 | } else { |
42 | var newMessage = h('div', tools.markdown(update.value.content.text)) |
43 | var latest = h('div.message__body', |
44 | tools.timestamp(update, {edited: true}), |
45 | newMessage |
46 | ) |
47 | message.replaceChild(latest, message.childNodes[message.childNodes.length - 2]) |
48 | fallback.messageText = update.value.content.text |
49 | opts.updated = update.key |
50 | opts.original = msg.key |
51 | } |
52 | }) |
53 | ) |
54 | |
55 | var buttons = h('div.buttons') |
56 | buttons.appendChild(h('button.btn', 'Reply', { |
57 | onclick: function () { |
58 | opts.type = 'post' |
59 | var r = message.childNodes.length - 1 |
60 | delete opts.updated |
61 | delete opts.original |
62 | delete fallback.messageText |
63 | fallback.buttons = message.childNodes[r] |
64 | var compose = h('div.message#re:' + msg.key.substring(0, 44), composer(opts, fallback)) |
65 | message.removeChild(message.childNodes[r]) |
66 | message.parentNode.insertBefore(compose, message.nextSibling) |
67 | } |
68 | })) |
69 | |
70 | if (msg.value.author == id) |
71 | buttons.appendChild(h('button.btn', 'Edit', { |
72 | onclick: function () { |
73 | opts.type = 'edit' |
74 | if (!fallback.messageText) |
75 | fallback.messageText = msg.value.content.text |
76 | |
77 | if (!opts.updated) |
78 | opts.updated = msg.key |
79 | opts.original = msg.key |
80 | |
81 | var r = message.childNodes.length - 1 |
82 | fallback.buttons = message.childNodes[r] |
83 | message.removeChild(message.childNodes[r]) |
84 | var compose = h('div#edit:' + msg.key.substring(0, 44), composer(opts, fallback)) |
85 | message.replaceChild(compose, message.lastElementChild) |
86 | } |
87 | })) |
88 | buttons.appendChild(tools.star(msg)) |
89 | message.appendChild(buttons) |
90 | return message |
91 | |
92 | } else if (msg.value.content.type == 'vote') { |
93 | if (msg.value.content.vote.value == 1) |
94 | var link = h('span', ' ', h('img.emoji', {src: config.emojiUrl + 'star.png'}), ' ', h('a', {href: msg.value.content.vote.link}, msg.value.content.vote.link.substring(0,16) + '...')) |
95 | else if (msg.value.content.vote.value == -1) |
96 | var link = h('span', ' ', h('img.emoji', {src: config.emojiUrl + 'stars.png'}), ' ', h('a', {href: msg.value.content.vote.link}, msg.value.content.vote.link.substring(0,16) + '...')) |
97 | message.appendChild(tools.mini(msg, link)) |
98 | return message |
99 | } else { |
100 | //message.appendChild(tools.header(msg)) |
101 | //message.appendChild(h('pre', tools.rawJSON(msg.value.content))) |
102 | //return message |
103 | return h('div.invisibleMessage') |
104 | } |
105 | } |
106 |
Built with git-ssb-web