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