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