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