git ssb

3+

ev / decent



Tree: 0489df1577d94faef0a87d1a8de094bcf75df2ee

Files: 0489df1577d94faef0a87d1a8de094bcf75df2ee / views.js

15648 bytesRaw
1var pull = require('pull-stream')
2var human = require('human-time')
3var sbot = require('./scuttlebot')
4var hyperscroll = require('hyperscroll')
5var hyperfile = require('hyperfile')
6var dataurl = require('dataurl-')
7var More = require('pull-more')
8var stream = require('hyperloadmore/stream')
9var h = require('hyperscript')
10var render = require('./render')
11var ref = require('ssb-ref')
12var client = require('ssb-client')
13
14var Next = require('pull-next-query')
15
16var config = require('./config')()
17
18var tools = require('./tools')
19var avatar = require('./avatar')
20var id = require('./keys').id
21
22var ssbKeys = require('ssb-keys')
23var keys = require('./keys')
24
25var compose = require('./compose')
26
27var mentionsStream = function (src) {
28 var content = h('div.content')
29
30 var screen = document.getElementById('screen')
31
32 screen.appendChild(hyperscroll(content))
33
34 function createStream (opts) {
35 return pull(
36 Next(sbot.backlinks, opts, ['value', 'timestamp']),
37 pull.map(function (msg) {
38 if (msg.value.private == true) return h('div.private')
39 return render(msg)
40 })
41 )
42 }
43
44 pull(
45 createStream({
46 limit: 10,
47 reverse: true,
48 index: 'DTA',
49 live: false,
50 query: [{$filter: {dest: src}}]
51 }),
52 stream.bottom(content)
53 )
54
55 pull(
56 createStream({
57 limit: 10,
58 old: false,
59 index: 'DTA',
60 live: true,
61 query: [{$filter: {dest: src}}]
62 }),
63 stream.top(content)
64 )
65
66 var profile = h('div.content#profile', h('div.message'))
67
68 if (screen.firstChild.firstChild) {
69 screen.firstChild.insertBefore(profile, screen.firstChild.firstChild)
70 } else {
71 screen.firstChild.appendChild(profile)
72 }
73
74 var name = avatar.name(src)
75
76 var editname = h('span',
77 avatar.name(src),
78 h('button.btn', 'New name', {
79 onclick: function () {
80 var nameput = h('input', {placeholder: name.textContent})
81 var nameedit =
82 h('span', nameput,
83 h('button.btn', 'Preview', {
84 onclick: function () {
85 if (nameput.value[0] != '@')
86 tobename = nameput.value
87 else
88 tobename = nameput.value.substring(1, 100)
89 var newname = h('span', h('a', {href: '#' + src}, '@' + tobename), h('button.btn', 'Publish', {
90 onclick: function () {
91 var donename = h('span', h('a', {href: '#' + src}, '@' + tobename))
92 sbot.publish({type: 'about', about: src, name: tobename})
93 localStorage[src + 'name'] = tobename
94 newname.parentNode.replaceChild(donename, newname)
95 }
96 }))
97 nameedit.parentNode.replaceChild(newname, nameedit)
98 }
99 })
100 )
101 editname.parentNode.replaceChild(nameedit, editname)
102 }
103 })
104 )
105
106 var editimage = h('span',
107 h('button.btn', 'New image', {
108 onclick: function () {
109 var upload =
110 h('span',
111 hyperfile.asDataURL(function (data) {
112 if(data) {
113 //img.src = data
114 var _data = dataurl.parse(data)
115 pull(
116 pull.once(_data.data),
117 sbot.addblob(function (err, hash) {
118 if(err) return alert(err.stack)
119 selected = {
120 link: hash,
121 size: _data.data.length,
122 type: _data.mimetype
123 }
124 })
125 )
126 }
127 }),
128 h('button.btn', 'Preview image', {
129 onclick: function() {
130 if (selected) {
131 console.log(selected)
132 var oldImage = document.getElementById('profileImage')
133 var newImage = h('span.avatar--medium', h('img', {src: config.blobsUrl + selected.link}))
134 var publish = h('button.btn', 'Publish image', {
135 onclick: function () {
136 sbot.publish({
137 type: 'about',
138 about: src,
139 image: selected
140 }, function (err, published) {
141 console.log(published)
142 })
143 }
144 })
145 upload.parentNode.replaceChild(publish, upload)
146 oldImage.parentNode.replaceChild(newImage, oldImage)
147 }
148 }
149 })
150 )
151 editimage.parentNode.replaceChild(upload, editimage)
152 }
153 })
154 )
155
156
157 var avatars = h('div.avatars',
158 h('a', {href: '#' + src},
159 h('span.avatar--medium#profileImage', avatar.image(src)),
160 editname,
161 h('br'),
162 editimage
163 )
164 )
165
166 pull(
167 sbot.userStream({id: src, reverse: false, limit: 1}),
168 pull.drain(function (msg) {
169 var howlong = h('span', h('br'), ' arrived ', human(new Date(msg.value.timestamp)))
170 avatars.appendChild(howlong)
171 console.log(msg)
172 })
173 )
174
175 var buttons = h('div.buttons')
176
177 profile.firstChild.appendChild(avatars)
178 profile.firstChild.appendChild(buttons)
179 buttons.appendChild(tools.mute(src))
180
181 setTimeout (function () {
182 opts = {}
183 opts.type = 'post'
184 opts.mentions = '[' + name.textContent + '](' + src + ')'
185 var composer = h('div#composer', h('div.message', compose(opts)))
186 profile.appendChild(composer)
187 }, 1000)
188
189 //buttons.appendChild(writeMessage)
190 buttons.appendChild(tools.follow(src))
191
192 buttons.appendChild(h('a', {href: '#' + src}, h('button.btn', "View ", avatar.name(src), "'s feed")))
193}
194
195var userStream = function (src) {
196 var content = h('div.content')
197
198 var screen = document.getElementById('screen')
199 screen.appendChild(hyperscroll(content))
200 function createStream (opts) {
201 return pull(
202 More(sbot.userStream, opts, ['value', 'sequence']),
203 pull.map(function (msg) {
204 return render(h('div', msg))
205 })
206 )
207 }
208
209 pull(
210 createStream({old: false, limit: 10, id: src}),
211 stream.top(content)
212 )
213
214 pull(
215 createStream({reverse: true, live: false, limit: 10, id: src}),
216 stream.bottom(content)
217 )
218
219 var profile = h('div.content#profile', h('div.message'))
220
221 if (screen.firstChild.firstChild) {
222 screen.firstChild.insertBefore(profile, screen.firstChild.firstChild)
223 } else {
224 screen.firstChild.appendChild(profile)
225 }
226
227 var name = avatar.name(src)
228
229 var editname = h('span',
230 avatar.name(src),
231 h('button.btn', 'New name', {
232 onclick: function () {
233 var nameput = h('input', {placeholder: name.textContent})
234 var nameedit =
235 h('span', nameput,
236 h('button.btn', 'Preview', {
237 onclick: function () {
238 if (nameput.value[0] != '@')
239 tobename = nameput.value
240 else
241 tobename = nameput.value.substring(1, 100)
242 var newname = h('span', h('a', {href: '#' + src}, '@' + tobename), h('button.btn', 'Publish', {
243 onclick: function () {
244 var donename = h('span', h('a', {href: '#' + src}, '@' + tobename))
245 sbot.publish({type: 'about', about: src, name: tobename})
246 localStorage[src + 'name'] = tobename
247 newname.parentNode.replaceChild(donename, newname)
248 }
249 }))
250 nameedit.parentNode.replaceChild(newname, nameedit)
251 }
252 })
253 )
254 editname.parentNode.replaceChild(nameedit, editname)
255 }
256 })
257 )
258
259 var editimage = h('span',
260 h('button.btn', 'New image', {
261 onclick: function () {
262 var upload =
263 h('span',
264 hyperfile.asDataURL(function (data) {
265 if(data) {
266 //img.src = data
267 var _data = dataurl.parse(data)
268 pull(
269 pull.once(_data.data),
270 sbot.addblob(function (err, hash) {
271 if(err) return alert(err.stack)
272 selected = {
273 link: hash,
274 size: _data.data.length,
275 type: _data.mimetype
276 }
277 })
278 )
279 }
280 }),
281 h('button.btn', 'Preview image', {
282 onclick: function() {
283 if (selected) {
284 console.log(selected)
285 var oldImage = document.getElementById('profileImage')
286 var newImage = h('span.avatar--medium', h('img', {src: config.blobsUrl + selected.link}))
287 var publish = h('button.btn', 'Publish image', {
288 onclick: function () {
289 sbot.publish({
290 type: 'about',
291 about: src,
292 image: selected
293 }, function (err, published) {
294 console.log(published)
295 })
296 }
297 })
298 upload.parentNode.replaceChild(publish, upload)
299 oldImage.parentNode.replaceChild(newImage, oldImage)
300 }
301 }
302 })
303 )
304 editimage.parentNode.replaceChild(upload, editimage)
305 }
306 })
307 )
308
309
310 var avatars = h('div.avatars',
311 h('a', {href: '#' + src},
312 h('span.avatar--medium#profileImage', avatar.image(src)),
313 editname,
314 h('br'),
315 editimage
316 )
317 )
318
319
320 pull(
321 sbot.userStream({id: src, reverse: false, limit: 1}),
322 pull.drain(function (msg) {
323 var howlong = h('span', h('br'), ' arrived ', human(new Date(msg.value.timestamp)))
324 avatars.appendChild(howlong)
325 console.log(msg)
326 })
327 )
328
329
330
331 var buttons = h('div.buttons')
332
333 profile.firstChild.appendChild(avatars)
334 profile.firstChild.appendChild(buttons)
335 buttons.appendChild(tools.mute(src))
336
337 buttons.appendChild(tools.follow(src))
338
339 buttons.appendChild(h('a', {href: '#wall/' + src}, h('button.btn', "Write on ", avatar.name(src), "'s wall")))
340}
341
342var msgThread = function (src) {
343
344 var content = h('div.content')
345 var screen = document.getElementById('screen')
346 screen.appendChild(hyperscroll(content))
347
348 pull(
349 sbot.query({query: [{$filter: { value: { content: {root: src}, timestamp: { $gt: 1 }}}}], live: true}),
350 pull.drain(function (msg) {
351 if (msg.value) {
352 content.appendChild(render(msg))
353 }
354 })
355 )
356
357
358 sbot.get(src, function (err, data) {
359 if (err) {
360 var message = h('div.message', 'Missing message!')
361 content.appendChild(message)
362 }
363 if (data) {
364 data.value = data
365 data.key = src
366 console.log(data)
367 var rootMsg = render(data)
368
369 if (content.firstChild) {
370 content.insertBefore(rootMsg, content.firstChild)
371 } else {
372 content.appendChild(rootMsg)
373 }
374 if (data.value.content.type == 'git-repo') {
375 pull(
376 sbot.backlinks({query: [{$filter: {value: {content: {type: 'git-update'}}, dest: src}}]}),
377 pull.drain(function (msg) {
378 if (msg.value) {
379 content.appendChild(render(msg))
380 }
381 })
382 )
383 }
384
385 }
386 })
387}
388
389var keyPage = function () {
390 var screen = document.getElementById('screen')
391
392 var importKey = h('textarea.import', {placeholder: 'Import a new public/private key', name: 'textarea', style: 'width: 97%; height: 100px;'})
393
394 var content = h('div.content',
395 h('div.message#key',
396 h('h1', 'Your Key'),
397 h('p', {innerHTML: 'Your public/private key is: <pre><code>' + localStorage[config.caps.shs + '/secret'] + '</code></pre>'},
398 h('button.btn', {onclick: function (e){
399 localStorage[config.caps.shs +'/secret'] = ''
400 alert('Your public/private key has been deleted')
401 e.preventDefault()
402 location.hash = ""
403 location.reload()
404 }}, 'Delete Key')
405 ),
406 h('hr'),
407 h('form',
408 importKey,
409 h('button.btn', {onclick: function (e){
410 if(importKey.value) {
411 localStorage[config.caps.shs + '/secret'] = importKey.value.replace(/\s+/g, ' ')
412 e.preventDefault()
413 alert('Your public/private key has been updated')
414 }
415 location.hash = ""
416 location.reload()
417 }}, 'Import key'),
418 )
419 )
420 )
421
422 screen.appendChild(hyperscroll(content))
423}
424
425function everythingStream () {
426
427 var screen = document.getElementById('screen')
428 var content = h('div.content')
429
430 screen.appendChild(hyperscroll(content))
431
432 function createStream (opts) {
433 return pull(
434 Next(sbot.query, opts, ['value', 'timestamp']),
435 pull.map(function (msg) {
436 if (msg.value) {
437 if (msg.value.timestamp > Date.now()) {
438 return h('div.future')
439 } else {
440 return render(msg)
441 }
442 }
443 })
444 )
445 }
446
447 pull(
448 createStream({
449 limit: 10,
450 reverse: true,
451 live: false,
452 query: [{$filter: { value: { timestamp: { $gt: 0 }}}}]
453 }),
454 stream.bottom(content)
455 )
456
457 pull(
458 createStream({
459 limit: 10,
460 old: false,
461 live: true,
462 query: [{$filter: { value: { timestamp: { $gt: 0 }}}}]
463 }),
464 stream.top(content)
465 )
466}
467
468function backchannel () {
469
470 var screen = document.getElementById('screen')
471 var content = h('div.content')
472
473 screen.appendChild(hyperscroll(content))
474
475 var chatbox = h('input', {placeholder: 'Backchannel'})
476
477 var chat = h('div.content')
478
479 var publish = h('button.btn', 'Publish', {
480 onclick: function () {
481 if (chatbox.value) {
482 var content = {
483 text: chatbox.value,
484 type: 'scat_message'
485 }
486 sbot.publish(content, function (err, msg) {
487 if (err) throw err
488 chatbox.value = ''
489 console.log('Published!', msg)
490 })
491 }
492 }
493 })
494
495 chat.appendChild(h('div.message', chatbox, publish))
496
497 if (screen.firstChild.firstChild) {
498 screen.firstChild.insertBefore(chat, screen.firstChild.firstChild)
499 } else {
500 screen.firstChild.appendChild(chat)
501 }
502
503 function createStream (opts) {
504 return pull(
505 Next(sbot.query, opts, ['value', 'timestamp']),
506 pull.map(function (msg) {
507 if (msg.value) {
508 return render(msg)
509 }
510 })
511 )
512 }
513
514 pull(
515 createStream({
516 limit: 10,
517 reverse: true,
518 live: false,
519 query: [{$filter: { value: { content: {type: 'scat_message'}, timestamp: { $gt: 0 }}}}]
520 }),
521 stream.bottom(content)
522 )
523
524 pull(
525 createStream({
526 limit: 10,
527 old: false,
528 live: true,
529 query: [{$filter: { value: { content: {type: 'scat_message'}, timestamp: { $gt: 0 }}}}]
530 }),
531 stream.top(content)
532 )
533}
534
535function search (src) {
536 console.log('search' + src)
537
538 var content = h('div.content')
539 var screen = document.getElementById('screen')
540 screen.appendChild(hyperscroll(content))
541
542 pull(
543 sbot.search.query({query: src, limit: 100}),
544 pull.drain(function (search) {
545 content.appendChild(render(search))
546 })
547 )
548
549}
550
551function hash () {
552 return window.location.hash.substring(1)
553}
554
555module.exports = function () {
556 var src = hash()
557
558 if (ref.isFeed(src)) {
559 userStream(src)
560 } else if (ref.isMsg(src)) {
561 msgThread(src)
562 } else if (ref.isFeed(src.substring(5))) {
563 mentionsStream(src.substring(5))
564 } else if (src == 'backchannel') {
565 backchannel()
566 } else if (src == 'key') {
567 keyPage()
568 } else if (src[0] == '?' || (src[0] == '#')) {
569 if (src[0] == '#')
570 search(src.split('%20').join(' '))
571 else
572 search(src.substr(1).split('%20').join(' '))
573 } else {
574 everythingStream()
575 }
576}
577

Built with git-ssb-web