Files: c68f5e54b067301117c9d60b66ddb801e6280484 / render.js
12707 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 | var avatar = require('./avatar') |
12 | var ssbAvatar = require('ssb-avatar') |
13 | |
14 | var ssbKeys = require('ssb-keys') |
15 | var keys = require('./keys') |
16 | |
17 | var diff = require('diff') |
18 | |
19 | function hash () { |
20 | return window.location.hash.substring(1) |
21 | } |
22 | |
23 | module.exports = function (msg) { |
24 | var message = h('div.message#' + msg.key.substring(0, 44)) |
25 | |
26 | if (!localStorage[msg.value.author]) |
27 | var cache = {mute: false} |
28 | else |
29 | var cache = JSON.parse(localStorage[msg.value.author]) |
30 | |
31 | if (cache.mute == true) { |
32 | var muted = h('span', ' muted') |
33 | message.appendChild(tools.mini(msg, muted)) |
34 | return message |
35 | } |
36 | |
37 | else if (msg.value.content.type == 'label'){ |
38 | var content = h('span', ' labeled ', tools.messageLink(msg.value.content.link), ' as ', h('mark', h('a', {href: '/#label/' + msg.value.content.label}, msg.value.content.label))) |
39 | message.appendChild(tools.mini(msg, content)) |
40 | return message |
41 | } |
42 | |
43 | else if (msg.value.content.type == 'queue') { |
44 | if (msg.value.content.queue == true) { |
45 | var content = h('span', ' added ', tools.messageLink(msg.value.content.message), ' to their ', h('a', {href: '#queue'}, 'queue')) |
46 | message.appendChild(tools.mini(msg, content)) |
47 | } |
48 | if (msg.value.content.queue == false) { |
49 | var content = h('span', ' removed ', tools.messageLink(msg.value.content.message), ' from their ', h('a', {href: '#queue'}, 'queue')) |
50 | message.appendChild(tools.mini(msg, content)) |
51 | |
52 | } |
53 | return message |
54 | } |
55 | |
56 | else if (msg.value.content.type == 'edit') { |
57 | message.appendChild(tools.header(msg)) |
58 | if (msg.value.content.text) { |
59 | var current = msg.value.content.text |
60 | sbot.get(msg.value.content.updated, function (err, updated) { |
61 | if (updated) { |
62 | // quick fix, need to decrypt messages if they're private |
63 | if (updated.content.text) { |
64 | fragment = document.createDocumentFragment() |
65 | var previous = updated.content.text |
66 | var ready = diff.diffWords(previous, current) |
67 | ready.forEach(function (part) { |
68 | if (part.added === true) { |
69 | color = 'blue' |
70 | } else if (part.removed === true) { |
71 | color = 'gray' |
72 | } else {color = '#333'} |
73 | var span = h('span') |
74 | span.style.color = color |
75 | if (part.removed === true) { |
76 | span.appendChild(h('del', document.createTextNode(part.value))) |
77 | } else { |
78 | span.appendChild(document.createTextNode(part.value)) |
79 | } |
80 | fragment.appendChild(span) |
81 | }) |
82 | message.appendChild(h('code', fragment)) |
83 | } |
84 | } |
85 | }) |
86 | } |
87 | return message |
88 | } |
89 | |
90 | else if (msg.value.content.type == 'scat_message') { |
91 | var src = hash() |
92 | if (src != 'backchannel') { |
93 | message.appendChild(h('button.btn.right', h('a', {href: '#backchannel'}, 'Chat'))) |
94 | } |
95 | message.appendChild(tools.mini(msg, ' ' + msg.value.content.text)) |
96 | return message |
97 | } |
98 | |
99 | |
100 | else if (msg.value.content.type == 'contact') { |
101 | if (msg.value.content.contact) { |
102 | var contact = h('a', {href: '#' + msg.value.content.contact}, avatar.name(msg.value.content.contact)) |
103 | } else { var contact = h('p', 'no contact named')} |
104 | |
105 | if (msg.value.content.following == true) { |
106 | var following = h('span', ' follows ', contact) |
107 | message.appendChild(tools.mini(msg, following)) |
108 | } |
109 | if (msg.value.content.following == false) { |
110 | var unfollowing = h('span', ' unfollows ', contact) |
111 | message.appendChild(tools.mini(msg, unfollowing)) |
112 | } |
113 | if (msg.value.content.blocking == true) { |
114 | var blocking = h('span', ' blocks ', contact) |
115 | message.appendChild(tools.mini(msg, blocking)) |
116 | } |
117 | if (msg.value.content.blocking == false) { |
118 | var unblocking = h('span', ' unblocks ', contact) |
119 | message.appendChild(tools.mini(msg, unblocking)) |
120 | } |
121 | return message |
122 | |
123 | } |
124 | |
125 | else if (msg.value.content.type == 'git-update') { |
126 | |
127 | var reponame = h('span', ' pushed to ', h('a', {href: '#' + msg.value.content.repo}, msg.value.content.repo)) |
128 | |
129 | message.appendChild(tools.mini(msg, reponame)) |
130 | |
131 | ssbAvatar(sbot, id, msg.value.content.repo, function (err, data) { |
132 | if (data) { |
133 | var actualname = h('span', ' pushed to ', h('a', {href: '#' + msg.value.content.repo}, '%' + data.name)) |
134 | reponame.parentNode.replaceChild(actualname, reponame) |
135 | } |
136 | }) |
137 | |
138 | var commits = h('ul') |
139 | |
140 | if (msg.value.content.commits) { |
141 | msg.value.content.commits.map(function (commit) { |
142 | commits.appendChild(h('li', h('code', commit.sha1), ' - ', commit.title)) |
143 | }) |
144 | } |
145 | |
146 | message.appendChild(commits) |
147 | |
148 | |
149 | var buttons = h('div.buttons') |
150 | buttons.appendChild(tools.star(msg)) |
151 | buttons.appendChild(tools.labeler(msg)) |
152 | message.appendChild(tools.getLabels(msg)) |
153 | message.appendChild(buttons) |
154 | |
155 | return message |
156 | |
157 | } |
158 | else if (msg.value.content.type == 'git-repo') { |
159 | |
160 | var reponame = h('span', ' created a git-ssb repo ', h('a', {href: '#' + msg.key}, msg.key)) |
161 | |
162 | message.appendChild(tools.mini(msg, reponame)) |
163 | |
164 | ssbAvatar(sbot, id, msg.key, function (err, data) { |
165 | if (data) |
166 | var actualname = h('span', ' created a git-ssb repo ', h('a', {href: '#' + msg.key}, '%' + data.name)) |
167 | reponame.parentNode.replaceChild(actualname, reponame) |
168 | }) |
169 | |
170 | var cloneurl = h('pre', 'git clone ssb://' + msg.key) |
171 | message.appendChild(cloneurl) |
172 | |
173 | var buttons = h('div.buttons') |
174 | |
175 | buttons.appendChild(tools.star(msg)) |
176 | buttons.appendChild(tools.labeler(msg)) |
177 | message.appendChild(tools.getLabels(msg)) |
178 | |
179 | message.appendChild(buttons) |
180 | |
181 | return message |
182 | } |
183 | |
184 | else if (msg.value.content.type == 'wiki') { |
185 | var fallback = {} |
186 | |
187 | var opts = { |
188 | type: 'wiki', |
189 | branch: msg.key |
190 | } |
191 | |
192 | if (msg.value.content.root) |
193 | opts.root = msg.value.content.root |
194 | else |
195 | opts.root = msg.key |
196 | |
197 | message.appendChild(tools.header(msg)) |
198 | |
199 | message.appendChild(h('div.message__body', tools.markdown(msg.value.content.text))) |
200 | |
201 | pull( |
202 | sbot.query({query: [{$filter: {value: {content: {type: 'edit', original: msg.key}}}}], limit: 100}), |
203 | pull.drain(function (update) { |
204 | if (update.sync) { |
205 | } else { |
206 | var newMessage = h('div', tools.markdown(update.value.content.text)) |
207 | var latest = h('div.message__body', |
208 | tools.timestamp(update, {edited: true}), |
209 | newMessage |
210 | ) |
211 | message.replaceChild(latest, message.childNodes[message.childNodes.length - 2]) |
212 | fallback.messageText = update.value.content.text |
213 | opts.updated = update.key |
214 | opts.original = msg.key |
215 | } |
216 | }) |
217 | ) |
218 | |
219 | var buttons = h('div.buttons') |
220 | |
221 | buttons.appendChild(h('button.btn', 'Edit wiki', { |
222 | onclick: function () { |
223 | opts.type = 'edit' |
224 | if (!fallback.messageText) |
225 | fallback.messageText = msg.value.content.text |
226 | |
227 | if (!opts.updated) |
228 | opts.updated = msg.key |
229 | opts.original = msg.key |
230 | |
231 | var r = message.childNodes.length - 1 |
232 | fallback.buttons = message.childNodes[r] |
233 | message.removeChild(message.childNodes[r]) |
234 | var compose = h('div#edit:' + msg.key.substring(0, 44), composer(opts, fallback)) |
235 | message.replaceChild(compose, message.lastElementChild) |
236 | } |
237 | })) |
238 | |
239 | buttons.appendChild(tools.star(msg)) |
240 | message.appendChild(buttons) |
241 | return message |
242 | |
243 | } else if (msg.value.content.type == 'post') { |
244 | var opts = { |
245 | type: 'post', |
246 | branch: msg.key |
247 | } |
248 | var fallback = {} |
249 | |
250 | |
251 | if (msg.value.content.root) |
252 | opts.root = msg.value.content.root |
253 | else |
254 | opts.root = msg.key |
255 | |
256 | message.appendChild(tools.header(msg)) |
257 | |
258 | if (msg.value.content.root) |
259 | message.appendChild(h('span', 're: ', tools.messageLink(msg.value.content.root))) |
260 | |
261 | message.appendChild(h('div.message__body', tools.markdown(msg.value.content.text))) |
262 | |
263 | pull( |
264 | sbot.query({query: [{$filter: {value: {content: {type: 'edit', original: msg.key}}}}], limit: 100}), |
265 | pull.drain(function (update) { |
266 | if (update.sync) { |
267 | } else { |
268 | var newMessage = h('div', tools.markdown(update.value.content.text)) |
269 | var latest = h('div.message__body', |
270 | tools.timestamp(update, {edited: true}), |
271 | newMessage |
272 | ) |
273 | message.replaceChild(latest, message.childNodes[message.childNodes.length - 2]) |
274 | fallback.messageText = update.value.content.text |
275 | opts.updated = update.key |
276 | opts.original = msg.key |
277 | } |
278 | }) |
279 | ) |
280 | |
281 | var name = avatar.name(msg.value.author) |
282 | |
283 | var buttons = h('div.buttons') |
284 | |
285 | buttons.appendChild(h('button.btn', 'Reply', { |
286 | onclick: function () { |
287 | opts.type = 'post' |
288 | opts.mentions = '[' + name.textContent + '](' + msg.value.author + ')' |
289 | if (msg.value.content.recps) { |
290 | opts.recps = msg.value.content.recps |
291 | } |
292 | var r = message.childNodes.length - 1 |
293 | delete opts.updated |
294 | delete opts.original |
295 | delete fallback.messageText |
296 | fallback.buttons = message.childNodes[r] |
297 | var compose = h('div.message#re:' + msg.key.substring(0, 44), composer(opts, fallback)) |
298 | message.removeChild(message.childNodes[r]) |
299 | message.parentNode.insertBefore(compose, message.nextSibling) |
300 | } |
301 | })) |
302 | |
303 | buttons.appendChild(h('button.btn', 'Boost', { |
304 | onclick: function () { |
305 | opts.type = 'post' |
306 | opts.mentions = '[' + name.textContent + '](' + msg.value.author + ')' |
307 | if (msg.value.content.recps) { |
308 | opts.recps = msg.value.content.recps |
309 | } |
310 | var r = message.childNodes.length - 1 |
311 | delete opts.updated |
312 | delete opts.original |
313 | delete fallback.messageText |
314 | opts.boostContent = msg.value.content.text |
315 | opts.boostKey = msg.key |
316 | opts.boostAuthor = msg.value.author |
317 | fallback.buttons = message.childNodes[r] |
318 | var compose = h('div.message#re:' + msg.key.substring(0, 44), composer(opts, fallback)) |
319 | message.removeChild(message.childNodes[r]) |
320 | message.parentNode.insertBefore(compose, message.nextSibling) |
321 | } |
322 | })) |
323 | |
324 | |
325 | if (msg.value.author == id) |
326 | buttons.appendChild(h('button.btn', 'Edit', { |
327 | onclick: function () { |
328 | opts.type = 'edit' |
329 | if (!fallback.messageText) |
330 | fallback.messageText = msg.value.content.text |
331 | |
332 | if (!opts.updated) |
333 | opts.updated = msg.key |
334 | opts.original = msg.key |
335 | |
336 | var r = message.childNodes.length - 1 |
337 | fallback.buttons = message.childNodes[r] |
338 | message.removeChild(message.childNodes[r]) |
339 | var compose = h('div#edit:' + msg.key.substring(0, 44), composer(opts, fallback)) |
340 | message.replaceChild(compose, message.lastElementChild) |
341 | } |
342 | })) |
343 | |
344 | |
345 | buttons.appendChild(tools.queueButton(msg)) |
346 | buttons.appendChild(tools.star(msg)) |
347 | buttons.appendChild(tools.labeler(msg)) |
348 | message.appendChild(tools.getLabels(msg)) |
349 | |
350 | message.appendChild(buttons) |
351 | return message |
352 | |
353 | } else if (msg.value.content.type == 'vote') { |
354 | if (msg.value.content.vote.value == 1) |
355 | var link = h('span', ' ', h('img.emoji', {src: config.emojiUrl + 'star.png'}), ' ', h('a', {href: '#' + msg.value.content.vote.link}, tools.messageLink(msg.value.content.vote.link))) |
356 | else if (msg.value.content.vote.value == -1) |
357 | var link = h('span', ' ', h('img.emoji', {src: config.emojiUrl + 'stars.png'}), ' ', h('a', {href: '#' + msg.value.content.vote.link}, tools.messageLink(msg.value.content.vote.link))) |
358 | message.appendChild(tools.mini(msg, link)) |
359 | return message |
360 | } else if (typeof msg.value.content === 'string') { |
361 | var unboxed = ssbKeys.unbox(msg.value.content, keys) |
362 | if (unboxed) { |
363 | msg.value.content = unboxed |
364 | msg.value.private = true |
365 | return module.exports(msg) |
366 | } else { |
367 | var privateMsg = h('span', ' sent a private message.') |
368 | message.appendChild(tools.mini(msg, privateMsg)) |
369 | return message |
370 | } |
371 | } else { |
372 | |
373 | //FULL FALLBACK |
374 | message.appendChild(tools.header(msg)) |
375 | message.appendChild(h('pre', tools.rawJSON(msg.value))) |
376 | |
377 | //MINI FALLBACK |
378 | //var fallback = h('span', ' ' + msg.value.content.type) |
379 | //message.appendChild(tools.mini(msg, fallback)) |
380 | return h('div', message) |
381 | } |
382 | } |
383 |
Built with git-ssb-web