Files: 84ba00f843d2ade9e33ea85b6ece83da282514c0 / views.js
7330 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 Next = require('pull-next-query') |
11 | |
12 | var config = require('./config')() |
13 | |
14 | var avatar = require('./avatar') |
15 | var id = require('./keys').id |
16 | |
17 | var fs = require('fs') |
18 | |
19 | var compose = require('./compose') |
20 | |
21 | var 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 | |
31 | var 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 | |
84 | var 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 | |
109 | var 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 | |
182 | var 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 | |
212 | var 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 | |
248 | function 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 | |
288 | function hash () { |
289 | return window.location.hash.substring(1) |
290 | } |
291 | |
292 | module.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