git ssb

1+

ev / scatter



Tree: 42abcae02e90d52d27bc16faa27ce36a2678fa62

Files: 42abcae02e90d52d27bc16faa27ce36a2678fa62 / views.js

5827 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 userStream = function (src) {
23 var content = h('div.content')
24 var screen = document.getElementById('screen')
25 screen.appendChild(hyperscroll(content))
26
27 function createStream (opts) {
28 return pull(
29 Next(sbot.query, opts, ['value', 'timestamp']),
30 pull.map(function (msg) {
31 if (msg.value) {
32 return render(msg)
33 }
34 })
35 )
36 }
37
38 pull(
39 createStream({
40 limit: 10,
41 reverse: true,
42 live: false,
43 query: [{$filter: { value: {author: src, content: {type: 'scat_message'}, timestamp: { $gt: 0 }}}}]
44 }),
45 stream.bottom(content)
46 )
47
48 pull(
49 createStream({
50 limit: 10,
51 old: false,
52 live: true,
53 query: [{$filter: { value: {author: src, content: {type:'scat_message'}, timestamp: { $gt: 0 }}}}]
54 }),
55 stream.top(content)
56 )
57
58 var profile = h('div.content#profile', h('div.message'))
59
60 if (screen.firstChild.firstChild) {
61 screen.firstChild.insertBefore(profile, screen.firstChild.firstChild)
62 } else {
63 screen.firstChild.appendChild(profile)
64 }
65
66 var avatars = h('div.avatars',
67 h('a', {href: '#' + src},
68 h('span.avatar--medium#profileImage', avatar.image(src)),
69 avatar.name(src)
70 ),
71 )
72
73 var buttons = h('div.buttons')
74
75 profile.firstChild.appendChild(avatars)
76 profile.firstChild.appendChild(buttons)
77 if (src != id) {
78 buttons.appendChild(tools.follow(src))
79 }
80 profile.firstChild.appendChild(h('hr'))
81}
82
83var msgThread = function (src) {
84
85 var content = h('div.content')
86 var screen = document.getElementById('screen')
87 screen.appendChild(hyperscroll(content))
88
89 pull(
90 sbot.query({query: [{$filter: { value: { content: {root: src}, timestamp: { $gt: 1 }}}}], live: true}),
91 pull.drain(function (msg) {
92 if (msg.value) {
93 content.appendChild(render(msg))
94 }
95 })
96 )
97
98
99 sbot.get(src, function (err, data) {
100 if (err) {
101 var message = h('div.message', 'Missing message!')
102 content.appendChild(message)
103 }
104 if (data) {
105 data.value = data
106 data.key = src
107 console.log(data)
108 var rootMsg = render(data)
109
110 if (content.firstChild) {
111 content.insertBefore(rootMsg, content.firstChild)
112 } else {
113 content.appendChild(rootMsg)
114 }
115 }
116 })
117}
118
119var keyPage = function () {
120 var screen = document.getElementById('screen')
121
122 var importKey = h('textarea.import', {placeholder: 'Import a new public/private key', name: 'textarea', style: 'width: 97%; height: 100px;'})
123
124 var content = h('div.content',
125 h('div.message#key',
126 h('h1', 'Your Key'),
127 h('p', {innerHTML: 'Your public/private key is: <pre><code>' + localStorage[config.caps.shs + '/secret'] + '</code></pre>'},
128 h('button.btn', {onclick: function (e){
129 localStorage[config.caps.shs +'/secret'] = ''
130 alert('Your public/private key has been deleted')
131 e.preventDefault()
132 location.hash = ""
133 location.reload()
134 }}, 'Delete Key')
135 ),
136 h('hr'),
137 h('form',
138 importKey,
139 h('button.btn', {onclick: function (e){
140 if(importKey.value) {
141 localStorage[config.caps.shs + '/secret'] = importKey.value.replace(/\s+/g, ' ')
142 e.preventDefault()
143 alert('Your public/private key has been updated')
144 }
145 location.hash = ""
146 location.reload()
147 }}, 'Import key'),
148 )
149 )
150 )
151
152 screen.appendChild(hyperscroll(content))
153}
154
155function scatterStream () {
156
157 var screen = document.getElementById('screen')
158 var content = h('div.content')
159
160 screen.appendChild(hyperscroll(content))
161
162 var chatbox = h('input.compose', {placeholder: '>'})
163
164 var chat = h('div.content')
165
166 var publish = h('button.btn.right', 'Send', {
167 onclick: function () {
168 if (chatbox.value) {
169 var content = {
170 text: chatbox.value,
171 type: 'scat_message'
172 }
173 sbot.publish(content, function (err, msg) {
174 if (err) throw err
175 chatbox.value = ''
176 console.log('Published!', msg)
177 })
178 }
179 }
180 })
181
182 chat.appendChild(h('div.message', chatbox, publish))
183
184 if (screen.firstChild.firstChild) {
185 screen.firstChild.insertBefore(chat, screen.firstChild.firstChild)
186 } else {
187 screen.firstChild.appendChild(chat)
188 }
189
190 function createStream (opts) {
191 return pull(
192 Next(sbot.query, opts, ['value', 'timestamp']),
193 pull.map(function (msg) {
194 if (msg.value) {
195 return render(msg)
196 }
197 })
198 )
199 }
200
201 pull(
202 createStream({
203 limit: 10,
204 reverse: true,
205 live: false,
206 query: [{$filter: { value: { content: {type: 'scat_message'}, timestamp: { $gt: 0 }}}}]
207 }),
208 stream.bottom(content)
209 )
210
211 pull(
212 createStream({
213 limit: 10,
214 old: false,
215 live: true,
216 query: [{$filter: { value: { content: {type: 'scat_message'}, timestamp: { $gt: 0 }}}}]
217 }),
218 stream.top(content)
219 )
220}
221
222function hash () {
223 return window.location.hash.substring(1)
224}
225
226module.exports = function () {
227 var src = hash()
228
229 if (ref.isFeed(src)) {
230 userStream(src)
231 } else if (ref.isMsg(src)) {
232 msgThread(src)
233 } else if (src == 'key') {
234 keyPage()
235 } else {
236 scatterStream()
237 }
238}
239

Built with git-ssb-web