1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-11-24 08:12:24 +02:00

Allow cancelling OAuth dance, and fixed status bar display issue

This commit is contained in:
Laurent Cozic 2017-10-24 18:03:12 +00:00
parent 3de107d937
commit 3f56404c25
8 changed files with 57 additions and 23 deletions

View File

@ -18,6 +18,7 @@ class Command extends BaseCommand {
super();
this.syncTarget_ = null;
this.releaseLockFn_ = null;
this.oneDriveApiUtils_ = null;
}
usage() {
@ -81,6 +82,14 @@ class Command extends BaseCommand {
throw error;
}
const cleanUp = () => {
cliUtils.redrawDone();
if (this.releaseLockFn_) {
this.releaseLockFn_();
this.releaseLockFn_ = null;
}
};
try {
this.syncTarget_ = Setting.value('sync.target');
if (args.options.target) this.syncTarget_ = args.options.target;
@ -88,12 +97,16 @@ class Command extends BaseCommand {
if (this.syncTarget_ == Setting.SYNC_TARGET_ONEDRIVE && !reg.syncHasAuth(this.syncTarget_)) {
app().gui().showConsole();
app().gui().maximizeConsole();
const oneDriveApiUtils = new OneDriveApiNodeUtils(reg.oneDriveApi());
const auth = await oneDriveApiUtils.oauthDance({
this.oneDriveApiUtils_ = new OneDriveApiNodeUtils(reg.oneDriveApi());
const auth = await this.oneDriveApiUtils_.oauthDance({
log: (...s) => { return this.stdout(...s); }
});
this.oneDriveApiUtils_ = null;
Setting.setValue('sync.3.auth', auth ? JSON.stringify(auth) : null);
if (!auth) return;
if (!auth) {
this.stdout(_('Authentication was not completed (did not receive an authentication token).'));
return cleanUp();
}
}
let sync = await reg.synchronizer(this.syncTarget_);
@ -135,18 +148,19 @@ class Command extends BaseCommand {
await app().refreshCurrentFolder();
} catch (error) {
cliUtils.redrawDone();
this.releaseLockFn_();
this.releaseLockFn_ = null;
cleanUp();
throw error;
}
cliUtils.redrawDone();
this.releaseLockFn_();
this.releaseLockFn_ = null;
cleanUp();
}
async cancel() {
if (this.oneDriveApiUtils_) {
this.oneDriveApiUtils_.cancelOAuthDance();
return;
}
const target = this.syncTarget_ ? this.syncTarget_ : Setting.value('sync.target');
cliUtils.redrawDone();

View File

@ -1,6 +1,7 @@
const BaseWidget = require('tkwidgets/BaseWidget.js');
const chalk = require('chalk');
const termutils = require('tkwidgets/framework/termutils.js');
const stripAnsi = require('strip-ansi');
class StatusBarWidget extends BaseWidget {
@ -22,7 +23,7 @@ class StatusBarWidget extends BaseWidget {
}
setItemAt(index, text) {
this.items_[index] = text;
this.items_[index] = stripAnsi(text).trim();
this.invalidate();
}
@ -33,8 +34,8 @@ class StatusBarWidget extends BaseWidget {
this.promptState_ = {
promise: null,
initialText: initialText,
promptString: promptString,
initialText: stripAnsi(initialText),
promptString: stripAnsi(promptString),
};
this.promptState_.promise = new Promise((resolve, reject) => {
@ -133,7 +134,8 @@ class StatusBarWidget extends BaseWidget {
} else {
for (let i = 0; i < this.items_.length; i++) {
this.term.write(textStyle(this.items_[i].trim()));
const s = this.items_[i].substr(0, this.innerWidth - 1);
this.term.write(textStyle(s));
}
}

View File

@ -10,6 +10,7 @@ class OneDriveApiNodeUtils {
constructor(api) {
this.api_ = api;
this.oauthServer_ = null;
}
api() {
@ -32,13 +33,16 @@ class OneDriveApiNodeUtils {
return header + message + footer;
}
cancelOAuthDance() {
if (!this.oauthServer_) return;
this.oauthServer_.destroy();
}
async oauthDance(targetConsole = null) {
if (targetConsole === null) targetConsole = console;
this.api().setAuth(null);
let ports = this.possibleOAuthDancePorts();
let port = null;
for (let i = 0; i < ports.length; i++) {
@ -54,10 +58,10 @@ class OneDriveApiNodeUtils {
let authCodeUrl = this.api().authCodeUrl('http://localhost:' + port);
return new Promise((resolve, reject) => {
let server = http.createServer();
this.oauthServer_ = http.createServer();
let errorMessage = null;
server.on('request', (request, response) => {
this.oauthServer_.on('request', (request, response) => {
const query = urlParser.parse(request.url, true).query;
const writeResponse = (code, message) => {
@ -71,7 +75,8 @@ class OneDriveApiNodeUtils {
// though it worked).
const waitAndDestroy = () => {
setTimeout(() => {
server.destroy();
this.oauthServer_.destroy();
this.oauthServer_ = null;
}, 1000);
}
@ -90,7 +95,7 @@ class OneDriveApiNodeUtils {
});
});
server.on('close', () => {
this.oauthServer_.on('close', () => {
if (errorMessage) {
reject(new Error(errorMessage));
} else {
@ -98,9 +103,9 @@ class OneDriveApiNodeUtils {
}
});
server.listen(port);
this.oauthServer_.listen(port);
enableServerDestroy(server);
enableServerDestroy(this.oauthServer_);
targetConsole.log(_('Please open the following URL in your browser to authenticate the application. The application will create a directory in "Apps/Joplin" and will only read and write files in this directory. It will have no access to any files outside this directory nor to any other personal data. No data will be shared with any third party.'));
targetConsole.log('');

View File

@ -390,6 +390,10 @@ msgid ""
"operation."
msgstr ""
msgid ""
"Authentication was not completed (did not receive an authentication token)."
msgstr ""
#, javascript-format
msgid "Synchronisation target: %s (%s)"
msgstr ""

View File

@ -436,6 +436,10 @@ msgid ""
"operation."
msgstr ""
msgid ""
"Authentication was not completed (did not receive an authentication token)."
msgstr ""
#, javascript-format
msgid "Synchronisation target: %s (%s)"
msgstr "Cible de la synchronisation : %s (%s)"

View File

@ -390,6 +390,10 @@ msgid ""
"operation."
msgstr ""
msgid ""
"Authentication was not completed (did not receive an authentication token)."
msgstr ""
#, javascript-format
msgid "Synchronisation target: %s (%s)"
msgstr ""

View File

@ -53,6 +53,7 @@
"sqlite3": "^3.1.8",
"string-padding": "^1.0.2",
"string-to-stream": "^1.1.0",
"strip-ansi": "^4.0.0",
"tcp-port-used": "^0.1.2",
"tkwidgets": "^0.5.3",
"uuid": "^3.0.1",

View File

@ -1815,9 +1815,9 @@ node-bitmap@0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/node-bitmap/-/node-bitmap-0.0.1.tgz#180eac7003e0c707618ef31368f62f84b2a69091"
"node-emoji@git+https://github.com/laurent22/node-emoji.git":
"node-emoji@https://github.com/laurent22/node-emoji":
version "1.8.1"
resolved "git+https://github.com/laurent22/node-emoji.git#9fa01eac463e94dde1316ef8c53089eeef4973b5"
resolved "https://github.com/laurent22/node-emoji#9fa01eac463e94dde1316ef8c53089eeef4973b5"
dependencies:
lodash.toarray "^4.4.0"