git ssb

2+

ev / mvd



Tree: d6c90ba12886129623834c81dfcd6e02e6ec8d5d

Files: d6c90ba12886129623834c81dfcd6e02e6ec8d5d / render.js

14191 bytesRaw
1var h = require('hyperscript')
2var pull = require('pull-stream')
3var human = require('human-time')
4
5var sbot = require('./scuttlebot')
6var composer = require('./compose')
7var tools = require('./tools')
8
9var config = require('./config')()
10var id = require('./keys').id
11var avatar = require('./avatar')
12var ssbAvatar = require('ssb-avatar')
13
14var ssbKeys = require('ssb-keys')
15var keys = require('./keys')
16
17var diff = require('diff')
18
19function hash () {
20 return window.location.hash.substring(1)
21}
22
23module.exports = function (msg) {
24 var message = h('div.message#' + msg.key.substring(0, 44))
25
26 if (!localStorage[msg.value.author])
27 var cache = {mute: false}
28 else
29 var cache = JSON.parse(localStorage[msg.value.author])
30
31 if (cache.mute == true) {
32 var muted = h('span', ' muted')
33 message.appendChild(tools.mini(msg, muted))
34 return message
35 }
36
37 else if (msg.value.content.type == 'about') {
38 if (msg.value.content.image) {
39 var image = h('span.avatar--small',
40 ' identified ',
41 h('a', {href: '#' + msg.value.content.about}, avatar.cachedName(msg.value.content.about)),
42 ' as ',
43 h('img', {src: config.blobsUrl + msg.value.content.image.link})
44 )
45 message.appendChild(tools.mini(msg, image))
46 }
47 if (msg.value.content.name) {
48 var name = h('span',
49 ' identified ',
50 h('a', {href: '#' + msg.value.content.about}, avatar.cachedName(msg.value.content.about)),
51 ' as ', msg.value.content.name
52 )
53 message.appendChild(tools.mini(msg, name))
54 }
55
56 return message
57 }
58
59 else if (msg.value.content.type == 'label'){
60 var content = h('span', ' labeled ', tools.messageLink(msg.value.content.link), ' as ', h('mark', h('a', {href: '/#label/' + msg.value.content.label}, msg.value.content.label)))
61 message.appendChild(tools.mini(msg, content))
62 return message
63 }
64
65 else if (msg.value.content.type == 'queue') {
66 if (msg.value.content.queue == true) {
67 var content = h('span', ' added ', tools.messageLink(msg.value.content.message), ' to their ', h('a', {href: '#queue'}, 'queue'))
68 message.appendChild(tools.mini(msg, content))
69 }
70 if (msg.value.content.queue == false) {
71 var content = h('span', ' removed ', tools.messageLink(msg.value.content.message), ' from their ', h('a', {href: '#queue'}, 'queue'))
72 message.appendChild(tools.mini(msg, content))
73
74 }
75 return message
76 }
77
78 else if (msg.value.content.type == 'edit') {
79 message.appendChild(tools.header(msg))
80 if (msg.value.content.text) {
81 var current = msg.value.content.text
82 sbot.get(msg.value.content.updated, function (err, updated) {
83 if (updated) {
84 // quick fix, need to decrypt messages if they're private
85 if (updated.content.text) {
86 fragment = document.createDocumentFragment()
87 var previous = updated.content.text
88 var ready = diff.diffWords(previous, current)
89 ready.forEach(function (part) {
90 if (part.added === true) {
91 color = 'blue'
92 } else if (part.removed === true) {
93 color = 'gray'
94 } else {color = '#333'}
95 var span = h('span')
96 span.style.color = color
97 if (part.removed === true) {
98 span.appendChild(h('del', document.createTextNode(part.value)))
99 } else {
100 span.appendChild(document.createTextNode(part.value))
101 }
102 fragment.appendChild(span)
103 })
104 message.appendChild(h('code', fragment))
105 }
106 }
107 })
108 }
109 return message
110 }
111
112 else if (msg.value.content.type == 'scat_message') {
113 var src = hash()
114 if (src != 'backchannel') {
115 message.appendChild(h('button.btn.right', h('a', {href: '#backchannel'}, 'Chat')))
116 }
117 message.appendChild(tools.mini(msg, ' ' + msg.value.content.text))
118 return message
119 }
120 else if (msg.value.content.type == 'contact') {
121 if (msg.value.content.contact) {
122 var contact = h('a', {href: '#' + msg.value.content.contact}, avatar.name(msg.value.content.contact))
123 } else { var contact = h('p', 'no contact named')}
124
125 if (msg.value.content.following == true) {
126 var following = h('span', ' follows ', contact)
127 message.appendChild(tools.mini(msg, following))
128 }
129 if (msg.value.content.following == false) {
130 var unfollowing = h('span', ' unfollows ', contact)
131 message.appendChild(tools.mini(msg, unfollowing))
132 }
133 if (msg.value.content.blocking == true) {
134 var blocking = h('span', ' blocks ', contact)
135 message.appendChild(tools.mini(msg, blocking))
136 }
137 if (msg.value.content.blocking == false) {
138 var unblocking = h('span', ' unblocks ', contact)
139 message.appendChild(tools.mini(msg, unblocking))
140 }
141 return message
142
143 }
144
145 else if (msg.value.content.type == 'git-update') {
146
147 message.appendChild(tools.header(msg))
148
149 var reponame = h('p', 'pushed to ', h('a', {href: '#' + msg.value.content.repo}, msg.value.content.repo))
150
151 var cloneurl = h('pre', 'git clone ssb://' + msg.value.content.repo)
152
153 message.appendChild(reponame)
154
155
156 ssbAvatar(sbot, id, msg.value.content.repo, function (err, data) {
157 if (data) {
158 var actualname = h('p', 'pushed to ', h('a', {href: '#' + msg.value.content.repo}, '%' + data.name))
159 reponame.parentNode.replaceChild(actualname, reponame)
160 }
161 })
162
163 message.appendChild(cloneurl)
164
165 var commits = h('ul')
166 //if (msg.value.content.commits[0]) {
167 if (msg.value.content.commits) {
168 msg.value.content.commits.map(function (commit) {
169 commits.appendChild(h('li', h('code', commit.sha1), ' - ', commit.title))
170 })
171
172 }
173
174 message.appendChild(commits)
175
176 return message
177
178 }
179 else if (msg.value.content.type == 'git-repo') {
180 message.appendChild(tools.header(msg))
181
182 var reponame = h('p', 'git-ssb repo ', h('a', {href: '#' + msg.key}, msg.key))
183
184 message.appendChild(reponame)
185
186 ssbAvatar(sbot, id, msg.key, function (err, data) {
187 if (data)
188 var actualname = h('p', 'git-ssb repo ', h('a', {href: '#' + msg.key}, '%' + data.name))
189 reponame.parentNode.replaceChild(actualname, reponame)
190 })
191
192 var cloneurl = h('pre', 'git clone ssb://' + msg.key)
193 message.appendChild(cloneurl)
194 return message
195 }
196
197 else if (msg.value.content.type == 'wiki') {
198 var fallback = {}
199
200 var opts = {
201 type: 'wiki',
202 branch: msg.key
203 }
204
205 if (msg.value.content.root)
206 opts.root = msg.value.content.root
207 else
208 opts.root = msg.key
209
210 message.appendChild(tools.header(msg))
211
212 message.appendChild(h('div.message__body', tools.markdown(msg.value.content.text)))
213
214 pull(
215 sbot.query({query: [{$filter: {value: {content: {type: 'edit', original: msg.key}}}}], limit: 100}),
216 pull.drain(function (update) {
217 if (update.sync) {
218 } else {
219 var newMessage = h('div', tools.markdown(update.value.content.text))
220 var latest = h('div.message__body',
221 tools.timestamp(update, {edited: true}),
222 newMessage
223 )
224 message.replaceChild(latest, message.childNodes[message.childNodes.length - 2])
225 fallback.messageText = update.value.content.text
226 opts.updated = update.key
227 opts.original = msg.key
228 }
229 })
230 )
231
232 var buttons = h('div.buttons')
233
234 buttons.appendChild(h('button.btn', 'Edit wiki', {
235 onclick: function () {
236 opts.type = 'edit'
237 if (!fallback.messageText)
238 fallback.messageText = msg.value.content.text
239
240 if (!opts.updated)
241 opts.updated = msg.key
242 opts.original = msg.key
243
244 var r = message.childNodes.length - 1
245 fallback.buttons = message.childNodes[r]
246 message.removeChild(message.childNodes[r])
247 var compose = h('div#edit:' + msg.key.substring(0, 44), composer(opts, fallback))
248 message.replaceChild(compose, message.lastElementChild)
249 }
250 }))
251
252 buttons.appendChild(tools.star(msg))
253 message.appendChild(buttons)
254 return message
255
256 } else if (msg.value.content.type == 'post') {
257 var opts = {
258 type: 'post',
259 branch: msg.key
260 }
261 var fallback = {}
262
263
264 if (msg.value.content.root)
265 opts.root = msg.value.content.root
266 else
267 opts.root = msg.key
268
269 message.appendChild(tools.header(msg))
270
271 if (msg.value.content.root)
272 message.appendChild(h('span', 're: ', tools.messageLink(msg.value.content.root)))
273
274 message.appendChild(h('div.message__body', tools.markdown(msg.value.content.text)))
275
276 pull(
277 sbot.query({query: [{$filter: {value: {content: {type: 'edit', original: msg.key}}}}], limit: 100}),
278 pull.drain(function (update) {
279 if (update.sync) {
280 } else {
281 var newMessage = h('div', tools.markdown(update.value.content.text))
282 var latest = h('div.message__body',
283 tools.timestamp(update, {edited: true}),
284 newMessage
285 )
286 message.replaceChild(latest, message.childNodes[message.childNodes.length - 2])
287 fallback.messageText = update.value.content.text
288 opts.updated = update.key
289 opts.original = msg.key
290 }
291 })
292 )
293
294 pull(
295 sbot.query({query: [{$filter: {value: { content: {type: 'label', link: msg.key}}}}], limit: 100, live: true}),
296 pull.drain(function (labels){
297 console.log(labels)
298 if (labels.value){
299 message.appendChild(h('span', ' ', h('mark', h('a', {href: '/#label/' + labels.value.content.label}, labels.value.content.label))))
300
301 }
302 })
303 )
304
305 var name = avatar.name(msg.value.author)
306
307 var buttons = h('div.buttons')
308
309 buttons.appendChild(h('button.btn', 'Reply', {
310 onclick: function () {
311 opts.type = 'post'
312 opts.mentions = '[' + name.textContent + '](' + msg.value.author + ')'
313 if (msg.value.content.recps) {
314 opts.recps = msg.value.content.recps
315 }
316 var r = message.childNodes.length - 1
317 delete opts.updated
318 delete opts.original
319 delete fallback.messageText
320 fallback.buttons = message.childNodes[r]
321 var compose = h('div.message#re:' + msg.key.substring(0, 44), composer(opts, fallback))
322 message.removeChild(message.childNodes[r])
323 message.parentNode.insertBefore(compose, message.nextSibling)
324 }
325 }))
326
327 buttons.appendChild(h('button.btn', 'Boost', {
328 onclick: function () {
329 opts.type = 'post'
330 opts.mentions = '[' + name.textContent + '](' + msg.value.author + ')'
331 if (msg.value.content.recps) {
332 opts.recps = msg.value.content.recps
333 }
334 var r = message.childNodes.length - 1
335 delete opts.updated
336 delete opts.original
337 delete fallback.messageText
338 opts.boostContent = msg.value.content.text
339 opts.boostKey = msg.key
340 opts.boostAuthor = msg.value.author
341 fallback.buttons = message.childNodes[r]
342 var compose = h('div.message#re:' + msg.key.substring(0, 44), composer(opts, fallback))
343 message.removeChild(message.childNodes[r])
344 message.parentNode.insertBefore(compose, message.nextSibling)
345 }
346 }))
347
348
349 if (msg.value.author == id)
350 buttons.appendChild(h('button.btn', 'Edit', {
351 onclick: function () {
352 opts.type = 'edit'
353 if (!fallback.messageText)
354 fallback.messageText = msg.value.content.text
355
356 if (!opts.updated)
357 opts.updated = msg.key
358 opts.original = msg.key
359
360 var r = message.childNodes.length - 1
361 fallback.buttons = message.childNodes[r]
362 message.removeChild(message.childNodes[r])
363 var compose = h('div#edit:' + msg.key.substring(0, 44), composer(opts, fallback))
364 message.replaceChild(compose, message.lastElementChild)
365 }
366 }))
367
368
369 var inputter = h('input', {placeholder: 'Add a label to this post ie art, books, new'})
370
371 var labeler = h('div',
372 inputter,
373 h('button.btn', 'Publish label', {
374 onclick: function () {
375 var post = {}
376 post.type = 'label',
377 post.label = inputter.value,
378 post.link = msg.key
379
380 sbot.publish(post, function (err, msg){
381 console.log(msg)
382 labeler.parentNode.replaceChild(buttons, labeler)
383 })
384 }
385 })
386 )
387
388 var labels = h('button.btn', 'Add label', {
389 onclick: function () {
390 buttons.parentNode.replaceChild(labeler, buttons)
391 }
392 })
393
394 buttons.appendChild(labels)
395 buttons.appendChild(tools.queueButton(msg))
396 buttons.appendChild(tools.star(msg))
397 message.appendChild(buttons)
398 return message
399
400 } else if (msg.value.content.type == 'vote') {
401 if (msg.value.content.vote.value == 1)
402 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)))
403 else if (msg.value.content.vote.value == -1)
404 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)))
405 message.appendChild(tools.mini(msg, link))
406 return message
407 } else if (typeof msg.value.content === 'string') {
408 var unboxed = ssbKeys.unbox(msg.value.content, keys)
409 if (unboxed) {
410 msg.value.content = unboxed
411 msg.value.private = true
412 return module.exports(msg)
413 } else {
414 var privateMsg = h('span', ' sent a private message.')
415 message.appendChild(tools.mini(msg, privateMsg))
416 return message
417 }
418 } else {
419
420 //FULL FALLBACK
421 message.appendChild(tools.header(msg))
422 message.appendChild(h('pre', tools.rawJSON(msg.value)))
423
424 //MINI FALLBACK
425 //var fallback = h('span', ' ' + msg.value.content.type)
426 //message.appendChild(tools.mini(msg, fallback))
427 return h('div', message)
428 }
429}
430

Built with git-ssb-web