git ssb

1+

punkmonk.termux / mvd



forked from ev / mvd

Tree: c2b974ef1a490124a6ebec3f593d14d4410d9dad

Files: c2b974ef1a490124a6ebec3f593d14d4410d9dad / views.js

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

Built with git-ssb-web