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