Commit eb1f4dc0536fe618895a84955b5f44818876c5e1
refactoring for testing #122
andre alves garzia committed on 5/20/2018, 11:56:30 PMParent: c0a64989f4157a9fd83067b87859a94b12675f90
Files changed
app/page/settings.js | changed |
backup/async/exportIdentity.js | changed |
backup/async/importIdentity.js | deleted |
backup/html/backup.js | changed |
backup/index.js | changed |
ftu/app.js | changed |
ftu/index.js | changed |
app/page/settings.js | ||
---|---|---|
@@ -18,11 +18,9 @@ | ||
18 | 18 | 'settings.sync.get': 'first', |
19 | 19 | 'settings.sync.set': 'first', |
20 | 20 | 'settings.obs.get': 'first', |
21 | 21 | 'translations.sync.strings': 'first', |
22 | - 'backup.html.exportIdentityButton': 'first', | |
23 | - 'backup.html.importIdentityButton': 'first' | |
24 | - | |
22 | + 'backup.html.exportIdentityButton': 'first' | |
25 | 23 | }) |
26 | 24 | |
27 | 25 | const LANGUAGES = ['zh', 'en'] |
28 | 26 | |
@@ -50,9 +48,8 @@ | ||
50 | 48 | const feed = api.keys.sync.id() |
51 | 49 | const strings = api.translations.sync.strings() |
52 | 50 | const currentLanguage = api.settings.sync.get('language') |
53 | 51 | const exportIdentityButton = api.backup.html.exportIdentityButton() |
54 | - const importIdentityButton = api.backup.html.importIdentityButton() | |
55 | 52 | |
56 | 53 | const editProfile = () => api.history.sync.push({ |
57 | 54 | page: 'userEdit', |
58 | 55 | feed, |
@@ -98,10 +95,9 @@ | ||
98 | 95 | ]), |
99 | 96 | h('section -backup', [ |
100 | 97 | h('div.left', strings.backup.sectionName), |
101 | 98 | h('div.right', [ |
102 | - exportIdentityButton, | |
103 | - importIdentityButton | |
99 | + exportIdentityButton | |
104 | 100 | ]) |
105 | 101 | ]) |
106 | 102 | ]) |
107 | 103 | ]) |
backup/async/exportIdentity.js | ||
---|---|---|
@@ -16,11 +16,11 @@ | ||
16 | 16 | 'sbot.obs.connection': 'first' |
17 | 17 | }) |
18 | 18 | |
19 | 19 | exports.create = function (api) { |
20 | - return nest('backup.async.exportIdentity', (password, filename, cb) => { | |
20 | + return nest('backup.async.exportIdentity', (filename, cb) => { | |
21 | 21 | if ("undefined" == typeof filename) { |
22 | - cb() | |
22 | + cb(new Error('backup requires a filename')) | |
23 | 23 | } else { |
24 | 24 | |
25 | 25 | console.log(`should export identity to file ${filename}`) |
26 | 26 | |
@@ -32,18 +32,18 @@ | ||
32 | 32 | let feedId = api.keys.sync.id() |
33 | 33 | |
34 | 34 | sbot.latestSequence(feedId, (err, sequence) => { |
35 | 35 | |
36 | - if (err) throw err | |
36 | + if (err) return cb(err) | |
37 | 37 | |
38 | 38 | let data = { |
39 | - exportDate: new Date(), | |
39 | + exportDate: new Date().toISOString(), | |
40 | 40 | latestSequence: sequence, |
41 | 41 | peers: peers, |
42 | 42 | secret: secret |
43 | 43 | } |
44 | 44 | |
45 | - fs.writeFileSync(filename, JSON.stringify(data), "utf8") | |
45 | + fs.writeFileSync(filename, JSON.stringify(data, null, 2), "utf8") | |
46 | 46 | |
47 | 47 | cb() |
48 | 48 | }) |
49 | 49 | }) |
backup/async/importIdentity.js | ||
---|---|---|
@@ -1,23 +1,0 @@ | ||
1 | -const nest = require('depnest') | |
2 | -const { onceTrue } = require('mutant') | |
3 | -const path = require('path') | |
4 | -const fs = require('fs') | |
5 | -const os = require('os') | |
6 | -const config = require('../../config').create().config.sync.load() | |
7 | -const peersFile = path.join(config.path, "gossip.json") | |
8 | -const secretFile = path.join(config.path, "secret") | |
9 | - | |
10 | -// TODO: files should take into account env vars | |
11 | - | |
12 | -exports.gives = nest('backup.async.importIdentity') | |
13 | - | |
14 | -exports.create = function (api) { | |
15 | - return nest('backup.async.importIdentity', (importData, cb) => { | |
16 | - | |
17 | - fs.writeFileSync(peersFile, JSON.stringify(importData.peers), "utf8") | |
18 | - fs.writeFileSync(secretFile, importData.secret, "utf8") | |
19 | - | |
20 | - cb() | |
21 | - | |
22 | - }) | |
23 | -} |
backup/html/backup.js | ||
---|---|---|
@@ -1,13 +1,12 @@ | ||
1 | 1 | const nest = require('depnest') |
2 | -const { h, computed, Value, resolve } = require('mutant') | |
3 | -const electron = require('electron') | |
2 | +const { h, Value, resolve } = require('mutant') | |
4 | 3 | const { dialog } = require('electron').remote; |
5 | 4 | const path = require('path') |
6 | 5 | const fs = require('fs') |
7 | 6 | |
8 | 7 | exports.gives = nest({ |
9 | - 'backup.html': ['exportIdentityButton', 'importIdentityButton'] | |
8 | + 'backup.html': ['exportIdentityButton'] | |
10 | 9 | }) |
11 | 10 | |
12 | 11 | exports.needs = nest({ |
13 | 12 | 'app.html.lightbox': 'first', |
@@ -16,70 +15,25 @@ | ||
16 | 15 | 'backup.async.exportIdentity': 'first' |
17 | 16 | }) |
18 | 17 | |
19 | 18 | exports.create = (api) => { |
20 | - return nest('backup.html', { exportIdentityButton, importIdentityButton }) | |
19 | + return nest('backup.html.exportIdentityButton', () => { | |
21 | 20 | |
22 | - function exportIdentityButton() { | |
23 | 21 | const strings = api.translations.sync.strings() |
24 | 22 | |
25 | - let isOpen = Value(false) | |
26 | - let encryptionKeyRaw = Value('') | |
23 | + function exportAction() { | |
24 | + let feedFragment = api.keys.sync.id().slice(1, 6) | |
25 | + dialog.showSaveDialog( | |
26 | + { | |
27 | + title: strings.backup.export.dialog.title, | |
28 | + butttonLabel: strings.backup.export.dialog.label, | |
29 | + defaultPath: `ticktack-identity-${feedFragment}.backup`, | |
30 | + }, | |
31 | + (filename) => api.backup.async.exportIdentity(filename, () => console.log('exported')) | |
32 | + ) | |
33 | + } | |
27 | 34 | |
28 | - let encryptionKeyInput = h('textarea#encryptionKey', { | |
29 | - style: { | |
30 | - width: '90%' | |
31 | - }, | |
32 | - placeholder: strings.backup.export.passwordPlaceholder, | |
33 | - value: encryptionKeyRaw, | |
34 | - 'ev-input': () => encryptionKeyRaw.set(encryptionKeyInput.value), | |
35 | - }) | |
36 | - | |
37 | - let exportDialog = h('div.dialog', { | |
38 | - style: { | |
39 | - 'text-align': 'left' | |
40 | - } | |
41 | - }, | |
42 | - [ | |
43 | - h('div.message', [ | |
44 | - h('h1', strings.backup.export.header), | |
45 | - h('p', strings.backup.export.message[0]), | |
46 | - h('p', strings.backup.export.message[1]) | |
47 | - ]), | |
48 | - h('div.form', [ | |
49 | - encryptionKeyInput | |
50 | - ]), | |
51 | - h('div.actions', [ | |
52 | - h('Button', { 'ev-click': () => isOpen.set(false) }, strings.backup.export.cancelAction), | |
53 | - h('Button -primary', { | |
54 | - 'ev-click': () => { | |
55 | - dialog.showSaveDialog( | |
56 | - { | |
57 | - title: strings.backup.export.dialog.title, | |
58 | - butttonLabel: strings.backup.export.dialog.label, | |
59 | - defaultPath: 'ticktack-identity.backup', | |
60 | - }, | |
61 | - (filename) => api.backup.async.exportIdentity( | |
62 | - resolve(encryptionKeyRaw), filename, () => isOpen.set(false) | |
63 | - ) | |
64 | - ) | |
65 | - } | |
66 | - }, strings.backup.export.exportAction) | |
67 | - ]) | |
68 | - ]) | |
69 | - | |
70 | - let lb = api.app.html.lightbox(exportDialog, isOpen) | |
71 | - | |
72 | 35 | return h('div.backupKeys', [ |
73 | - h('Button -backup', { 'ev-click': () => isOpen.set(true) }, strings.backup.export.exportAction), | |
74 | - lb | |
36 | + h('Button -backup', { 'ev-click': exportAction }, strings.backup.export.exportAction) | |
75 | 37 | ]) |
76 | - } | |
77 | - | |
78 | - function importIdentityButton() { | |
79 | - const strings = api.translations.sync.strings() | |
80 | - | |
81 | - return h('div.backupKeys', [ | |
82 | - h('Button -backup', strings.backup.import.importAction) | |
83 | - ]) | |
84 | - } | |
38 | + }) | |
85 | 39 | } |
backup/index.js | ||
---|---|---|
@@ -3,7 +3,6 @@ | ||
3 | 3 | backup: require('./html/backup') |
4 | 4 | }, |
5 | 5 | async: { |
6 | 6 | exportIdentity: require('./async/exportIdentity'), |
7 | - importIdentity: require('./async/importIdentity') | |
8 | 7 | } |
9 | 8 | } |
ftu/app.js | ||
---|---|---|
@@ -1,5 +1,5 @@ | ||
1 | -const { h, Value, when, resolve, computed, Struct, watch } = require('mutant') | |
1 | +const { h, Value, when, resolve, computed, Struct, watch, throttle } = require('mutant') | |
2 | 2 | const nest = require('depnest') |
3 | 3 | const path = require('path') |
4 | 4 | const fs = require('fs') |
5 | 5 | const { remote } = require('electron') |
@@ -58,9 +58,9 @@ | ||
58 | 58 | ]) |
59 | 59 | ]) |
60 | 60 | |
61 | 61 | // This watcher is responsible for switching from FTU to Ticktack main app |
62 | - watch(state, s => { | |
62 | + watch(throttle(state, 500), s => { | |
63 | 63 | if (s.currentSequence >= s.latestSequence) { |
64 | 64 | console.log('all imported') |
65 | 65 | electron.ipcRenderer.send('import-completed') |
66 | 66 | } |
@@ -70,12 +70,20 @@ | ||
70 | 70 | // somehow the FTU started but the identity is already in place. |
71 | 71 | // treat it as a failed import and start importing... |
72 | 72 | console.log('resuming import') |
73 | 73 | let previousData = getImportData() |
74 | - state.latestSequence.set(previousData.latestSequence) | |
75 | - state.currentSequence.set(previousData.currentSequence) | |
76 | - isPresentingOptions.set(false) | |
77 | - observeSequence() | |
74 | + if (previousData === false) { | |
75 | + // there is a secret but there is no previous import data. | |
76 | + // so, we proceed as normal because we can't do anything else, | |
77 | + // it looks like a normal standard installation... | |
78 | + setImportData({ importing: false }) | |
79 | + electron.ipcRenderer.send('import-completed') | |
80 | + } else { | |
81 | + state.latestSequence.set(previousData.latestSequence) | |
82 | + state.currentSequence.set(previousData.currentSequence) | |
83 | + isPresentingOptions.set(false) | |
84 | + observeSequence() | |
85 | + } | |
78 | 86 | } |
79 | 87 | |
80 | 88 | var app = h('App', [ |
81 | 89 | h('Header', [ |
@@ -214,30 +222,25 @@ | ||
214 | 222 | if (err) { |
215 | 223 | console.error('problem starting client', err) |
216 | 224 | } else { |
217 | 225 | console.log('> sbot running!!!!') |
218 | - ssbServer.whoami((err, data) => { | |
219 | - console.log("whoami", data.id) | |
220 | 226 | |
221 | - var feedSource = ssbServer.createUserStream({ | |
222 | - live: true, | |
223 | - id: data.id | |
224 | - }) | |
227 | + var feedSource = ssbServer.createUserStream({ | |
228 | + live: true, | |
229 | + id: ssbServer.id | |
230 | + }) | |
225 | 231 | |
226 | - var valueLogger = pull.drain((msg) => { | |
227 | - console.log("msg", msg) | |
232 | + var valueLogger = pull.drain((msg) => { | |
233 | + let seq = _.get(msg, "value.sequence", false) | |
234 | + if (seq) { | |
235 | + state.currentSequence.set(seq) | |
236 | + } | |
237 | + }) | |
228 | 238 | |
229 | - let seq = _.get(msg, "value.sequence", false) | |
230 | - if (seq) { | |
231 | - state.currentSequence.set(seq) | |
232 | - } | |
233 | - }) | |
239 | + pull( | |
240 | + feedSource, | |
241 | + valueLogger, | |
242 | + ) | |
234 | 243 | |
235 | - pull( | |
236 | - feedSource, | |
237 | - valueLogger, | |
238 | - ) | |
239 | - | |
240 | - }) | |
241 | 244 | } |
242 | 245 | }) |
243 | 246 | } |
Built with git-ssb-web