Files: a8221d7410bb00bbd9d418fea1249dc77aa1f981 / render.js
10982 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 | message.appendChild(cloneurl) |
115 | |
116 | var commits = h('ul') |
117 | //if (msg.value.content.commits[0]) { |
118 | if (msg.value.content.commits) { |
119 | msg.value.content.commits.map(function (commit) { |
120 | commits.appendChild(h('li', h('code', commit.sha1), ' - ', commit.title)) |
121 | }) |
122 | |
123 | } |
124 | |
125 | message.appendChild(commits) |
126 | |
127 | return message |
128 | |
129 | } |
130 | else if (msg.value.content.type == 'git-repo') { |
131 | message.appendChild(tools.header(msg)) |
132 | |
133 | var reponame = h('p', 'git-ssb repo ', h('a', {href: '#' + msg.key}, msg.key)) |
134 | |
135 | message.appendChild(reponame) |
136 | |
137 | ssbAvatar(sbot, id, msg.key, function (err, data) { |
138 | if (data) |
139 | var actualname = h('p', 'git-ssb repo ', h('a', {href: '#' + msg.key}, '%' + data.name)) |
140 | reponame.parentNode.replaceChild(actualname, reponame) |
141 | }) |
142 | |
143 | var cloneurl = h('pre', 'git clone ssb://' + msg.key) |
144 | message.appendChild(cloneurl) |
145 | return message |
146 | } |
147 | |
148 | else if (msg.value.content.type == 'wiki') { |
149 | var fallback = {} |
150 | |
151 | var opts = { |
152 | type: 'wiki', |
153 | branch: msg.key |
154 | } |
155 | |
156 | if (msg.value.content.root) |
157 | opts.root = msg.value.content.root |
158 | else |
159 | opts.root = msg.key |
160 | |
161 | message.appendChild(tools.header(msg)) |
162 | |
163 | message.appendChild(h('div.message__body', tools.markdown(msg.value.content.text))) |
164 | |
165 | pull( |
166 | sbot.query({query: [{$filter: {value: {content: {type: 'edit', original: msg.key}}}}], limit: 100}), |
167 | pull.drain(function (update) { |
168 | if (update.sync) { |
169 | } else { |
170 | var newMessage = h('div', tools.markdown(update.value.content.text)) |
171 | var latest = h('div.message__body', |
172 | tools.timestamp(update, {edited: true}), |
173 | newMessage |
174 | ) |
175 | message.replaceChild(latest, message.childNodes[message.childNodes.length - 2]) |
176 | fallback.messageText = update.value.content.text |
177 | opts.updated = update.key |
178 | opts.original = msg.key |
179 | } |
180 | }) |
181 | ) |
182 | |
183 | var buttons = h('div.buttons') |
184 | |
185 | buttons.appendChild(h('button.btn', 'Edit wiki', { |
186 | onclick: function () { |
187 | opts.type = 'edit' |
188 | if (!fallback.messageText) |
189 | fallback.messageText = msg.value.content.text |
190 | |
191 | if (!opts.updated) |
192 | opts.updated = msg.key |
193 | opts.original = msg.key |
194 | |
195 | var r = message.childNodes.length - 1 |
196 | fallback.buttons = message.childNodes[r] |
197 | message.removeChild(message.childNodes[r]) |
198 | var compose = h('div#edit:' + msg.key.substring(0, 44), composer(opts, fallback)) |
199 | message.replaceChild(compose, message.lastElementChild) |
200 | } |
201 | })) |
202 | |
203 | buttons.appendChild(tools.star(msg)) |
204 | message.appendChild(buttons) |
205 | return message |
206 | |
207 | } else if (msg.value.content.type == 'post') { |
208 | var opts = { |
209 | type: 'post', |
210 | branch: msg.key |
211 | } |
212 | var fallback = {} |
213 | |
214 | |
215 | if (msg.value.content.root) |
216 | opts.root = msg.value.content.root |
217 | else |
218 | opts.root = msg.key |
219 | |
220 | message.appendChild(tools.header(msg)) |
221 | |
222 | if (msg.value.content.root) |
223 | message.appendChild(h('span', 're: ', tools.messageLink(msg.value.content.root))) |
224 | |
225 | message.appendChild(h('div.message__body', tools.markdown(msg.value.content.text))) |
226 | |
227 | pull( |
228 | sbot.query({query: [{$filter: {value: {content: {type: 'edit', original: msg.key}}}}], limit: 100}), |
229 | pull.drain(function (update) { |
230 | if (update.sync) { |
231 | } else { |
232 | var newMessage = h('div', tools.markdown(update.value.content.text)) |
233 | var latest = h('div.message__body', |
234 | tools.timestamp(update, {edited: true}), |
235 | newMessage |
236 | ) |
237 | message.replaceChild(latest, message.childNodes[message.childNodes.length - 2]) |
238 | fallback.messageText = update.value.content.text |
239 | opts.updated = update.key |
240 | opts.original = msg.key |
241 | } |
242 | }) |
243 | ) |
244 | |
245 | var name = avatar.name(msg.value.author) |
246 | |
247 | var buttons = h('div.buttons') |
248 | |
249 | buttons.appendChild(h('button.btn', 'Reply', { |
250 | onclick: function () { |
251 | opts.type = 'post' |
252 | opts.mentions = '[' + name.textContent + '](' + msg.value.author + ')' |
253 | if (msg.value.content.recps) { |
254 | opts.recps = msg.value.content.recps |
255 | } |
256 | var r = message.childNodes.length - 1 |
257 | delete opts.updated |
258 | delete opts.original |
259 | delete fallback.messageText |
260 | fallback.buttons = message.childNodes[r] |
261 | var compose = h('div.message#re:' + msg.key.substring(0, 44), composer(opts, fallback)) |
262 | message.removeChild(message.childNodes[r]) |
263 | message.parentNode.insertBefore(compose, message.nextSibling) |
264 | } |
265 | })) |
266 | |
267 | buttons.appendChild(h('button.btn', 'Boost', { |
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 | opts.boostContent = msg.value.content.text |
279 | opts.boostKey = msg.key |
280 | opts.boostAuthor = msg.value.author |
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 | |
289 | if (msg.value.author == id) |
290 | buttons.appendChild(h('button.btn', 'Edit', { |
291 | onclick: function () { |
292 | opts.type = 'edit' |
293 | if (!fallback.messageText) |
294 | fallback.messageText = msg.value.content.text |
295 | |
296 | if (!opts.updated) |
297 | opts.updated = msg.key |
298 | opts.original = msg.key |
299 | |
300 | var r = message.childNodes.length - 1 |
301 | fallback.buttons = message.childNodes[r] |
302 | message.removeChild(message.childNodes[r]) |
303 | var compose = h('div#edit:' + msg.key.substring(0, 44), composer(opts, fallback)) |
304 | message.replaceChild(compose, message.lastElementChild) |
305 | } |
306 | })) |
307 | |
308 | buttons.appendChild(tools.star(msg)) |
309 | message.appendChild(buttons) |
310 | return message |
311 | |
312 | } else if (msg.value.content.type == 'vote') { |
313 | if (msg.value.content.vote.value == 1) |
314 | 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))) |
315 | else if (msg.value.content.vote.value == -1) |
316 | 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))) |
317 | message.appendChild(tools.mini(msg, link)) |
318 | return message |
319 | } else if (typeof msg.value.content === 'string') { |
320 | var privateMsg = h('span', ' sent a private message.') |
321 | message.appendChild(tools.mini(msg, privateMsg)) |
322 | return message |
323 | } else { |
324 | |
325 | //FULL FALLBACK |
326 | //message.appendChild(tools.header(msg)) |
327 | //message.appendChild(h('pre', tools.rawJSON(msg.value.content))) |
328 | |
329 | //MINI FALLBACK |
330 | var fallback = h('span', ' ' + msg.value.content.type) |
331 | message.appendChild(tools.mini(msg, fallback)) |
332 | return message |
333 | } |
334 | } |
335 |
Built with git-ssb-web