Files: 9c6fb452a138c1a841302b2164284b0787c7bc3a / views.js
7242 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 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 | |
107 | var 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 | |
180 | var 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 }}}}]}), |
188 | pull.drain(function (msg) { |
189 | console.log(msg) |
190 | content.appendChild(render(msg)) |
191 | }) |
192 | ) |
193 | |
194 | sbot.get(src, function (err, data) { |
195 | if (err) {console.log('could not find message')} |
196 | data.value = data |
197 | data.key = src |
198 | console.log(data) |
199 | var rootMsg = render(data) |
200 | |
201 | if (content.firstChild) { |
202 | content.insertBefore(rootMsg, content.firstChild) |
203 | } else { |
204 | content.appendChild(rootMsg) |
205 | } |
206 | }) |
207 | |
208 | } |
209 | |
210 | var keyPage = function () { |
211 | var screen = document.getElementById('screen') |
212 | |
213 | var importKey = h('textarea.import', {placeholder: 'Import a new public/private key', name: 'textarea', style: 'width: 97%; height: 100px;'}) |
214 | |
215 | var content = h('div.content', |
216 | h('div.message#key', |
217 | h('h1', 'Your Key'), |
218 | h('p', {innerHTML: 'Your public/private key is: <pre><code>' + localStorage[config.caps.shs + '/secret'] + '</code></pre>'}, |
219 | h('button.btn', {onclick: function (e){ |
220 | localStorage[config.caps.shs +'/secret'] = '' |
221 | alert('Your public/private key has been deleted') |
222 | e.preventDefault() |
223 | location.hash = "" |
224 | location.reload() |
225 | }}, 'Delete Key') |
226 | ), |
227 | h('hr'), |
228 | h('form', |
229 | importKey, |
230 | h('button.btn', {onclick: function (e){ |
231 | if(importKey.value) { |
232 | localStorage[config.caps.shs + '/secret'] = importKey.value.replace(/\s+/g, ' ') |
233 | e.preventDefault() |
234 | alert('Your public/private key has been updated') |
235 | } |
236 | location.hash = "" |
237 | location.reload() |
238 | }}, 'Import key'), |
239 | ) |
240 | ) |
241 | ) |
242 | |
243 | screen.appendChild(hyperscroll(content)) |
244 | } |
245 | |
246 | function everythingStream () { |
247 | var content = h('div.content') |
248 | |
249 | var screen = document.getElementById('screen') |
250 | |
251 | screen.appendChild(hyperscroll(content)) |
252 | |
253 | function newStream () { |
254 | return pull( |
255 | sbot.query({query: [{$filter: { value: { timestamp: { $gt: 0 }}}}], old: false, live: true}), |
256 | pull.map(function (msg) { |
257 | return render(msg) |
258 | }) |
259 | ) |
260 | } |
261 | |
262 | function oldStream () { |
263 | return pull( |
264 | sbot.query({query: [{$filter: { value: { timestamp: { $gt: 0 }}}}], reverse: true, live: false}), |
265 | pull.map(function (msg) { |
266 | return render(msg) |
267 | }) |
268 | ) |
269 | } |
270 | |
271 | pull( |
272 | oldStream(), |
273 | stream.bottom(content) |
274 | ) |
275 | |
276 | pull( |
277 | newStream(), |
278 | stream.top(content) |
279 | ) |
280 | } |
281 | |
282 | function hash () { |
283 | return window.location.hash.substring(1) |
284 | } |
285 | |
286 | module.exports = function () { |
287 | var src = hash() |
288 | |
289 | if (ref.isFeed(src)) { |
290 | userStream(src) |
291 | } else if (ref.isMsg(src)) { |
292 | msgThread(src) |
293 | } else if (src == 'queue') { |
294 | mentionsStream() |
295 | } else if (src == 'about') { |
296 | about() |
297 | } else if (src == 'edit') { |
298 | edit() |
299 | } else if (src == 'key') { |
300 | keyPage() |
301 | } else { |
302 | everythingStream() |
303 | } |
304 | } |
305 |
Built with git-ssb-web