Files: 73e7d576ed222947d6e6508457bf26c7868b1786 / render.js
13469 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 | else if (msg.value.content.type == 'contact') { |
99 | if (msg.value.content.contact) { |
100 | var contact = h('a', {href: '#' + msg.value.content.contact}, avatar.name(msg.value.content.contact)) |
101 | } else { var contact = h('p', 'no contact named')} |
102 | |
103 | if (msg.value.content.following == true) { |
104 | var following = h('span', ' follows ', contact) |
105 | message.appendChild(tools.mini(msg, following)) |
106 | } |
107 | if (msg.value.content.following == false) { |
108 | var unfollowing = h('span', ' unfollows ', contact) |
109 | message.appendChild(tools.mini(msg, unfollowing)) |
110 | } |
111 | if (msg.value.content.blocking == true) { |
112 | var blocking = h('span', ' blocks ', contact) |
113 | message.appendChild(tools.mini(msg, blocking)) |
114 | } |
115 | if (msg.value.content.blocking == false) { |
116 | var unblocking = h('span', ' unblocks ', contact) |
117 | message.appendChild(tools.mini(msg, unblocking)) |
118 | } |
119 | return message |
120 | |
121 | } |
122 | |
123 | else if (msg.value.content.type == 'git-update') { |
124 | |
125 | message.appendChild(tools.header(msg)) |
126 | |
127 | var reponame = h('p', 'pushed to ', h('a', {href: '#' + msg.value.content.repo}, msg.value.content.repo)) |
128 | |
129 | var cloneurl = h('pre', 'git clone ssb://' + msg.value.content.repo) |
130 | |
131 | message.appendChild(reponame) |
132 | |
133 | |
134 | ssbAvatar(sbot, id, msg.value.content.repo, function (err, data) { |
135 | if (data) { |
136 | var actualname = h('p', 'pushed to ', h('a', {href: '#' + msg.value.content.repo}, '%' + data.name)) |
137 | reponame.parentNode.replaceChild(actualname, reponame) |
138 | } |
139 | }) |
140 | |
141 | message.appendChild(cloneurl) |
142 | |
143 | var commits = h('ul') |
144 | //if (msg.value.content.commits[0]) { |
145 | if (msg.value.content.commits) { |
146 | msg.value.content.commits.map(function (commit) { |
147 | commits.appendChild(h('li', h('code', commit.sha1), ' - ', commit.title)) |
148 | }) |
149 | |
150 | } |
151 | |
152 | message.appendChild(commits) |
153 | |
154 | return message |
155 | |
156 | } |
157 | else if (msg.value.content.type == 'git-repo') { |
158 | message.appendChild(tools.header(msg)) |
159 | |
160 | var reponame = h('p', 'git-ssb repo ', h('a', {href: '#' + msg.key}, msg.key)) |
161 | |
162 | message.appendChild(reponame) |
163 | |
164 | ssbAvatar(sbot, id, msg.key, function (err, data) { |
165 | if (data) |
166 | var actualname = h('p', '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 | return message |
173 | } |
174 | |
175 | else if (msg.value.content.type == 'wiki') { |
176 | var fallback = {} |
177 | |
178 | var opts = { |
179 | type: 'wiki', |
180 | branch: msg.key |
181 | } |
182 | |
183 | if (msg.value.content.root) |
184 | opts.root = msg.value.content.root |
185 | else |
186 | opts.root = msg.key |
187 | |
188 | message.appendChild(tools.header(msg)) |
189 | |
190 | message.appendChild(h('div.message__body', tools.markdown(msg.value.content.text))) |
191 | |
192 | pull( |
193 | sbot.query({query: [{$filter: {value: {content: {type: 'edit', original: msg.key}}}}], limit: 100}), |
194 | pull.drain(function (update) { |
195 | if (update.sync) { |
196 | } else { |
197 | var newMessage = h('div', tools.markdown(update.value.content.text)) |
198 | var latest = h('div.message__body', |
199 | tools.timestamp(update, {edited: true}), |
200 | newMessage |
201 | ) |
202 | message.replaceChild(latest, message.childNodes[message.childNodes.length - 2]) |
203 | fallback.messageText = update.value.content.text |
204 | opts.updated = update.key |
205 | opts.original = msg.key |
206 | } |
207 | }) |
208 | ) |
209 | |
210 | var buttons = h('div.buttons') |
211 | |
212 | buttons.appendChild(h('button.btn', 'Edit wiki', { |
213 | onclick: function () { |
214 | opts.type = 'edit' |
215 | if (!fallback.messageText) |
216 | fallback.messageText = msg.value.content.text |
217 | |
218 | if (!opts.updated) |
219 | opts.updated = msg.key |
220 | opts.original = msg.key |
221 | |
222 | var r = message.childNodes.length - 1 |
223 | fallback.buttons = message.childNodes[r] |
224 | message.removeChild(message.childNodes[r]) |
225 | var compose = h('div#edit:' + msg.key.substring(0, 44), composer(opts, fallback)) |
226 | message.replaceChild(compose, message.lastElementChild) |
227 | } |
228 | })) |
229 | |
230 | buttons.appendChild(tools.star(msg)) |
231 | message.appendChild(buttons) |
232 | return message |
233 | |
234 | } else if (msg.value.content.type == 'post') { |
235 | var opts = { |
236 | type: 'post', |
237 | branch: msg.key |
238 | } |
239 | var fallback = {} |
240 | |
241 | |
242 | if (msg.value.content.root) |
243 | opts.root = msg.value.content.root |
244 | else |
245 | opts.root = msg.key |
246 | |
247 | message.appendChild(tools.header(msg)) |
248 | |
249 | if (msg.value.content.root) |
250 | message.appendChild(h('span', 're: ', tools.messageLink(msg.value.content.root))) |
251 | |
252 | message.appendChild(h('div.message__body', tools.markdown(msg.value.content.text))) |
253 | |
254 | pull( |
255 | sbot.query({query: [{$filter: {value: {content: {type: 'edit', original: msg.key}}}}], limit: 100}), |
256 | pull.drain(function (update) { |
257 | if (update.sync) { |
258 | } else { |
259 | var newMessage = h('div', tools.markdown(update.value.content.text)) |
260 | var latest = h('div.message__body', |
261 | tools.timestamp(update, {edited: true}), |
262 | newMessage |
263 | ) |
264 | message.replaceChild(latest, message.childNodes[message.childNodes.length - 2]) |
265 | fallback.messageText = update.value.content.text |
266 | opts.updated = update.key |
267 | opts.original = msg.key |
268 | } |
269 | }) |
270 | ) |
271 | |
272 | pull( |
273 | sbot.query({query: [{$filter: {value: { content: {type: 'label', link: msg.key}}}}], limit: 100, live: true}), |
274 | pull.drain(function (labels){ |
275 | console.log(labels) |
276 | if (labels.value){ |
277 | message.appendChild(h('span', ' ', h('mark', h('a', {href: '/#label/' + labels.value.content.label}, labels.value.content.label)))) |
278 | |
279 | } |
280 | }) |
281 | ) |
282 | |
283 | var name = avatar.name(msg.value.author) |
284 | |
285 | var buttons = h('div.buttons') |
286 | |
287 | buttons.appendChild(h('button.btn', 'Reply', { |
288 | onclick: function () { |
289 | opts.type = 'post' |
290 | opts.mentions = '[' + name.textContent + '](' + msg.value.author + ')' |
291 | if (msg.value.content.recps) { |
292 | opts.recps = msg.value.content.recps |
293 | } |
294 | var r = message.childNodes.length - 1 |
295 | delete opts.updated |
296 | delete opts.original |
297 | delete fallback.messageText |
298 | fallback.buttons = message.childNodes[r] |
299 | var compose = h('div.message#re:' + msg.key.substring(0, 44), composer(opts, fallback)) |
300 | message.removeChild(message.childNodes[r]) |
301 | message.parentNode.insertBefore(compose, message.nextSibling) |
302 | } |
303 | })) |
304 | |
305 | buttons.appendChild(h('button.btn', 'Boost', { |
306 | onclick: function () { |
307 | opts.type = 'post' |
308 | opts.mentions = '[' + name.textContent + '](' + msg.value.author + ')' |
309 | if (msg.value.content.recps) { |
310 | opts.recps = msg.value.content.recps |
311 | } |
312 | var r = message.childNodes.length - 1 |
313 | delete opts.updated |
314 | delete opts.original |
315 | delete fallback.messageText |
316 | opts.boostContent = msg.value.content.text |
317 | opts.boostKey = msg.key |
318 | opts.boostAuthor = msg.value.author |
319 | fallback.buttons = message.childNodes[r] |
320 | var compose = h('div.message#re:' + msg.key.substring(0, 44), composer(opts, fallback)) |
321 | message.removeChild(message.childNodes[r]) |
322 | message.parentNode.insertBefore(compose, message.nextSibling) |
323 | } |
324 | })) |
325 | |
326 | |
327 | if (msg.value.author == id) |
328 | buttons.appendChild(h('button.btn', 'Edit', { |
329 | onclick: function () { |
330 | opts.type = 'edit' |
331 | if (!fallback.messageText) |
332 | fallback.messageText = msg.value.content.text |
333 | |
334 | if (!opts.updated) |
335 | opts.updated = msg.key |
336 | opts.original = msg.key |
337 | |
338 | var r = message.childNodes.length - 1 |
339 | fallback.buttons = message.childNodes[r] |
340 | message.removeChild(message.childNodes[r]) |
341 | var compose = h('div#edit:' + msg.key.substring(0, 44), composer(opts, fallback)) |
342 | message.replaceChild(compose, message.lastElementChild) |
343 | } |
344 | })) |
345 | |
346 | |
347 | var inputter = h('input', {placeholder: 'Add a label to this post ie art, books, new'}) |
348 | |
349 | var labeler = h('div', |
350 | inputter, |
351 | h('button.btn', 'Publish label', { |
352 | onclick: function () { |
353 | var post = {} |
354 | post.type = 'label', |
355 | post.label = inputter.value, |
356 | post.link = msg.key |
357 | |
358 | sbot.publish(post, function (err, msg){ |
359 | console.log(msg) |
360 | labeler.parentNode.replaceChild(buttons, labeler) |
361 | }) |
362 | } |
363 | }) |
364 | ) |
365 | |
366 | var labels = h('button.btn', 'Add label', { |
367 | onclick: function () { |
368 | buttons.parentNode.replaceChild(labeler, buttons) |
369 | } |
370 | }) |
371 | |
372 | buttons.appendChild(labels) |
373 | buttons.appendChild(tools.queueButton(msg)) |
374 | buttons.appendChild(tools.star(msg)) |
375 | message.appendChild(buttons) |
376 | return message |
377 | |
378 | } else if (msg.value.content.type == 'vote') { |
379 | if (msg.value.content.vote.value == 1) |
380 | 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))) |
381 | else if (msg.value.content.vote.value == -1) |
382 | 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))) |
383 | message.appendChild(tools.mini(msg, link)) |
384 | return message |
385 | } else if (typeof msg.value.content === 'string') { |
386 | var unboxed = ssbKeys.unbox(msg.value.content, keys) |
387 | if (unboxed) { |
388 | msg.value.content = unboxed |
389 | msg.value.private = true |
390 | return module.exports(msg) |
391 | } else { |
392 | var privateMsg = h('span', ' sent a private message.') |
393 | message.appendChild(tools.mini(msg, privateMsg)) |
394 | return message |
395 | } |
396 | } else { |
397 | |
398 | //FULL FALLBACK |
399 | message.appendChild(tools.header(msg)) |
400 | message.appendChild(h('pre', tools.rawJSON(msg.value))) |
401 | |
402 | //MINI FALLBACK |
403 | //var fallback = h('span', ' ' + msg.value.content.type) |
404 | //message.appendChild(tools.mini(msg, fallback)) |
405 | return h('div', message) |
406 | } |
407 | } |
408 |
Built with git-ssb-web