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