git ssb

2+

mixmix / ticktack



Commit bc9472388e14fcf3a02a4397b3d69d16bf96390d

refactoring for testing #122

andre alves garzia committed on 5/21/2018, 12:03:23 AM
Parent: 34eba24a618f2a4d20c291a7be43c692b6b97549

Files changed

app/page/settings.jschanged
backup/async/exportIdentity.jschanged
backup/async/importIdentity.jsdeleted
backup/html/backup.jschanged
backup/index.jschanged
ftu/app.jschanged
ftu/index.jschanged
app/page/settings.jsView
@@ -18,11 +18,9 @@
1818 'settings.sync.get': 'first',
1919 'settings.sync.set': 'first',
2020 'settings.obs.get': 'first',
2121 'translations.sync.strings': 'first',
22- 'backup.html.exportIdentityButton': 'first',
23- 'backup.html.importIdentityButton': 'first'
24-
22+ 'backup.html.exportIdentityButton': 'first'
2523 })
2624
2725 const LANGUAGES = ['zh', 'en']
2826
@@ -45,9 +43,8 @@
4543 const feed = api.keys.sync.id()
4644 const strings = api.translations.sync.strings()
4745 const currentLanguage = api.settings.sync.get('language')
4846 const exportIdentityButton = api.backup.html.exportIdentityButton()
49- const importIdentityButton = api.backup.html.importIdentityButton()
5047
5148 const editProfile = () => api.history.sync.push({
5249 page: 'userEdit',
5350 feed,
@@ -105,10 +102,9 @@
105102 ]),
106103 h('section -backup', [
107104 h('div.left', strings.backup.sectionName),
108105 h('div.right', [
109- exportIdentityButton,
110- importIdentityButton
106+ exportIdentityButton
111107 ])
112108 ])
113109 ])
114110 ])
backup/async/exportIdentity.jsView
@@ -16,11 +16,11 @@
1616 'sbot.obs.connection': 'first'
1717 })
1818
1919 exports.create = function (api) {
20- return nest('backup.async.exportIdentity', (password, filename, cb) => {
20+ return nest('backup.async.exportIdentity', (filename, cb) => {
2121 if ("undefined" == typeof filename) {
22- cb()
22+ cb(new Error('backup requires a filename'))
2323 } else {
2424
2525 console.log(`should export identity to file ${filename}`)
2626
@@ -32,18 +32,18 @@
3232 let feedId = api.keys.sync.id()
3333
3434 sbot.latestSequence(feedId, (err, sequence) => {
3535
36- if (err) throw err
36+ if (err) return cb(err)
3737
3838 let data = {
39- exportDate: new Date(),
39+ exportDate: new Date().toISOString(),
4040 latestSequence: sequence,
4141 peers: peers,
4242 secret: secret
4343 }
4444
45- fs.writeFileSync(filename, JSON.stringify(data), "utf8")
45+ fs.writeFileSync(filename, JSON.stringify(data, null, 2), "utf8")
4646
4747 cb()
4848 })
4949 })
backup/async/importIdentity.jsView
@@ -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.jsView
@@ -1,13 +1,12 @@
11 const nest = require('depnest')
2-const { h, computed, Value, resolve } = require('mutant')
3-const electron = require('electron')
2+const { h, Value, resolve } = require('mutant')
43 const { dialog } = require('electron').remote;
54 const path = require('path')
65 const fs = require('fs')
76
87 exports.gives = nest({
9- 'backup.html': ['exportIdentityButton', 'importIdentityButton']
8+ 'backup.html': ['exportIdentityButton']
109 })
1110
1211 exports.needs = nest({
1312 'app.html.lightbox': 'first',
@@ -16,70 +15,25 @@
1615 'backup.async.exportIdentity': 'first'
1716 })
1817
1918 exports.create = (api) => {
20- return nest('backup.html', { exportIdentityButton, importIdentityButton })
19+ return nest('backup.html.exportIdentityButton', () => {
2120
22- function exportIdentityButton() {
2321 const strings = api.translations.sync.strings()
2422
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+ }
2734
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-
7235 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)
7537 ])
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+ })
8539 }
backup/index.jsView
@@ -3,7 +3,6 @@
33 backup: require('./html/backup')
44 },
55 async: {
66 exportIdentity: require('./async/exportIdentity'),
7- importIdentity: require('./async/importIdentity')
87 }
98 }
ftu/app.jsView
@@ -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')
22 const nest = require('depnest')
33 const path = require('path')
44 const fs = require('fs')
55 const { remote } = require('electron')
@@ -58,9 +58,9 @@
5858 ])
5959 ])
6060
6161 // This watcher is responsible for switching from FTU to Ticktack main app
62- watch(state, s => {
62+ watch(throttle(state, 500), s => {
6363 if (s.currentSequence >= s.latestSequence) {
6464 console.log('all imported')
6565 electron.ipcRenderer.send('import-completed')
6666 }
@@ -70,12 +70,20 @@
7070 // somehow the FTU started but the identity is already in place.
7171 // treat it as a failed import and start importing...
7272 console.log('resuming import')
7373 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+ }
7886 }
7987
8088 var app = h('App', [
8189 h('Header', [
@@ -214,30 +222,25 @@
214222 if (err) {
215223 console.error('problem starting client', err)
216224 } else {
217225 console.log('> sbot running!!!!')
218- ssbServer.whoami((err, data) => {
219- console.log("whoami", data.id)
220226
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+ })
225231
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+ })
228238
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+ )
234243
235- pull(
236- feedSource,
237- valueLogger,
238- )
239-
240- })
241244 }
242245 })
243246 }
ftu/index.jsView
@@ -13,9 +13,8 @@
1313 const sockets = combine(
1414 // need some modules first
1515 {
1616 styles: require('../styles'),
17- settings: require('patch-settings'),
1817 translations: require('../translations/sync'),
1918 },
2019 {
2120 app: require('./app')

Built with git-ssb-web