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