From 6e94edfcb5b97c8fda2cca81ee44a5dee5c05f42 Mon Sep 17 00:00:00 2001 From: Andrew Ferrazzutti Date: Wed, 10 Feb 2021 03:24:28 -0500 Subject: [PATCH] Use functions that return a Promise --- mautrix_line/commands/auth.py | 7 ++++--- mautrix_line/rpc/client.py | 2 +- puppet/src/puppet.js | 30 ++++++++++++++---------------- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/mautrix_line/commands/auth.py b/mautrix_line/commands/auth.py index 308f913..584eb45 100644 --- a/mautrix_line/commands/auth.py +++ b/mautrix_line/commands/auth.py @@ -82,10 +82,11 @@ async def login_do(evt: CommandEvent, gen: AsyncGenerator[Tuple[str, str], None] pin_event_id = await evt.az.intent.send_message(evt.room_id, content) elif item[0] in ("failure", "error"): # TODO Handle errors differently? - reason = item[1] failure = True - content = TextMessageEventContent(body=reason, msgtype=MessageType.NOTICE) - await evt.az.intent.send_message(evt.room_id, content) + reason = item[1] + if reason: + content = TextMessageEventContent(body=reason, msgtype=MessageType.NOTICE) + await evt.az.intent.send_message(evt.room_id, content) # else: pass if not failure and evt.sender.command_status: diff --git a/mautrix_line/rpc/client.py b/mautrix_line/rpc/client.py index e5232ed..7c80619 100644 --- a/mautrix_line/rpc/client.py +++ b/mautrix_line/rpc/client.py @@ -79,7 +79,7 @@ class Client(RPCClient): event.set() async def failure_handler(req: LoginCommand) -> None: - data.append(("failure", req["reason"])) + data.append(("failure", req.get("reason"))) event.set() async def cancel_watcher() -> None: diff --git a/puppet/src/puppet.js b/puppet/src/puppet.js index 25c5764..0923d5f 100644 --- a/puppet/src/puppet.js +++ b/puppet/src/puppet.js @@ -150,7 +150,7 @@ export default class MessagesPuppeteer { case "email": { this.log("Running email login") if (!login_data) { - _sendLoginFailure("No login credentials provided for email login") + this._sendLoginFailure("No login credentials provided for email login") return } @@ -169,7 +169,7 @@ export default class MessagesPuppeteer { } // TODO Phone number login default: - _sendLoginFailure(`Invalid login type: ${login_type}`) + this._sendLoginFailure(`Invalid login type: ${login_type}`) return } @@ -181,20 +181,17 @@ export default class MessagesPuppeteer { this.log("Waiting for login response") let doneWaiting = false let loginSuccess = false - const cancelableResolve = (promiseWithShortTimeout) => { + const cancelableResolve = (promiseFn) => { const executor = (resolve, reject) => { - promiseWithShortTimeout.then( + promiseFn().then( value => { - this.log(`Done: ${value}`) doneWaiting = true resolve(value) }, reason => { if (!doneWaiting) { - this.log(`Not done, waiting some more. ${reason}`) - setTimeout(executor, 3000, resolve, reject) + setTimeout(executor, 1000, resolve, reject) } else { - this.log(`Final fail. ${reason}`) resolve() } } @@ -204,25 +201,27 @@ export default class MessagesPuppeteer { } const result = await Promise.race([ - this.page.waitForSelector("#wrap_message_sync", {timeout: 2000}) + () => this.page.waitForSelector("#wrap_message_sync", {timeout: 2000}) .then(element => { loginSuccess = true return element }), - this.page.waitForSelector("#login_incorrect", {visible: true, timeout: 2000}) + () => this.page.waitForSelector("#login_incorrect", {visible: true, timeout: 2000}) .then(element => element.innerText), - this._waitForLoginCancel(), - ].map(promise => cancelableResolve(promise))) + () => this._waitForLoginCancel(), + ].map(promiseFn => cancelableResolve(promiseFn))) this.log("Removing observers") await this.page.evaluate(() => window.__mautrixController.removeQRChangeObserver()) - await this.page.evaluate(() => window.__mautrixController.removeLoginChildrenObserver(element)) + await this.page.evaluate(() => window.__mautrixController.removeQRAppearObserver()) + await this.page.evaluate(() => window.__mautrixController.removeEmailAppearObserver()) + await this.page.evaluate(() => window.__mautrixController.removePINAppearObserver()) await this.page.evaluate(() => window.__mautrixController.removeExpiryObserver()) delete this.login_email delete this.login_password if (!loginSuccess) { - _sendLoginFailure(result) + this._sendLoginFailure(result) return } @@ -246,13 +245,12 @@ export default class MessagesPuppeteer { async cancelLogin() { if (this.loginRunning) { this.loginCancelled = true - //await this._preparePage(false) + await this._preparePage(false) } } _waitForLoginCancel() { return new Promise((resolve, reject) => { - console.log(`>>>>> ${this.loginCancelled}`) if (this.loginCancelled) { resolve() } else {