git ssb

2+

ev / mvd



Tree: d89e5bb09b3a46bb3c18119673f03862020eb44c

Files: d89e5bb09b3a46bb3c18119673f03862020eb44c / views.js

9571 bytesRaw
1var pull = require('pull-stream')
2var human = require('human-time')
3var sbot = require('./scuttlebot')
4var hyperscroll = require('hyperscroll')
5var More = require('pull-more')
6var stream = require('hyperloadmore/stream')
7var h = require('hyperscript')
8var render = require('./render')
9var ref = require('ssb-ref')
10var client = require('ssb-client')
11
12var Next = require('pull-next-query')
13
14var config = require('./config')()
15
16var tools = require('./tools')
17var avatar = require('./avatar')
18var id = require('./keys').id
19
20var ssbKeys = require('ssb-keys')
21var keys = require('./keys')
22
23var checkInvite = require('./invite')
24
25var compose = require('./compose')
26
27var about = function () {
28 var screen = document.getElementById('screen')
29
30 var about = require('./about')
31
32 var content = h('div.content', about)
33
34 screen.appendChild(hyperscroll(content))
35}
36
37var privateStream = function () {
38 var content = h('div.content')
39 var screen = document.getElementById('screen')
40 screen.appendChild(hyperscroll(content))
41
42 function createStream (opts) {
43 return pull(
44 More(sbot.createLogStream, opts),
45 pull.filter(function (msg) {
46 return 'string' == typeof msg.value.content
47 }),
48 pull.filter(function (msg) {
49 var unboxed = ssbKeys.unbox(msg.value.content, keys)
50 if (unboxed) {
51 msg.value.content = unboxed
52 msg.value.private = true
53 return msg
54 }
55 }),
56 pull.map(function (msg) {
57 return render(msg)
58 })
59 )
60 }
61
62 pull(
63 createStream({old: false, limit: 1000}),
64 stream.top(content)
65 )
66
67 pull(
68 createStream({reverse: true, live: false, limit: 1000}),
69 stream.bottom(content)
70 )
71}
72
73var mentionsStream = function () {
74 var content = h('div.content')
75
76 var screen = document.getElementById('screen')
77
78 screen.appendChild(hyperscroll(content))
79
80 function createStream (opts) {
81 return pull(
82 Next(sbot.backlinks, opts, ['value', 'timestamp']),
83 pull.map(function (msg) {
84 if (msg.value.private == true) return
85 return render(msg)
86 })
87 )
88 }
89
90 pull(
91 createStream({
92 limit: 10,
93 reverse: true,
94 index: 'DTA',
95 live: false,
96 query: [{$filter: {dest: id}}]
97 }),
98 stream.bottom(content)
99 )
100
101 pull(
102 createStream({
103 limit: 10,
104 old: false,
105 index: 'DTA',
106 live: true,
107 query: [{$filter: {dest: id}}]
108 }),
109 stream.top(content)
110 )
111}
112
113var userStream = function (src) {
114 var content = h('div.content')
115 var screen = document.getElementById('screen')
116 screen.appendChild(hyperscroll(content))
117 function createStream (opts) {
118 return pull(
119 More(sbot.userStream, opts, ['value', 'sequence']),
120 pull.map(function (msg) {
121 return render(msg)
122 })
123 )
124 }
125
126 pull(
127 createStream({old: false, limit: 10, id: src}),
128 stream.top(content)
129 )
130
131 pull(
132 createStream({reverse: true, live: false, limit: 10, id: src}),
133 stream.bottom(content)
134 )
135
136 var profile = h('div.content#profile', h('div.message'))
137
138 if (screen.firstChild.firstChild) {
139 screen.firstChild.insertBefore(profile, screen.firstChild.firstChild)
140 } else {
141 screen.firstChild.appendChild(profile)
142 }
143
144 var avatars = h('div.avatars',
145 h('a', {href: '#' + src},
146 h('span.avatar--medium', avatar.image(src)),
147 avatar.name(src)
148 )
149 )
150
151 pull(
152 sbot.userStream({id: src, reverse: false, limit: 1}),
153 pull.drain(function (msg) {
154 var howlong = h('span', ' arrived ', human(new Date(msg.value.timestamp)))
155 avatars.appendChild(howlong)
156 console.log(msg)
157 })
158 )
159
160 var name = avatar.name(src)
161
162 var buttons = h('div.buttons')
163
164 profile.firstChild.appendChild(avatars)
165 profile.firstChild.appendChild(buttons)
166 buttons.appendChild(tools.mute(src))
167
168 var writeMessage = h('button.btn', 'Public message ', avatar.name(src), {
169 onclick: function () {
170 opts = {}
171 opts.type = 'post'
172 opts.mentions = '[' + name.textContent + '](' + src + ')'
173 var composer = h('div#composer', h('div.message', compose(opts)))
174 profile.appendChild(composer)
175 }
176 })
177
178 var writePrivate = h('button.btn', 'Private message ', avatar.name(src), {
179 onclick: function () {
180 opts = {}
181 opts.type = 'post'
182 opts.mentions = '[' + name.textContent + '](' + src + ')'
183 opts.recps = [src, id]
184 var composer = h('div#composer', h('div.message', compose(opts)))
185 profile.appendChild(composer)
186 }
187 })
188
189 buttons.appendChild(writeMessage)
190 buttons.appendChild(writePrivate)
191 buttons.appendChild(tools.follow(src))
192
193 profile.firstChild.appendChild(tools.getFollowing(src))
194 profile.firstChild.appendChild(tools.getFollowers(src))
195}
196
197var msgThread = function (src) {
198
199 var content = h('div.content')
200 var screen = document.getElementById('screen')
201 screen.appendChild(hyperscroll(content))
202
203 pull(
204 sbot.query({query: [{$filter: { value: { content: {root: src}, timestamp: { $gt: 1 }}}}], live: true}),
205 pull.drain(function (msg) {
206 if (msg.value) {
207 content.appendChild(render(msg))
208 }
209 })
210 )
211
212 sbot.get(src, function (err, data) {
213 if (err) {console.log('could not find message')}
214 data.value = data
215 data.key = src
216 console.log(data)
217 var rootMsg = render(data)
218
219 if (content.firstChild) {
220 content.insertBefore(rootMsg, content.firstChild)
221 } else {
222 content.appendChild(rootMsg)
223 }
224 })
225}
226
227var keyPage = function () {
228 var screen = document.getElementById('screen')
229
230 var importKey = h('textarea.import', {placeholder: 'Import a new public/private key', name: 'textarea', style: 'width: 97%; height: 100px;'})
231
232 var content = h('div.content',
233 h('div.message#key',
234 h('h1', 'Your Key'),
235 h('p', {innerHTML: 'Your public/private key is: <pre><code>' + localStorage[config.caps.shs + '/secret'] + '</code></pre>'},
236 h('button.btn', {onclick: function (e){
237 localStorage[config.caps.shs +'/secret'] = ''
238 alert('Your public/private key has been deleted')
239 e.preventDefault()
240 location.hash = ""
241 location.reload()
242 }}, 'Delete Key')
243 ),
244 h('hr'),
245 h('form',
246 importKey,
247 h('button.btn', {onclick: function (e){
248 if(importKey.value) {
249 localStorage[config.caps.shs + '/secret'] = importKey.value.replace(/\s+/g, ' ')
250 e.preventDefault()
251 alert('Your public/private key has been updated')
252 }
253 location.hash = ""
254 location.reload()
255 }}, 'Import key'),
256 )
257 )
258 )
259
260 screen.appendChild(hyperscroll(content))
261}
262
263function everythingStream () {
264
265 var screen = document.getElementById('screen')
266 var content = h('div.content')
267
268 screen.appendChild(hyperscroll(content))
269
270 function createStream (opts) {
271 return pull(
272 Next(sbot.query, opts, ['value', 'timestamp']),
273 pull.map(function (msg) {
274 if (msg.value) {
275 return render(msg)
276 }
277 })
278 )
279 }
280
281 pull(
282 createStream({
283 limit: 10,
284 reverse: true,
285 live: false,
286 query: [{$filter: { value: { timestamp: { $gt: 0 }}}}]
287 }),
288 stream.bottom(content)
289 )
290
291 pull(
292 createStream({
293 limit: 10,
294 old: false,
295 live: true,
296 query: [{$filter: { value: { timestamp: { $gt: 0 }}}}]
297 }),
298 stream.top(content)
299 )
300}
301
302function backchannel () {
303
304 var screen = document.getElementById('screen')
305 var content = h('div.content')
306
307 screen.appendChild(hyperscroll(content))
308
309 var chatbox = h('input', {placeholder: 'Backchannel'})
310
311 var chat = h('div.content')
312
313 var publish = h('button.btn', 'Publish', {
314 onclick: function () {
315 if (chatbox.value) {
316 var content = {
317 text: chatbox.value,
318 type: 'scat_message'
319 }
320 sbot.publish(content, function (err, msg) {
321 if (err) throw err
322 chatbox.value = ''
323 console.log('Published!', msg)
324 })
325 }
326 }
327 })
328
329 chat.appendChild(h('div.message', chatbox, publish))
330
331 if (screen.firstChild.firstChild) {
332 screen.firstChild.insertBefore(chat, screen.firstChild.firstChild)
333 } else {
334 screen.firstChild.appendChild(chat)
335 }
336
337 function createStream (opts) {
338 return pull(
339 Next(sbot.query, opts, ['value', 'timestamp']),
340 pull.map(function (msg) {
341 if (msg.value) {
342 return render(msg)
343 }
344 })
345 )
346 }
347
348 pull(
349 createStream({
350 limit: 10,
351 reverse: true,
352 live: false,
353 query: [{$filter: { value: { content: {type: 'scat_message'}, timestamp: { $gt: 0 }}}}]
354 }),
355 stream.bottom(content)
356 )
357
358 pull(
359 createStream({
360 limit: 10,
361 old: false,
362 live: true,
363 query: [{$filter: { value: { content: {type: 'scat_message'}, timestamp: { $gt: 0 }}}}]
364 }),
365 stream.top(content)
366 )
367}
368
369
370function hash () {
371 return window.location.hash.substring(1)
372}
373
374module.exports = function () {
375 var src = hash()
376
377 if (ref.isFeed(src)) {
378 userStream(src)
379 } else if (ref.isMsg(src)) {
380 msgThread(src)
381 } else if (src == 'mentions') {
382 mentionsStream()
383 } else if (src == 'about') {
384 about()
385 } else if (src == 'backchannel') {
386 backchannel()
387 } else if (src == 'private') {
388 privateStream()
389 } else if (src == 'key') {
390 keyPage()
391 } else {
392 everythingStream()
393 checkInvite()
394 }
395
396}
397

Built with git-ssb-web