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