Files: 0fc4aa1df3882d6f515d9326b51183e7bd3361f1 / views.js
8733 bytesRaw
1 | var pull = require('pull-stream') |
2 | var sbot = require('./scuttlebot') |
3 | var hyperscroll = require('hyperscroll') |
4 | var More = require('pull-more') |
5 | var stream = require('hyperloadmore/stream') |
6 | var h = require('hyperscript') |
7 | var render = require('./render') |
8 | var ref = require('ssb-ref') |
9 | |
10 | var config = require('./config')() |
11 | |
12 | var avatar = require('./avatar') |
13 | var id = require('./keys').id |
14 | |
15 | var fs = require('fs') |
16 | |
17 | var compose = require('./compose') |
18 | |
19 | var 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 | |
29 | var 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 | |
82 | var queryStream = 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 | sbot.query({query: [{$filter: { value: {content: {type: 'post'}, timestamp: { $gt: 1 }}}}], reverse: true}), |
93 | //sbot.query({query: [{$filter: { value: { content: {type: 'post'}, timestamp: { $gt: 0, $lt: undefined }}}}], reverse: true}), |
94 | pull.map(function (msg) { |
95 | return render(msg) |
96 | }) |
97 | ) |
98 | } |
99 | |
100 | pull( |
101 | createStream({reverse: true, limit: 10}), |
102 | stream.bottom(content) |
103 | ) |
104 | } |
105 | |
106 | var mentionsStream = function () { |
107 | var content = h('div.content') |
108 | |
109 | var screen = document.getElementById('screen') |
110 | |
111 | screen.appendChild(hyperscroll(content)) |
112 | |
113 | function createStream (opts) { |
114 | return pull( |
115 | sbot.backlinks({query: [{$filter: {dest: id}}], reverse: true}), |
116 | pull.map(function (msg) { |
117 | //if (msg.value.private == true) |
118 | // return 'ignoring private message' |
119 | //else |
120 | return render(msg) |
121 | }) |
122 | ) |
123 | } |
124 | |
125 | pull( |
126 | createStream({reverse: true, limit: 10}), |
127 | stream.bottom(content) |
128 | ) |
129 | } |
130 | |
131 | var logStream = function () { |
132 | var content = h('div.content') |
133 | var screen = document.getElementById('screen') |
134 | screen.appendChild(hyperscroll(content)) |
135 | |
136 | function createStream (opts) { |
137 | return pull( |
138 | More(sbot.createLogStream, opts), |
139 | pull.map(function (msg) { |
140 | return render(msg) |
141 | }) |
142 | |
143 | ) |
144 | } |
145 | |
146 | pull( |
147 | createStream({old: false, limit: 10}), |
148 | stream.top(content) |
149 | ) |
150 | |
151 | pull( |
152 | createStream({reverse: true, live: false, limit: 10}), |
153 | stream.bottom(content) |
154 | ) |
155 | } |
156 | |
157 | var userStream = function (src) { |
158 | var content = h('div.content') |
159 | var screen = document.getElementById('screen') |
160 | screen.appendChild(hyperscroll(content)) |
161 | function createStream (opts) { |
162 | return pull( |
163 | More(sbot.userStream, opts, ['value', 'sequence']), |
164 | pull.map(function (msg) { |
165 | return render(msg) |
166 | }) |
167 | ) |
168 | } |
169 | |
170 | pull( |
171 | createStream({old: false, limit: 10, id: src}), |
172 | stream.top(content) |
173 | ) |
174 | |
175 | pull( |
176 | createStream({reverse: true, live: false, limit: 10, id: src}), |
177 | stream.bottom(content) |
178 | ) |
179 | |
180 | |
181 | var profile = h('div.content#profile', h('div.message')) |
182 | |
183 | if (screen.firstChild.firstChild) { |
184 | screen.firstChild.insertBefore(profile, screen.firstChild.firstChild) |
185 | } else { |
186 | screen.firstChild.appendChild(profile) |
187 | } |
188 | |
189 | var avatars = h('div.avatars', |
190 | h('a', {href: '#' + src}, |
191 | h('span.avatar--medium', avatar.image(src)), |
192 | avatar.name(src) |
193 | ) |
194 | ) |
195 | |
196 | var buttons = h('div.buttons') |
197 | |
198 | |
199 | profile.firstChild.appendChild(avatars) |
200 | profile.firstChild.appendChild(buttons) |
201 | |
202 | if (!localStorage[src]) |
203 | var cache = {mute: false} |
204 | else |
205 | var cache = JSON.parse(localStorage[src]) |
206 | |
207 | console.log(cache) |
208 | |
209 | if (cache.mute == true) |
210 | var mute = h('button.btn', 'Unmute', { |
211 | onclick: function () { |
212 | cache.mute = false |
213 | localStorage[src] = JSON.stringify(cache) |
214 | location.reload() |
215 | } |
216 | }) |
217 | else |
218 | var mute = h('button.btn', 'Mute', { |
219 | onclick: function () { |
220 | cache.mute = true |
221 | localStorage[src] = JSON.stringify(cache) |
222 | location.reload() |
223 | } |
224 | }) |
225 | |
226 | buttons.appendChild(mute) |
227 | |
228 | } |
229 | |
230 | var msgThread = function (src) { |
231 | |
232 | var content = h('div.content') |
233 | var screen = document.getElementById('screen') |
234 | screen.appendChild(hyperscroll(content)) |
235 | |
236 | pull( |
237 | sbot.query({query: [{$filter: { value: { content: {root: src}, timestamp: { $gt: 1 }}}}]}), |
238 | pull.drain(function (msg) { |
239 | console.log(msg) |
240 | content.appendChild(render(msg)) |
241 | }) |
242 | ) |
243 | |
244 | sbot.get(src, function (err, data) { |
245 | if (err) {console.log('could not find message')} |
246 | data.value = data |
247 | data.key = src |
248 | console.log(data) |
249 | var rootMsg = render(data) |
250 | |
251 | if (content.firstChild) { |
252 | content.insertBefore(rootMsg, content.firstChild) |
253 | } else { |
254 | content.appendChild(rootMsg) |
255 | } |
256 | }) |
257 | |
258 | /*pull( |
259 | sbot.backlinks({query: [{$filter: {dest: src}}]}), |
260 | pull.drain(function (msg) { |
261 | console.log(msg) |
262 | content.appendChild(render(msg)) |
263 | }) |
264 | )*/ |
265 | |
266 | } |
267 | |
268 | /*var msgThread = function(src) { |
269 | var content = h('div.content') |
270 | var screen = document.getElementById('screen') |
271 | screen.appendChild(hyperscroll(content)) |
272 | sbot.get(src, function (err, data) { |
273 | if (err) {console.log('could not find message') } |
274 | data.value = data |
275 | var root = src |
276 | if (data.value.content.root) |
277 | root = data.value.content.root |
278 | sbot.get(root, function (err, data) { |
279 | if (err) { console.log('could not find root')} |
280 | data.value = data |
281 | data.key = root |
282 | content.appendChild(render(data)) |
283 | pull( |
284 | sbot.links({rel: 'root', dest: root, values: true, keys: true, live: true}), |
285 | pull.drain(function (msg) { |
286 | if (msg.value) |
287 | content.appendChild(render(msg)) |
288 | }) |
289 | ) |
290 | }) |
291 | }) |
292 | }*/ |
293 | |
294 | var keyPage = function () { |
295 | var screen = document.getElementById('screen') |
296 | |
297 | var importKey = h('textarea.import', {placeholder: 'Import a new public/private key', name: 'textarea', style: 'width: 97%; height: 100px;'}) |
298 | |
299 | var content = h('div.content', |
300 | h('div.message#key', |
301 | h('h1', 'Your Key'), |
302 | h('p', {innerHTML: 'Your public/private key is: <pre><code>' + localStorage[config.caps.shs + '/secret'] + '</code></pre>'}, |
303 | h('button.btn', {onclick: function (e){ |
304 | localStorage[config.caps.shs +'/secret'] = '' |
305 | alert('Your public/private key has been deleted') |
306 | e.preventDefault() |
307 | location.hash = "" |
308 | location.reload() |
309 | }}, 'Delete Key') |
310 | ), |
311 | h('hr'), |
312 | h('form', |
313 | importKey, |
314 | h('button.btn', {onclick: function (e){ |
315 | if(importKey.value) { |
316 | localStorage[config.caps.shs + '/secret'] = importKey.value.replace(/\s+/g, ' ') |
317 | e.preventDefault() |
318 | alert('Your public/private key has been updated') |
319 | } |
320 | location.hash = "" |
321 | location.reload() |
322 | }}, 'Import key'), |
323 | ) |
324 | ) |
325 | ) |
326 | |
327 | screen.appendChild(hyperscroll(content)) |
328 | } |
329 | |
330 | |
331 | function hash () { |
332 | return window.location.hash.substring(1) |
333 | } |
334 | |
335 | module.exports = function () { |
336 | var src = hash() |
337 | |
338 | if (ref.isFeed(src)) { |
339 | userStream(src) |
340 | } else if (ref.isMsg(src)) { |
341 | msgThread(src) |
342 | } else if (src == 'all') { |
343 | logStream() |
344 | } else if (src == 'posts'){ |
345 | queryStream() |
346 | } else if (src == 'about') { |
347 | about() |
348 | } else if (src == 'edit') { |
349 | edit() |
350 | } else if (src == 'key') { |
351 | keyPage() |
352 | } else { |
353 | mentionsStream() |
354 | } |
355 | } |
356 |
Built with git-ssb-web