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