From e4702d9d3e97c55d09cf9390cbc862adfa74ad23 Mon Sep 17 00:00:00 2001 From: Andrew Ferrazzutti Date: Thu, 14 Oct 2021 23:56:33 -0400 Subject: [PATCH] When kicked out of LINE, post last known LINE dialog message --- matrix_puppeteer_line/rpc/client.py | 6 +++--- matrix_puppeteer_line/user.py | 7 +++++-- puppet/src/client.js | 5 +++-- puppet/src/contentscript.js | 16 ++++++++++++---- puppet/src/puppet.js | 6 +++--- 5 files changed, 26 insertions(+), 14 deletions(-) diff --git a/matrix_puppeteer_line/rpc/client.py b/matrix_puppeteer_line/rpc/client.py index eae2f0c..50cfda1 100644 --- a/matrix_puppeteer_line/rpc/client.py +++ b/matrix_puppeteer_line/rpc/client.py @@ -103,9 +103,9 @@ class Client(RPCClient): self.add_event_handler("receipt", wrapper) - async def on_logged_out(self, func: Callable[[], Awaitable[None]]) -> None: - async def wrapper(data: Dict[str, Any]) -> None: - await func() + async def on_logged_out(self, func: Callable[[str], Awaitable[None]]) -> None: + async def wrapper(data: Dict[str, str]) -> None: + await func(data.get("message")) self.add_event_handler("logged_out", wrapper) diff --git a/matrix_puppeteer_line/user.py b/matrix_puppeteer_line/user.py index 6dc7a55..82a5591 100644 --- a/matrix_puppeteer_line/user.py +++ b/matrix_puppeteer_line/user.py @@ -225,8 +225,11 @@ class User(DBUser, BaseUser): await portal.create_matrix_room(self, chat_info) await portal.handle_remote_receipt(receipt) - async def handle_logged_out(self) -> None: - await self.send_bridge_notice("Logged out of LINE. Please run either \"login-qr\" or \"login-email\" to log back in.") + async def handle_logged_out(self, message: str) -> None: + newline = "\n" + await self.send_bridge_notice( + f"Logged out of LINE{'.' if not message else ' with message: ' + message.replace(newline, ' ') + newline} " + "Please run either \"login-qr\" or \"login-email\" to log back in.") if self._connection_check_task: self._connection_check_task.cancel() self._connection_check_task = None diff --git a/puppet/src/client.js b/puppet/src/client.js index 2a3b280..c0850fa 100644 --- a/puppet/src/client.js +++ b/puppet/src/client.js @@ -152,11 +152,12 @@ export default class Client { }) } - sendLoggedOut() { - this.log("Sending logout notice to client") + sendLoggedOut(message) { + this.log(`Sending logout notice to client${!message ? "" : " with message: " + message}`) return this._write({ id: --this.notificationID, command: "logged_out", + message, }) } diff --git a/puppet/src/contentscript.js b/puppet/src/contentscript.js index 34a31ab..9f480c2 100644 --- a/puppet/src/contentscript.js +++ b/puppet/src/contentscript.js @@ -75,9 +75,10 @@ window.__mautrixReceivePIN = function (pin) {} */ window.__mautrixExpiry = function (button) {} /** + * @param {string} message - The message of the most recent dialog that appeared on screen. * @return {void} */ -window.__mautrixLoggedOut = function() {} +window.__mautrixLoggedOut = function(message) {} /** * typedef ChatTypeEnum @@ -1756,14 +1757,19 @@ window.__mautrixController = new MautrixController() /** * Watch for an error dialog / PIN expiry dialog to appear, and click its "OK" button. * Must watch for both its parent appearing & it being added to its parent in the first place. + * TODO Clean up dialog message promise */ const layer = document.querySelector("#layer_contents") -new MutationObserver(() => { +var resolveDialogMessage +var promiseDialogMessage = new Promise(resolve => {resolveDialogMessage = resolve}) +new MutationObserver(async () => { if (!layer.classList.contains("MdNonDisp")) { const button = layer.querySelector("dialog button") if (button) { + const dialogMessage = layer.querySelector("dialog p")?.innerText console.log("Popup appeared, clicking OK button to continue") button.click() + resolveDialogMessage(dialogMessage) } } }).observe(layer, { @@ -1776,9 +1782,11 @@ new MutationObserver(() => { * Watch for being logged out. */ const mainApp = document.querySelector("#mainApp") -new MutationObserver(() => { +new MutationObserver(async () => { if (mainApp.classList.contains("MdNonDisp")) { - window.__mautrixLoggedOut() + const dialogMessage = await promiseDialogMessage + promiseDialogMessage = new Promise(resolve => {resolveDialogMessage = resolve}) + await window.__mautrixLoggedOut(dialogMessage) } }).observe(mainApp, { attributes: true, diff --git a/puppet/src/puppet.js b/puppet/src/puppet.js index a8c583c..75c5462 100644 --- a/puppet/src/puppet.js +++ b/puppet/src/puppet.js @@ -1307,12 +1307,12 @@ export default class MessagesPuppeteer { } } - _onLoggedOut() { - this.log("Got logged out!") + _onLoggedOut(message) { + this.log(`Got logged out!${!message ? "" : " Message: " + message}`) this.stopObserving() this.page.bringToFront() if (this.client) { - this.client.sendLoggedOut().catch(err => + this.client.sendLoggedOut(message).catch(err => this.error("Failed to send logout notice to client:", err)) } else { this.log("No client connected, not sending logout notice")