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