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