git ssb

1+

punkmonk.termux / mvd



forked from ev / mvd

Tree: 3af069778afcb0c83310f914e0db65aaad3209c8

Files: 3af069778afcb0c83310f914e0db65aaad3209c8 / views.js

7451 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 userStream = function (src) {
108 var content = h('div.content')
109 var screen = document.getElementById('screen')
110 screen.appendChild(hyperscroll(content))
111 function createStream (opts) {
112 return pull(
113 More(sbot.userStream, opts, ['value', 'sequence']),
114 pull.map(function (msg) {
115 return render(msg)
116 })
117 )
118 }
119
120 pull(
121 createStream({old: false, limit: 10, id: src}),
122 stream.top(content)
123 )
124
125 pull(
126 createStream({reverse: true, live: false, limit: 10, id: src}),
127 stream.bottom(content)
128 )
129
130
131 var profile = h('div.content#profile', h('div.message'))
132
133 if (screen.firstChild.firstChild) {
134 screen.firstChild.insertBefore(profile, screen.firstChild.firstChild)
135 } else {
136 screen.firstChild.appendChild(profile)
137 }
138
139 var avatars = h('div.avatars',
140 h('a', {href: '#' + src},
141 h('span.avatar--medium', avatar.image(src)),
142 avatar.name(src)
143 )
144 )
145
146 var buttons = h('div.buttons')
147
148
149 profile.firstChild.appendChild(avatars)
150 profile.firstChild.appendChild(buttons)
151
152 if (!localStorage[src])
153 var cache = {mute: false}
154 else
155 var cache = JSON.parse(localStorage[src])
156
157 console.log(cache)
158
159 if (cache.mute == true)
160 var mute = h('button.btn', 'Unmute', {
161 onclick: function () {
162 cache.mute = false
163 localStorage[src] = JSON.stringify(cache)
164 location.reload()
165 }
166 })
167 else
168 var mute = h('button.btn', 'Mute', {
169 onclick: function () {
170 cache.mute = true
171 localStorage[src] = JSON.stringify(cache)
172 location.reload()
173 }
174 })
175
176 buttons.appendChild(mute)
177
178}
179
180var msgThread = function (src) {
181
182 var content = h('div.content')
183 var screen = document.getElementById('screen')
184 screen.appendChild(hyperscroll(content))
185
186 pull(
187 sbot.query({query: [{$filter: { value: { content: {root: src}, timestamp: { $gt: 1 }}}}], live: true}),
188 pull.drain(function (msg) {
189 content.appendChild(render(msg))
190 })
191 )
192
193 sbot.get(src, function (err, data) {
194 if (err) {console.log('could not find message')}
195 data.value = data
196 data.key = src
197 console.log(data)
198 var rootMsg = render(data)
199
200 if (content.firstChild) {
201 content.insertBefore(rootMsg, content.firstChild)
202 } else {
203 content.appendChild(rootMsg)
204 }
205 })
206}
207
208var keyPage = function () {
209 var screen = document.getElementById('screen')
210
211 var importKey = h('textarea.import', {placeholder: 'Import a new public/private key', name: 'textarea', style: 'width: 97%; height: 100px;'})
212
213 var content = h('div.content',
214 h('div.message#key',
215 h('h1', 'Your Key'),
216 h('p', {innerHTML: 'Your public/private key is: <pre><code>' + localStorage[config.caps.shs + '/secret'] + '</code></pre>'},
217 h('button.btn', {onclick: function (e){
218 localStorage[config.caps.shs +'/secret'] = ''
219 alert('Your public/private key has been deleted')
220 e.preventDefault()
221 location.hash = ""
222 location.reload()
223 }}, 'Delete Key')
224 ),
225 h('hr'),
226 h('form',
227 importKey,
228 h('button.btn', {onclick: function (e){
229 if(importKey.value) {
230 localStorage[config.caps.shs + '/secret'] = importKey.value.replace(/\s+/g, ' ')
231 e.preventDefault()
232 alert('Your public/private key has been updated')
233 }
234 location.hash = ""
235 location.reload()
236 }}, 'Import key'),
237 )
238 )
239 )
240
241 screen.appendChild(hyperscroll(content))
242}
243
244var nested = require('libnested')
245
246function everythingStream () {
247
248 var screen = document.getElementById('screen')
249 var content = h('div.content')
250
251 screen.appendChild(hyperscroll(content))
252
253 /*function createStream (opts) {
254 return pull(
255 More(sbot.query, opts, [{$filter: { value: { timestamp: { $gt: 0 }}}}]),
256 pull.map(function (msg) {
257 return render(msg)
258 })
259 )
260 }
261
262 pull(
263 createStream({ limit: 10, old: false}),
264 stream.top(content)
265 )
266
267 pull(
268 createStream({limit: 10, live: false, reverse: true}),
269 stream.bottom(content)
270 )*/
271
272 function createStream (opts) {
273 return pull(
274 sbot.query({query: [{$filter: { value: { timestamp: { $gt: 0 }}}}], reverse: true}),
275 pull.map(function (msg) {
276 return render(msg)
277 })
278 )
279 }
280
281 /*pull(
282 createStream({old: false}),
283 stream.top(content)
284 )*/
285
286 pull(
287 createStream(),
288 stream.bottom(content)
289 )
290}
291
292
293function hash () {
294 return window.location.hash.substring(1)
295}
296
297module.exports = function () {
298 var src = hash()
299
300 if (ref.isFeed(src)) {
301 userStream(src)
302 } else if (ref.isMsg(src)) {
303 msgThread(src)
304 } else if (src == 'queue') {
305 mentionsStream()
306 } else if (src == 'about') {
307 about()
308 } else if (src == 'edit') {
309 edit()
310 } else if (src == 'key') {
311 keyPage()
312 } else {
313 everythingStream()
314 }
315}
316

Built with git-ssb-web