ftu/app.jsView |
---|
4 | 4 | const fs = require('fs') |
5 | 5 | const { remote } = require('electron') |
6 | 6 | const insertCss = require('insert-css') |
7 | 7 | const values = require('lodash/values') |
| 8 | +const get = require('lodash/get') |
8 | 9 | const electron = require('electron') |
9 | 10 | const { dialog } = require('electron').remote |
10 | 11 | const os = require('os') |
| 12 | +const progress = require('progress-string') |
| 13 | + |
11 | 14 | const appName = process.env.SSB_APPNAME || 'ssb' |
12 | 15 | const configFolder = path.join(os.homedir(), `.${appName}`) |
13 | 16 | |
14 | 17 | var isBusy = Value(false) |
15 | 18 | var isPresentingOptions = Value(true) |
| 19 | +var checkerTimeout |
16 | 20 | |
17 | 21 | |
18 | 22 | var state = Struct({ |
19 | 23 | latestSequence: 0, |
| 24 | + confirmedRemotely: false, |
20 | 25 | currentSequence: -1 |
21 | 26 | }) |
22 | 27 | |
23 | 28 | exports.gives = nest('ftu.app') |
49 | 54 | when(isBusy, busyMessage, actionButtons) |
50 | 55 | ]) |
51 | 56 | ]) |
52 | 57 | |
53 | | - var importProcess = h('Page -ftu', [ |
| 58 | + var importProgress = h('Page -ftu', [ |
54 | 59 | h('div.content', [ |
55 | 60 | h('h1', strings.backup.import.header), |
56 | | - h('p', [strings.backup.import.synchronizeMessage, state.currentSequence, '/', state.latestSequence]) |
| 61 | + h('p', [strings.backup.import.synchronizeMessage]), |
| 62 | + h('pre', computed(state, s => { |
| 63 | + return progress({ |
| 64 | + width: 42, |
| 65 | + total: s.latestSequence, |
| 66 | + style: function (complete, incomplete) { |
| 67 | + |
| 68 | + return `${complete}>${incomplete} (${s.currentSequence}/ ${s.latestSequence})` |
| 69 | + } |
| 70 | + })(s.currentSequence) |
| 71 | + })) |
57 | 72 | ]) |
58 | 73 | ]) |
59 | 74 | |
60 | 75 | |
61 | 76 | watch(throttle(state, 500), s => { |
62 | | - if (s.currentSequence >= s.latestSequence) { |
| 77 | + if (s.currentSequence >= s.latestSequence && s.confirmedRemotely) { |
63 | 78 | console.log('all imported') |
| 79 | + clearTimeout(checkerTimeout) |
64 | 80 | electron.ipcRenderer.send('import-completed') |
65 | 81 | } |
66 | 82 | }) |
67 | 83 | |
87 | 103 | var app = h('App', [ |
88 | 104 | h('Header', [ |
89 | 105 | windowControls() |
90 | 106 | ]), |
91 | | - when(isPresentingOptions, initialOptions, importProcess) |
| 107 | + when(isPresentingOptions, initialOptions, importProgress) |
92 | 108 | ]) |
93 | 109 | |
94 | 110 | return app |
95 | 111 | } |
209 | 225 | function observeSequence () { |
210 | 226 | const pull = require('pull-stream') |
211 | 227 | const Client = require('ssb-client') |
212 | 228 | const config = require('../config').create().config.sync.load() |
213 | | - const _ = require('lodash') |
214 | 229 | |
215 | 230 | Client(config.keys, config, (err, ssbServer) => { |
216 | | - if (err) { |
217 | | - console.error('problem starting client', err) |
218 | | - } else { |
219 | | - console.log('> sbot running!!!!') |
| 231 | + if (err) return console.error('problem starting client', err) |
220 | 232 | |
221 | | - ssbServer.gossip.peers((err, data) => { |
222 | | - console.log('PEERS', err, data) |
| 233 | + console.log('> sbot running!!!!') |
223 | 234 | |
224 | | - data.forEach(peer => { |
225 | | - ssbServer.gossip.connect({ |
226 | | - 'host': peer.host, |
227 | | - 'port': peer.port, |
228 | | - 'key': peer.key |
229 | | - }, function (err, v) { |
230 | | - console.log('connected to ', peer.host) |
231 | | - }) |
232 | | - }) |
| 235 | + ssbServer.gossip.peers((err, peers) => { |
| 236 | + if (err) return console.error(err) |
| 237 | + |
| 238 | + connectToPeers(peers) |
| 239 | + checkPeers() |
| 240 | + }) |
| 241 | + |
| 242 | + |
| 243 | + pull( |
| 244 | + ssbServer.createUserStream({ live: true, id: ssbServer.id }), |
| 245 | + pull.drain((msg) => { |
| 246 | + let seq = get(msg, 'value.sequence', false) |
| 247 | + if (seq) { |
| 248 | + state.currentSequence.set(seq) |
| 249 | + } |
233 | 250 | }) |
| 251 | + ) |
234 | 252 | |
235 | | - |
236 | | - |
237 | | - |
238 | | - |
239 | | - |
240 | | - |
241 | | - |
| 253 | + function connectToPeers (peers) { |
| 254 | + if (peers.length > 10) { |
| 255 | + const lessPeers = peers.filter(p => !p.error) |
| 256 | + if (lessPeers.length > 10) peers = lessPeers |
| 257 | + } |
242 | 258 | |
243 | | - |
244 | | - |
245 | | - |
246 | | - |
247 | | - |
248 | | - |
249 | | - |
| 259 | + peers.forEach(({ host, port, key }) => { |
| 260 | + if (host && port && key) { |
| 261 | + ssbServer.gossip.connect({ host, port, key }, (err, v) => { |
| 262 | + if (err) console.log('error connecting to ', host, err) |
| 263 | + else console.log('connected to ', host) |
| 264 | + }) |
| 265 | + } |
| 266 | + }) |
| 267 | + } |
| 268 | + function checkPeers () { |
| 269 | + ssbServer.ebt.peerStatus(ssbServer.id, (err, data) => { |
| 270 | + if (err) { |
| 271 | + checkerTimeout = setTimeout(checkPeers, 5000) |
| 272 | + return |
| 273 | + } |
250 | 274 | |
251 | | - |
252 | | - |
253 | | - |
254 | | - |
255 | | - |
256 | | - |
257 | | - |
258 | | - |
| 275 | + const latest = resolve(state.latestSequence) |
259 | 276 | |
260 | | - function checkPeers () { |
261 | | - ssbServer.ebt.peerStatus(ssbServer.id, (err, data) => { |
262 | | - console.log('PEER STATUS:') |
263 | | - console.log(data.seq) |
264 | | - console.log(data.peers) |
265 | | - console.log('-------') |
266 | | - }) |
| 277 | + const remoteSeqs = Object.keys(data.peers) |
| 278 | + .map(p => data.peers[p].seq) |
| 279 | + .filter(s => s >= latest) |
| 280 | + .sort((a, b) => a > b ? -1 : 1) |
267 | 281 | |
268 | | - setTimeout(checkPeers, 5000) |
269 | | - } |
| 282 | + console.log(remoteSeqs) |
270 | 283 | |
271 | | - var feedSource = ssbServer.createUserStream({ |
272 | | - live: true, |
273 | | - id: ssbServer.id |
274 | | - }) |
| 284 | + const newLatest = remoteSeqs[0] |
| 285 | + if (newLatest) { |
| 286 | + state.latestSequence.set(newLatest) |
275 | 287 | |
276 | | - var valueLogger = pull.drain((msg) => { |
277 | | - let seq = _.get(msg, 'value.sequence', false) |
278 | | - if (seq) { |
279 | | - state.currentSequence.set(seq) |
| 288 | + |
| 289 | + if (remoteSeqs.filter(s => s === newLatest).length >= 2) { |
| 290 | + state.confirmedRemotely.set(true) |
| 291 | + } |
280 | 292 | } |
| 293 | + |
| 294 | + checkerTimeout = setTimeout(checkPeers, 5000) |
281 | 295 | }) |
282 | | - |
283 | | - pull( |
284 | | - feedSource, |
285 | | - valueLogger |
286 | | - ) |
287 | 296 | } |
288 | 297 | }) |
289 | 298 | } |