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