git ssb

2+

ev / mvd



Tree: c0ee0528003bc98da07846b27cf4fc5ed3ea6832

Files: c0ee0528003bc98da07846b27cf4fc5ed3ea6832 / views.js

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

Built with git-ssb-web