Commit dc5f4dc6989f09141fae9235f7690c01ebdc9e74
Merge pull request #146 from ticktackim/export-full
wip export side PRAndre Alves Garzia authored on 5/28/2018, 1:38:49 AM
GitHub committed on 5/28/2018, 1:38:49 AM
Parent: 5d4d175f2895574c5670910407ca9436c1154f06
Parent: 2266bb60f9e3162b169687e66a63363af534ece5
Files changed
app/html/app.js | changed |
ftu/app.js | changed |
package-lock.json | changed |
package.json | changed |
ssb-server-ticktack.js | changed |
app/html/app.js | ||
---|---|---|
@@ -1,6 +1,6 @@ | ||
1 | 1 | const nest = require('depnest') |
2 | -const { h, Value } = require('mutant') | |
2 | +const { h, Value, onceTrue } = require('mutant') | |
3 | 3 | |
4 | 4 | exports.gives = nest('app.html.app') |
5 | 5 | |
6 | 6 | exports.needs = nest({ |
@@ -12,15 +12,17 @@ | ||
12 | 12 | 'keys.sync.id': 'first', |
13 | 13 | 'router.sync.router': 'first', |
14 | 14 | 'settings.sync.get': 'first', |
15 | 15 | 'settings.sync.set': 'first', |
16 | - | |
17 | 16 | 'invite.async.autofollow': 'first', |
18 | 17 | 'config.sync.load': 'first', |
19 | 18 | 'sbot.async.friendsGet': 'first', |
20 | - 'sbot.async.get': 'first' | |
19 | + 'sbot.async.get': 'first', | |
20 | + 'sbot.obs.connection': 'first' // EBT | |
21 | 21 | }) |
22 | 22 | |
23 | +var count = 0 // TODO - rm | |
24 | + | |
23 | 25 | exports.create = (api) => { |
24 | 26 | var view |
25 | 27 | |
26 | 28 | return nest({ |
@@ -33,12 +35,33 @@ | ||
33 | 35 | api.history.obs.location()(loc => console.log('location:', loc)) |
34 | 36 | |
35 | 37 | startApp() |
36 | 38 | |
39 | + onceTrue(api.sbot.obs.connection, server => getMySeq(server)) // EBT | |
40 | + | |
37 | 41 | return app |
38 | 42 | } |
39 | 43 | }) |
40 | 44 | |
45 | + function getMySeq (server) { | |
46 | + console.log('REMOVE THIS') | |
47 | + // server.ebt.peerStatus(server.id, err, data) => { | |
48 | + // server.latestSequence(server.id, (err, seq) => { | |
49 | + // console.log(`${count} mins`) | |
50 | + // count = count + 1 | |
51 | + // console.log('actual seq:', seq) | |
52 | + // console.log(JSON.stringify(data, null, 2)) | |
53 | + // console.log('ticktack pubs', [ | |
54 | + // '@7xMrWP8708+LDvaJrRMRQJEixWYp4Oipa9ohqY7+NyQ=.ed25519', | |
55 | + // '@MflVZCcOBOUe6BLrm/8TyirkTu9/JtdnIJALcd8v5bc=.ed25519' | |
56 | + // ]) | |
57 | + // console.log('------------------') | |
58 | + // }) | |
59 | + // }) | |
60 | + | |
61 | + // setTimeout(() => getMySeq(server), 60000) | |
62 | + } | |
63 | + | |
41 | 64 | function renderLocation (loc) { |
42 | 65 | var page = api.router.sync.router(loc) |
43 | 66 | if (page) { |
44 | 67 | view.set([ |
@@ -85,19 +108,25 @@ | ||
85 | 108 | console.log('no invites') |
86 | 109 | return |
87 | 110 | } |
88 | 111 | |
89 | - var self_id = api.config.sync.load().keys.id | |
90 | - api.sbot.async.friendsGet({dest: self_id}, function (err, friends) { | |
91 | - // if you have less than 5 followers, maybe use the autoinvite | |
92 | - if (Object.keys(friends).length <= 5) { | |
93 | - invites.forEach(invite => { | |
94 | - console.log('using invite:', invite) | |
95 | - api.invite.async.autofollow(invite, (err, follows) => { | |
96 | - if (err) console.error('Autofollow error:', err) | |
97 | - else console.log('Autofollow success', follows) | |
98 | - }) | |
112 | + useInvites(invites) | |
113 | + // TODO change it so that if you already have a bunch of friends you unfollow the pubs after they follow you? | |
114 | + | |
115 | + // var myKey = api.config.sync.load().keys.id | |
116 | + // api.sbot.async.friendsGet({dest: myKey}, function (err, friends) { | |
117 | + // // if you have less than 5 followers, maybe use the autoinvite | |
118 | + // if (Object.keys(friends).length <= 5) useInvites(invites) | |
119 | + // else console.log('no autoinvite - you have friends already') | |
120 | + // }) | |
121 | + | |
122 | + function useInvites (invites) { | |
123 | + invites.forEach(invite => { | |
124 | + console.log('using invite:', invite) | |
125 | + api.invite.async.autofollow(invite, (err, follows) => { | |
126 | + if (err) console.error('Autofollow error:', err) | |
127 | + else console.log('Autofollow success', follows) | |
99 | 128 | }) |
100 | - } else { console.log('no autoinvite - you have friends already') } | |
101 | - }) | |
129 | + }) | |
130 | + } | |
102 | 131 | } |
103 | 132 | } |
ftu/app.js | ||
---|---|---|
@@ -4,34 +4,38 @@ | ||
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 | // these initial values are overwritten by the identity file. |
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') |
24 | 29 | |
25 | 30 | exports.needs = nest({ |
26 | 31 | 'styles.css': 'reduce', |
27 | - 'translations.sync.strings': 'first', | |
32 | + 'translations.sync.strings': 'first' | |
28 | 33 | }) |
29 | 34 | |
30 | 35 | exports.create = (api) => { |
31 | 36 | return nest({ |
32 | - 'ftu.app': function app() { | |
33 | - | |
37 | + 'ftu.app': function app () { | |
34 | 38 | const strings = api.translations.sync.strings() |
35 | 39 | |
36 | 40 | const css = values(api.styles.css()).join('\n') |
37 | 41 | insertCss(css) |
@@ -50,24 +54,35 @@ | ||
50 | 54 | when(isBusy, busyMessage, actionButtons) |
51 | 55 | ]) |
52 | 56 | ]) |
53 | 57 | |
54 | - var importProcess = h('Page -ftu', [ | |
58 | + var importProgress = h('Page -ftu', [ | |
55 | 59 | h('div.content', [ |
56 | 60 | h('h1', strings.backup.import.header), |
57 | - 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 | + // add an arrow at the head of the completed part | |
68 | + return `${complete}>${incomplete} (${s.currentSequence}/ ${s.latestSequence})` | |
69 | + } | |
70 | + })(s.currentSequence) | |
71 | + })) | |
58 | 72 | ]) |
59 | 73 | ]) |
60 | 74 | |
61 | 75 | // This watcher is responsible for switching from FTU to Ticktack main app |
62 | 76 | watch(throttle(state, 500), s => { |
63 | - if (s.currentSequence >= s.latestSequence) { | |
77 | + if (s.currentSequence >= s.latestSequence && s.confirmedRemotely) { | |
64 | 78 | console.log('all imported') |
79 | + clearTimeout(checkerTimeout) | |
65 | 80 | electron.ipcRenderer.send('import-completed') |
66 | 81 | } |
67 | 82 | }) |
68 | 83 | |
69 | - if (fs.existsSync(path.join(configFolder, "secret"))) { | |
84 | + if (fs.existsSync(path.join(configFolder, 'secret'))) { | |
70 | 85 | // somehow the FTU started but the identity is already in place. |
71 | 86 | // treat it as a failed import and start importing... |
72 | 87 | console.log('resuming import') |
73 | 88 | let previousData = getImportData() |
@@ -88,9 +103,9 @@ | ||
88 | 103 | var app = h('App', [ |
89 | 104 | h('Header', [ |
90 | 105 | windowControls() |
91 | 106 | ]), |
92 | - when(isPresentingOptions, initialOptions, importProcess) | |
107 | + when(isPresentingOptions, initialOptions, importProgress) | |
93 | 108 | ]) |
94 | 109 | |
95 | 110 | return app |
96 | 111 | } |
@@ -102,27 +117,24 @@ | ||
102 | 117 | |
103 | 118 | observeSequence() |
104 | 119 | }) |
105 | 120 | |
106 | - | |
107 | - | |
108 | -function actionCreateNewOne() { | |
121 | +function actionCreateNewOne () { | |
109 | 122 | isBusy.set(true) |
110 | - const manifest = JSON.parse(fs.readFileSync(path.join(__dirname, "../manifest.json"))) | |
123 | + const manifest = JSON.parse(fs.readFileSync(path.join(__dirname, '../manifest.json'))) | |
111 | 124 | const manifestFile = path.join(configFolder, 'manifest.json') |
112 | 125 | if (!fs.existsSync(configFolder)) { |
113 | 126 | fs.mkdirSync(configFolder) |
114 | 127 | } |
115 | 128 | fs.writeFileSync(manifestFile, JSON.stringify(manifest)) |
116 | 129 | |
117 | - | |
118 | 130 | electron.ipcRenderer.send('create-new-identity') |
119 | 131 | } |
120 | 132 | |
121 | -function actionImportIdentity(strings) { | |
122 | - const peersFile = path.join(configFolder, "gossip.json") | |
123 | - const secretFile = path.join(configFolder, "secret") | |
124 | - const manifest = JSON.parse(fs.readFileSync(path.join(__dirname, "../manifest.json"))) | |
133 | +function actionImportIdentity (strings) { | |
134 | + const peersFile = path.join(configFolder, 'gossip.json') | |
135 | + const secretFile = path.join(configFolder, 'secret') | |
136 | + const manifest = JSON.parse(fs.readFileSync(path.join(__dirname, '../manifest.json'))) | |
125 | 137 | const manifestFile = path.join(configFolder, 'manifest.json') |
126 | 138 | |
127 | 139 | // place the other files first |
128 | 140 | dialog.showOpenDialog( |
@@ -132,19 +144,19 @@ | ||
132 | 144 | defaultPath: 'ticktack-identity.backup', |
133 | 145 | properties: ['openFile'] |
134 | 146 | }, |
135 | 147 | (filenames) => { |
136 | - if (typeof filenames !== "undefined") { | |
148 | + if (typeof filenames !== 'undefined') { | |
137 | 149 | let filename = filenames[0] |
138 | 150 | let data = JSON.parse(fs.readFileSync(filename)) |
139 | - if (data.hasOwnProperty("secret") && data.hasOwnProperty("peers") && data.hasOwnProperty("latestSequence")) { | |
151 | + if (data.hasOwnProperty('secret') && data.hasOwnProperty('peers') && data.hasOwnProperty('latestSequence')) { | |
140 | 152 | if (!fs.existsSync(configFolder)) { |
141 | 153 | fs.mkdirSync(configFolder) |
142 | 154 | } |
143 | 155 | |
144 | 156 | fs.writeFileSync(manifestFile, JSON.stringify(manifest)) |
145 | - fs.writeFileSync(peersFile, JSON.stringify(data.peers), "utf8") | |
146 | - fs.writeFileSync(secretFile, data.secret, "utf8") | |
157 | + fs.writeFileSync(peersFile, JSON.stringify(data.peers), 'utf8') | |
158 | + fs.writeFileSync(secretFile, data.secret, 'utf8') | |
147 | 159 | state.latestSequence.set(data.latestSequence) |
148 | 160 | state.currentSequence.set(0) |
149 | 161 | isPresentingOptions.set(false) |
150 | 162 | |
@@ -154,18 +166,18 @@ | ||
154 | 166 | setImportData(data) |
155 | 167 | |
156 | 168 | electron.ipcRenderer.send('import-identity') |
157 | 169 | } else { |
158 | - console.log("> bad export file") | |
170 | + console.log('> bad export file') | |
159 | 171 | console.log(data) |
160 | - alert("Bad Export File") | |
172 | + alert('Bad Export File') | |
161 | 173 | } |
162 | 174 | } |
163 | 175 | } |
164 | 176 | ) |
165 | 177 | } |
166 | 178 | |
167 | -function windowControls() { | |
179 | +function windowControls () { | |
168 | 180 | if (process.platform === 'darwin') return |
169 | 181 | |
170 | 182 | const window = remote.getCurrentWindow() |
171 | 183 | const minimize = () => window.minimize() |
@@ -190,15 +202,13 @@ | ||
190 | 202 | }) |
191 | 203 | ]) |
192 | 204 | } |
193 | 205 | |
194 | - | |
195 | -function assetPath(name) { | |
206 | +function assetPath (name) { | |
196 | 207 | return path.join(__dirname, '../assets', name) |
197 | 208 | } |
198 | 209 | |
199 | - | |
200 | -function getImportData() { | |
210 | +function getImportData () { | |
201 | 211 | var importFile = path.join(configFolder, 'importing.json') |
202 | 212 | if (fs.existsSync(importFile)) { |
203 | 213 | let data = JSON.parse(fs.readFileSync(importFile)) |
204 | 214 | return data || false |
@@ -206,41 +216,83 @@ | ||
206 | 216 | return false |
207 | 217 | } |
208 | 218 | } |
209 | 219 | |
210 | -function setImportData(data) { | |
220 | +function setImportData (data) { | |
211 | 221 | var importFile = path.join(configFolder, 'importing.json') |
212 | 222 | fs.writeFileSync(importFile, JSON.stringify(data)) |
213 | 223 | } |
214 | 224 | |
215 | -function observeSequence() { | |
225 | +function observeSequence () { | |
216 | 226 | const pull = require('pull-stream') |
217 | 227 | const Client = require('ssb-client') |
218 | 228 | const config = require('../config').create().config.sync.load() |
219 | - const _ = require('lodash') | |
220 | 229 | |
221 | 230 | Client(config.keys, config, (err, ssbServer) => { |
222 | - if (err) { | |
223 | - console.error('problem starting client', err) | |
224 | - } else { | |
225 | - console.log('> sbot running!!!!') | |
231 | + if (err) return console.error('problem starting client', err) | |
226 | 232 | |
227 | - var feedSource = ssbServer.createUserStream({ | |
228 | - live: true, | |
229 | - id: ssbServer.id | |
230 | - }) | |
233 | + console.log('> sbot running!!!!') | |
231 | 234 | |
232 | - var valueLogger = pull.drain((msg) => { | |
233 | - let seq = _.get(msg, "value.sequence", false) | |
235 | + ssbServer.gossip.peers((err, peers) => { | |
236 | + if (err) return console.error(err) | |
237 | + | |
238 | + connectToPeers(peers) | |
239 | + checkPeers() | |
240 | + }) | |
241 | + | |
242 | + // start listening to the my seq, and update the state | |
243 | + pull( | |
244 | + ssbServer.createUserStream({ live: true, id: ssbServer.id }), | |
245 | + pull.drain((msg) => { | |
246 | + let seq = get(msg, 'value.sequence', false) | |
234 | 247 | if (seq) { |
235 | 248 | state.currentSequence.set(seq) |
236 | 249 | } |
237 | 250 | }) |
251 | + ) | |
238 | 252 | |
239 | - pull( | |
240 | - feedSource, | |
241 | - valueLogger, | |
242 | - ) | |
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 | + } | |
243 | 258 | |
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 | + }) | |
244 | 267 | } |
268 | + function checkPeers () { | |
269 | + ssbServer.ebt.peerStatus(ssbServer.id, (err, data) => { | |
270 | + if (err) { | |
271 | + checkerTimeout = setTimeout(checkPeers, 5000) | |
272 | + return | |
273 | + } | |
274 | + | |
275 | + const latest = resolve(state.latestSequence) | |
276 | + | |
277 | + const remoteSeqs = Object.keys(data.peers) | |
278 | + .map(p => data.peers[p].seq) // get my seq reported by each peer | |
279 | + .filter(s => s >= latest) // only keep remote seq that confirm or update backup seq | |
280 | + .sort((a, b) => a > b ? -1 : 1) // order them | |
281 | + | |
282 | + console.log(remoteSeqs) | |
283 | + | |
284 | + const newLatest = remoteSeqs[0] | |
285 | + if (newLatest) { | |
286 | + state.latestSequence.set(newLatest) | |
287 | + | |
288 | + // if this value is confirmed remotely twice, assume safe | |
289 | + if (remoteSeqs.filter(s => s === newLatest).length >= 2) { | |
290 | + state.confirmedRemotely.set(true) | |
291 | + } | |
292 | + } | |
293 | + | |
294 | + checkerTimeout = setTimeout(checkPeers, 5000) | |
295 | + }) | |
296 | + } | |
245 | 297 | }) |
246 | -} | |
298 | +} |
package-lock.json | ||
---|---|---|
The diff is too large to show. Use a local git client to view these changes. Old file size: 289973 bytes New file size: 290240 bytes |
package.json | ||
---|---|---|
@@ -49,8 +49,9 @@ | ||
49 | 49 | "patch-profile": "^1.0.4", |
50 | 50 | "patch-settings": "^1.1.2", |
51 | 51 | "patch-suggest": "^1.1.0", |
52 | 52 | "patchcore": "^1.23.3", |
53 | + "progress-string": "^1.2.2", | |
53 | 54 | "pull-defer": "^0.2.2", |
54 | 55 | "pull-next": "^1.0.1", |
55 | 56 | "pull-next-step": "^1.0.0", |
56 | 57 | "pull-obv": "^1.3.0", |
@@ -65,9 +66,9 @@ | ||
65 | 66 | "ssb-backlinks": "^0.6.1", |
66 | 67 | "ssb-blobs": "^1.1.4", |
67 | 68 | "ssb-client": "4.5.2", |
68 | 69 | "ssb-config": "^2.2.0", |
69 | - "ssb-ebt": "^5.1.5", | |
70 | + "ssb-ebt": "^5.2.0", | |
70 | 71 | "ssb-friends": "^2.3.5", |
71 | 72 | "ssb-keys": "^7.0.10", |
72 | 73 | "ssb-markdown": "^3.3.0", |
73 | 74 | "ssb-mentions": "^0.4.0", |
ssb-server-ticktack.js | ||
---|---|---|
@@ -191,9 +191,9 @@ | ||
191 | 191 | delete opts.limit |
192 | 192 | // have to remove limit from the query otherwise Next stalls out if it doesn't get a new result |
193 | 193 | |
194 | 194 | const _source = pull( |
195 | - server.messagesByType(opts), | |
195 | + server.messagesByType(opts), // TODO - check/ note why I didn't use e.g. readComments | |
196 | 196 | pull.filter(makeFilter(blogIds)), |
197 | 197 | limit ? pull.take(limit) : true |
198 | 198 | ) |
199 | 199 |
Built with git-ssb-web