git ssb

2+

ev / mvd



Tree: 6d20a46e7abaa94845f87c72e3d6c8a135b098a2

Files: 6d20a46e7abaa94845f87c72e3d6c8a135b098a2 / views.js

6894 bytesRaw
1var pull = require('pull-stream')
2var sbot = require('./scuttlebot')
3var hyperscroll = require('hyperscroll')
4var More = require('pull-more')
5var stream = require('hyperloadmore/stream')
6var h = require('hyperscript')
7var render = require('./render')
8var ref = require('ssb-ref')
9
10var config = require('./config')()
11
12var avatar = require('./avatar')
13var id = require('./keys').id
14
15var fs = require('fs')
16
17var compose = require('./compose')
18
19var about = function () {
20 var screen = document.getElementById('screen')
21
22 var about = require('./about')
23
24 var content = h('div.content', about)
25
26 screen.appendChild(hyperscroll(content))
27}
28
29var edit = function() {
30 var content = h('div.content')
31
32 var screen = document.getElementById('screen')
33
34 screen.appendChild(hyperscroll(content))
35
36 var nameInput = h('input', {placeholder: 'New name'})
37
38 var locInput = h('input', {placeholder: 'New location'})
39
40 var descInput = h('textarea', {placeholder: 'New description'})
41
42 var editor = h('div.message',
43 h('h1', 'Edit profile'),
44 nameInput,
45 h('button.btn.btn-primary', 'Preview', {onclick: function () {
46 if(nameInput.value) {
47 api.message_confirm({
48 type: 'about',
49 about: id,
50 name: nameInput.value || undefined
51 })
52 }
53 }}),
54 h('hr'),
55 locInput,
56 h('button.btn.btn-primary', 'Preview', {onclick: function () {
57 if(locInput.value) {
58 api.message_confirm({
59 type: 'loc',
60 about: id,
61 loc: locInput.value || undefined
62 })
63 }
64 }}),
65 h('hr'),
66 descInput,
67 h('button.btn.btn-primary', 'Preview', {onclick: function (){
68 if(descInput.value) {
69 api.message_confirm({
70 type: 'description',
71 about: id,
72 description: descInput.value || undefined
73 })
74 }
75 }}),
76 h('hr')
77 )
78
79 content.appendChild(editor)
80}
81
82var mentionsStream = function () {
83 var content = h('div.content')
84
85 var screen = document.getElementById('screen')
86
87 screen.appendChild(hyperscroll(content))
88
89 function createStream (opts) {
90 return pull(
91 sbot.backlinks({query: [{$filter: {dest: id}}], reverse: true}),
92 pull.map(function (msg) {
93 if (msg.value.private == true)
94 return 'ignoring private message'
95 else
96 return render(msg)
97 })
98 )
99 }
100
101 pull(
102 createStream({reverse: true, limit: 10}),
103 stream.bottom(content)
104 )
105}
106
107var logStream = function () {
108 var content = h('div.content')
109 var screen = document.getElementById('screen')
110 screen.appendChild(hyperscroll(content))
111
112 function createStream (opts) {
113 return pull(
114 More(sbot.createLogStream, opts),
115 pull.map(function (msg) {
116 return render(msg)
117 })
118
119 )
120 }
121
122 pull(
123 createStream({old: false, limit: 10}),
124 stream.top(content)
125 )
126
127 pull(
128 createStream({reverse: true, live: false, limit: 10}),
129 stream.bottom(content)
130 )
131}
132
133var userStream = function (src) {
134 var content = h('div.content')
135 var screen = document.getElementById('screen')
136 screen.appendChild(hyperscroll(content))
137 function createStream (opts) {
138 return pull(
139 More(sbot.userStream, opts, ['value', 'sequence']),
140 pull.map(function (msg) {
141 return render(msg)
142 })
143 )
144 }
145
146 pull(
147 createStream({old: false, limit: 10, id: src}),
148 stream.top(content)
149 )
150
151 pull(
152 createStream({reverse: true, live: false, limit: 10, id: src}),
153 stream.bottom(content)
154 )
155
156
157 var profile = h('div.content#profile', h('div.message'))
158
159 if (screen.firstChild.firstChild) {
160 screen.firstChild.insertBefore(profile, screen.firstChild.firstChild)
161 } else {
162 screen.firstChild.appendChild(profile)
163 }
164
165 var avatars = h('div.avatars',
166 h('a', {href: '#' + src},
167 h('span.avatar--medium', avatar.image(src)),
168 avatar.name(src)
169 )
170 )
171
172 var buttons = h('div.buttons')
173
174
175 profile.firstChild.appendChild(avatars)
176 profile.firstChild.appendChild(buttons)
177
178 if (localStorage['mute:' + src])
179 var mute = h('button.btn', 'Unmute', {
180 onclick: function () {
181 delete localStorage['mute:' + src]
182 location.reload()
183 }
184 })
185 else
186 var mute = h('button.btn', 'Mute', {
187 onclick: function () {
188 localStorage['mute:' + src] = true
189 location.reload()
190 }
191 })
192
193 buttons.appendChild(mute)
194
195}
196
197var msgThread = function(src) {
198 var content = h('div.content')
199 var screen = document.getElementById('screen')
200 screen.appendChild(hyperscroll(content))
201 sbot.get(src, function (err, data) {
202 if (err) {console.log('could not find message') }
203 data.value = data
204 var root = src
205 if (data.value.content.root)
206 root = data.value.content.root
207 sbot.get(root, function (err, data) {
208 if (err) { console.log('could not find root')}
209 data.value = data
210 data.key = root
211 content.appendChild(render(data))
212 pull(
213 sbot.links({rel: 'root', dest: root, values: true, keys: true, live: true}),
214 pull.drain(function (msg) {
215 if (msg.value)
216 content.appendChild(render(msg))
217 })
218 )
219 })
220 })
221}
222
223var keyPage = function () {
224 var screen = document.getElementById('screen')
225
226 var importKey = h('textarea.import', {placeholder: 'Import a new public/private key', name: 'textarea', style: 'width: 97%; height: 100px;'})
227
228 var content = h('div.content',
229 h('div.message#key',
230 h('h1', 'Your Key'),
231 h('p', {innerHTML: 'Your public/private key is: <pre><code>' + localStorage[config.caps.shs + '/secret'] + '</code></pre>'},
232 h('button.btn', {onclick: function (e){
233 localStorage[config.caps.shs +'/secret'] = ''
234 alert('Your public/private key has been deleted')
235 e.preventDefault()
236 location.hash = ""
237 location.reload()
238 }}, 'Delete Key')
239 ),
240 h('hr'),
241 h('form',
242 importKey,
243 h('button.btn', {onclick: function (e){
244 if(importKey.value) {
245 localStorage[config.caps.shs + '/secret'] = importKey.value.replace(/\s+/g, ' ')
246 e.preventDefault()
247 alert('Your public/private key has been updated')
248 }
249 location.hash = ""
250 location.reload()
251 }}, 'Import key'),
252 )
253 )
254 )
255
256 screen.appendChild(hyperscroll(content))
257}
258
259
260function hash () {
261 return window.location.hash.substring(1)
262}
263
264module.exports = function () {
265 var src = hash()
266
267 if (ref.isFeed(src)) {
268 userStream(src)
269 } else if (ref.isMsg(src)) {
270 msgThread(src)
271 } else if (src == 'mentions') {
272 mentionsStream()
273 } else if (src == 'about') {
274 about()
275 } else if (src == 'edit') {
276 edit()
277 } else if (src == 'key') {
278 keyPage()
279 } else {
280 logStream()
281 }
282}
283

Built with git-ssb-web