git ssb

2+

ev / mvd



Tree: 072843e1788c3cdbb773ca4e7524a92b3f0ef002

Files: 072843e1788c3cdbb773ca4e7524a92b3f0ef002 / views.js

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

Built with git-ssb-web