Files: b7c122f32a21918d997f6a13cc0d2e51f3ab4bcc / views.js
6633 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 name = avatar.name(src) |
102 | |
103 | var avatars = h('div.avatars', |
104 | h('a', {href: '#' + src}, |
105 | h('span.avatar--medium', avatar.image(src)), |
106 | name |
107 | ) |
108 | ) |
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 ' + name.textContent, { |
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 ' + name.textContent, { |
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 | |
146 | var msgThread = function (src) { |
147 | |
148 | var content = h('div.content') |
149 | var screen = document.getElementById('screen') |
150 | screen.appendChild(hyperscroll(content)) |
151 | |
152 | pull( |
153 | sbot.query({query: [{$filter: { value: { content: {root: src}, timestamp: { $gt: 1 }}}}], live: true}), |
154 | pull.drain(function (msg) { |
155 | if (msg.value) { |
156 | content.appendChild(render(msg)) |
157 | } |
158 | }) |
159 | ) |
160 | |
161 | sbot.get(src, function (err, data) { |
162 | if (err) {console.log('could not find message')} |
163 | data.value = data |
164 | data.key = src |
165 | console.log(data) |
166 | var rootMsg = render(data) |
167 | |
168 | if (content.firstChild) { |
169 | content.insertBefore(rootMsg, content.firstChild) |
170 | } else { |
171 | content.appendChild(rootMsg) |
172 | } |
173 | }) |
174 | } |
175 | |
176 | var keyPage = function () { |
177 | var screen = document.getElementById('screen') |
178 | |
179 | var importKey = h('textarea.import', {placeholder: 'Import a new public/private key', name: 'textarea', style: 'width: 97%; height: 100px;'}) |
180 | |
181 | var content = h('div.content', |
182 | h('div.message#key', |
183 | h('h1', 'Your Key'), |
184 | h('p', {innerHTML: 'Your public/private key is: <pre><code>' + localStorage[config.caps.shs + '/secret'] + '</code></pre>'}, |
185 | h('button.btn', {onclick: function (e){ |
186 | localStorage[config.caps.shs +'/secret'] = '' |
187 | alert('Your public/private key has been deleted') |
188 | e.preventDefault() |
189 | location.hash = "" |
190 | location.reload() |
191 | }}, 'Delete Key') |
192 | ), |
193 | h('hr'), |
194 | h('form', |
195 | importKey, |
196 | h('button.btn', {onclick: function (e){ |
197 | if(importKey.value) { |
198 | localStorage[config.caps.shs + '/secret'] = importKey.value.replace(/\s+/g, ' ') |
199 | e.preventDefault() |
200 | alert('Your public/private key has been updated') |
201 | } |
202 | location.hash = "" |
203 | location.reload() |
204 | }}, 'Import key'), |
205 | ) |
206 | ) |
207 | ) |
208 | |
209 | screen.appendChild(hyperscroll(content)) |
210 | } |
211 | |
212 | function everythingStream () { |
213 | |
214 | var screen = document.getElementById('screen') |
215 | var content = h('div.content') |
216 | |
217 | screen.appendChild(hyperscroll(content)) |
218 | |
219 | function createStream (opts) { |
220 | return pull( |
221 | Next(sbot.query, opts, ['value', 'timestamp']), |
222 | pull.map(function (msg) { |
223 | if (msg.value) { |
224 | return render(msg) |
225 | } |
226 | }) |
227 | ) |
228 | } |
229 | |
230 | pull( |
231 | createStream({ |
232 | limit: 10, |
233 | reverse: true, |
234 | live: false, |
235 | query: [{$filter: { value: { timestamp: { $gt: 0 }}}}] |
236 | }), |
237 | stream.bottom(content) |
238 | ) |
239 | |
240 | pull( |
241 | createStream({ |
242 | limit: 10, |
243 | old: false, |
244 | live: true, |
245 | query: [{$filter: { value: { timestamp: { $gt: 0 }}}}] |
246 | }), |
247 | stream.top(content) |
248 | ) |
249 | } |
250 | |
251 | |
252 | function hash () { |
253 | return window.location.hash.substring(1) |
254 | } |
255 | |
256 | module.exports = function () { |
257 | var src = hash() |
258 | |
259 | if (ref.isFeed(src)) { |
260 | userStream(src) |
261 | } else if (ref.isMsg(src)) { |
262 | msgThread(src) |
263 | } else if (src == 'queue') { |
264 | mentionsStream() |
265 | } else if (src == 'about') { |
266 | about() |
267 | } else if (src == 'edit') { |
268 | edit() |
269 | } else if (src == 'key') { |
270 | keyPage() |
271 | } else { |
272 | everythingStream() |
273 | } |
274 | } |
275 |
Built with git-ssb-web