Commit aae76c66fea3fd35f5110467b6f2b7ea69dd5a8e
implemented sequence export
andre alves garzia committed on 5/13/2018, 6:48:44 PMParent: c24946ad74bf65f4ff10b27c737cc02628f88c75
Files changed
backup/async/exportIdentity.js | changed |
backup/html/backup.js | changed |
index.js | changed |
ftu/app.js | added |
ftu/app.mcss | added |
ftu/index.js | added |
backup/async/exportIdentity.js | ||
---|---|---|
@@ -1,5 +1,6 @@ | ||
1 | 1 | const nest = require('depnest') |
2 | +const { onceTrue } = require('mutant') | |
2 | 3 | const path = require('path') |
3 | 4 | const fs = require('fs') |
4 | 5 | const os = require('os') |
5 | 6 | const homedir = os.homedir() |
@@ -9,8 +10,14 @@ | ||
9 | 10 | |
10 | 11 | |
11 | 12 | exports.gives = nest('backup.async.exportIdentity') |
12 | 13 | |
14 | + | |
15 | +exports.needs = nest({ | |
16 | + 'keys.sync.id': 'first', | |
17 | + 'sbot.obs.connection': 'first' | |
18 | +}) | |
19 | + | |
13 | 20 | exports.create = function (api) { |
14 | 21 | return nest('backup.async.exportIdentity', (password, filename, cb) => { |
15 | 22 | if ("undefined" == typeof filename) { |
16 | 23 | cb() |
@@ -20,19 +27,28 @@ | ||
20 | 27 | |
21 | 28 | let peers = JSON.parse(fs.readFileSync(peersFile)) |
22 | 29 | let secret = fs.readFileSync(secretFile, "utf8") |
23 | 30 | |
31 | + onceTrue(api.sbot.obs.connection, sbot => { | |
24 | 32 | |
25 | - let data = { | |
26 | - exportDate: new Date(), | |
27 | - latestSequence: "", | |
28 | - peers: peers, | |
29 | - secret: secret | |
30 | - } | |
33 | + let feedId = api.keys.sync.id() | |
31 | 34 | |
32 | - fs.writeFileSync(filename, JSON.stringify(data), "utf8") | |
35 | + sbot.latestSequence(feedId, (err, sequence) => { | |
33 | 36 | |
34 | - cb() | |
37 | + if (err) throw err | |
38 | + | |
39 | + let data = { | |
40 | + exportDate: new Date(), | |
41 | + latestSequence: sequence, | |
42 | + peers: peers, | |
43 | + secret: secret | |
44 | + } | |
45 | + | |
46 | + fs.writeFileSync(filename, JSON.stringify(data), "utf8") | |
47 | + | |
48 | + cb() | |
49 | + }) | |
50 | + }) | |
35 | 51 | } |
36 | 52 | return true |
37 | 53 | }) |
38 | 54 | } |
backup/html/backup.js | ||
---|---|---|
@@ -18,9 +18,8 @@ | ||
18 | 18 | |
19 | 19 | exports.create = (api) => { |
20 | 20 | return nest('backup.html', { exportIdentityButton, importIdentityButton }) |
21 | 21 | |
22 | - let feed = api.keys.sync.id() | |
23 | 22 | let strings = api.translations.sync.strings() |
24 | 23 | |
25 | 24 | function exportIdentityButton() { |
26 | 25 | let isOpen = Value(false) |
index.js | ||
---|---|---|
@@ -3,8 +3,15 @@ | ||
3 | 3 | var electron = require('electron') |
4 | 4 | var Menu = electron.Menu |
5 | 5 | var Path = require('path') |
6 | 6 | |
7 | +// FTU needs | |
8 | +const fs = require('fs') | |
9 | +const Config = require('ssb-config/inject') | |
10 | +const appName = process.env.ssb_appname || 'ssb' | |
11 | +const config = Config(appName) | |
12 | +const isInstalled = fs.existsSync(Path.join(config.path, 'secret')) | |
13 | + | |
7 | 14 | var windows = {} |
8 | 15 | var quitting = false |
9 | 16 | |
10 | 17 | electron.app.on('ready', () => { |
@@ -32,10 +39,22 @@ | ||
32 | 39 | } |
33 | 40 | |
34 | 41 | Menu.setApplicationMenu(Menu.buildFromTemplate(menu)) |
35 | 42 | |
36 | - startBackgroundProcess() | |
43 | + // TODO: FTU must happen before this part. | |
44 | + if (!isInstalled) { | |
45 | + console.log('Ticktack or SSB not installed, run FTU') | |
46 | + openFTUWindow() | |
47 | + } else { | |
48 | + startBackgroundProcess() | |
49 | + } | |
37 | 50 | |
51 | + // FTU told app to create new identity, so proceed as normal | |
52 | + electron.ipcMain.once('create-new-identity', function (ev) { | |
53 | + console.log('create new identity') | |
54 | + startBackgroundProcess() | |
55 | + }) | |
56 | + | |
38 | 57 | // wait until server has started before opening main window |
39 | 58 | electron.ipcMain.once('server-started', function (ev, config) { |
40 | 59 | openMainWindow() |
41 | 60 | }) |
@@ -46,14 +65,14 @@ | ||
46 | 65 | |
47 | 66 | // allow inspecting of background process |
48 | 67 | electron.ipcMain.on('open-background-devtools', function (ev, config) { |
49 | 68 | if (windows.background) { |
50 | - windows.background.webContents.openDevTools({detach: true}) | |
69 | + windows.background.webContents.openDevTools({ detach: true }) | |
51 | 70 | } |
52 | 71 | }) |
53 | 72 | }) |
54 | 73 | |
55 | -function startBackgroundProcess () { | |
74 | +function startBackgroundProcess() { | |
56 | 75 | if (!windows.background) { |
57 | 76 | windows.background = openWindow(Path.join(__dirname, 'background-process.js'), { |
58 | 77 | connect: false, |
59 | 78 | center: true, |
@@ -71,9 +90,9 @@ | ||
71 | 90 | }) |
72 | 91 | } |
73 | 92 | } |
74 | 93 | |
75 | -function openMainWindow () { | |
94 | +function openMainWindow() { | |
76 | 95 | if (!windows.main) { |
77 | 96 | var windowState = WindowState({ |
78 | 97 | defaultWidth: 1024, |
79 | 98 | defaultHeight: 768 |
@@ -106,9 +125,44 @@ | ||
106 | 125 | }) |
107 | 126 | } |
108 | 127 | } |
109 | 128 | |
110 | -function openWindow (path, opts) { | |
129 | +function openFTUWindow() { | |
130 | + if (!windows.main) { | |
131 | + var windowState = WindowState({ | |
132 | + defaultWidth: 1024, | |
133 | + defaultHeight: 768 | |
134 | + }) | |
135 | + windows.main = openWindow(Path.join(__dirname, 'ftu', 'index.js'), { | |
136 | + minWidth: 800, | |
137 | + x: windowState.x, | |
138 | + y: windowState.y, | |
139 | + width: windowState.width, | |
140 | + height: windowState.height, | |
141 | + autoHideMenuBar: true, | |
142 | + title: 'Ticktack', | |
143 | + frame: false, | |
144 | + titleBarStyle: 'hidden', | |
145 | + show: true, | |
146 | + backgroundColor: '#EEE', | |
147 | + icon: './assets/icon.png' | |
148 | + }) | |
149 | + windowState.manage(windows.main) | |
150 | + windows.main.setSheetOffset(40) | |
151 | + windows.main.on('close', function (e) { | |
152 | + if (!quitting && process.platform === 'darwin') { | |
153 | + e.preventDefault() | |
154 | + windows.main.hide() | |
155 | + } | |
156 | + }) | |
157 | + windows.main.on('closed', function () { | |
158 | + windows.main = null | |
159 | + if (process.platform !== 'darwin') electron.app.quit() | |
160 | + }) | |
161 | + } | |
162 | +} | |
163 | + | |
164 | +function openWindow(path, opts) { | |
111 | 165 | var window = new electron.BrowserWindow(opts) |
112 | 166 | window.webContents.on('dom-ready', function () { |
113 | 167 | window.webContents.executeJavaScript(` |
114 | 168 | var electron = require('electron') |
ftu/app.js | ||
---|---|---|
@@ -1,0 +1,81 @@ | ||
1 | +const { h, Value } = require('mutant') | |
2 | +const nest = require('depnest') | |
3 | +const path = require('path') | |
4 | +const { remote } = require('electron') | |
5 | +const insertCss = require('insert-css') | |
6 | +const values = require('lodash/values') | |
7 | +const electron = require('electron') | |
8 | + | |
9 | + | |
10 | +exports.gives = nest('ftu.app') | |
11 | + | |
12 | +exports.needs = nest({ | |
13 | + 'styles.css': 'reduce', | |
14 | + 'translations.sync.strings': 'first' | |
15 | +}) | |
16 | + | |
17 | +exports.create = (api) => { | |
18 | + return nest({ | |
19 | + 'ftu.app': function app() { | |
20 | + | |
21 | + const css = values(api.styles.css()).join('\n') | |
22 | + insertCss(css) | |
23 | + | |
24 | + var app = h('App', [ | |
25 | + h('Header', [ | |
26 | + windowControls() | |
27 | + ]), | |
28 | + h('Page -ftu', [ | |
29 | + h('div.content', [ | |
30 | + h('h1', 'Welcome to Ticktack'), | |
31 | + h('p', 'Do you want to create a new identity or import one?'), | |
32 | + h('section', [ | |
33 | + h('div.left', h('Button', 'Import identity')), | |
34 | + h('div.right', h('Button', { 'ev-click': () => actionCreateNewOne() }, 'Create a new one')) | |
35 | + ]) | |
36 | + ]) | |
37 | + ]) | |
38 | + ]) | |
39 | + | |
40 | + return app | |
41 | + } | |
42 | + | |
43 | + }) | |
44 | + | |
45 | +} | |
46 | + | |
47 | +function actionCreateNewOne() { | |
48 | + electron.ipcRenderer.send('create-new-identity') | |
49 | +} | |
50 | + | |
51 | +function windowControls() { | |
52 | + if (process.platform === 'darwin') return | |
53 | + | |
54 | + const window = remote.getCurrentWindow() | |
55 | + const minimize = () => window.minimize() | |
56 | + const maximize = () => { | |
57 | + if (!window.isMaximized()) window.maximize() | |
58 | + else window.unmaximize() | |
59 | + } | |
60 | + const close = () => window.close() | |
61 | + | |
62 | + return h('div.window-controls', [ | |
63 | + h('img.min', { | |
64 | + src: assetPath('minimize.png'), | |
65 | + 'ev-click': minimize | |
66 | + }), | |
67 | + h('img.max', { | |
68 | + src: assetPath('maximize.png'), | |
69 | + 'ev-click': maximize | |
70 | + }), | |
71 | + h('img.close', { | |
72 | + src: assetPath('close.png'), | |
73 | + 'ev-click': close | |
74 | + }) | |
75 | + ]) | |
76 | +} | |
77 | + | |
78 | + | |
79 | +function assetPath(name) { | |
80 | + return path.join(__dirname, '../assets', name) | |
81 | +} |
ftu/app.mcss | ||
---|---|---|
@@ -1,0 +1,56 @@ | ||
1 | +App { | |
2 | + overflow: hidden | |
3 | + position: fixed | |
4 | + top: 0 | |
5 | + bottom: 0 | |
6 | + right: 0 | |
7 | + left: 0 | |
8 | +} | |
9 | + | |
10 | +Page -ftu { | |
11 | + margin-top: 0 | |
12 | + height: calc(100%) | |
13 | + | |
14 | + div.content { | |
15 | + section { | |
16 | + display: flex | |
17 | + align-content: center | |
18 | + | |
19 | + div { | |
20 | + padding: .5rem | |
21 | + | |
22 | + display: flex | |
23 | + align-items: center | |
24 | + } | |
25 | + | |
26 | + div.left { | |
27 | + flex-basis: 40% | |
28 | + justify-content: flex-end | |
29 | + } | |
30 | + div.right { | |
31 | + flex-basis: 60% | |
32 | + justify-content: flex-start | |
33 | + } | |
34 | + } | |
35 | + } | |
36 | +} | |
37 | + | |
38 | +Header { | |
39 | + div.window-controls { | |
40 | + position: fixed | |
41 | + right: 0 | |
42 | + z-index: 100 | |
43 | + | |
44 | + display: flex | |
45 | + | |
46 | + img { | |
47 | + padding: .5rem | |
48 | + cursor: pointer | |
49 | + :hover { | |
50 | + filter: drop-shadow(rgba(255, 255, 255, .5) 0 0 2px) | |
51 | + } | |
52 | + } | |
53 | + } | |
54 | +} | |
55 | + | |
56 | + |
ftu/index.js | ||
---|---|---|
@@ -1,0 +1,29 @@ | ||
1 | +const combine = require('depject') | |
2 | +const entry = require('depject/entry') | |
3 | +const nest = require('depnest') | |
4 | + | |
5 | + | |
6 | +// polyfills | |
7 | +require('setimmediate') | |
8 | + | |
9 | +// add inspect right click menu | |
10 | +require('../context-menu') | |
11 | + | |
12 | +// from more specialized to more general | |
13 | +const sockets = combine( | |
14 | + // need some modules first | |
15 | + { | |
16 | + styles: require('../styles'), | |
17 | + settings: require('patch-settings'), | |
18 | + translations: require('../translations/sync') | |
19 | + }, | |
20 | + { | |
21 | + app: require('./app') | |
22 | + } | |
23 | +) | |
24 | + | |
25 | +const api = entry(sockets, nest({ | |
26 | + 'ftu.app': 'first' | |
27 | +})) | |
28 | + | |
29 | +document.body.appendChild(api.ftu.app()) |
Built with git-ssb-web