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