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