From 00e7226efeaa874c0501a244fde5133f9b3bc6df Mon Sep 17 00:00:00 2001 From: Andrew Ferrazzutti Date: Sun, 28 Mar 2021 03:16:07 -0400 Subject: [PATCH] Outbound images, no echo --- matrix_puppeteer_line/portal.py | 9 ++++++--- puppet/src/contentscript.js | 30 ++++++++++++++++++++++-------- puppet/src/puppet.js | 19 ++++++++++++------- 3 files changed, 40 insertions(+), 18 deletions(-) diff --git a/matrix_puppeteer_line/portal.py b/matrix_puppeteer_line/portal.py index aad7eff..c76dae7 100644 --- a/matrix_puppeteer_line/portal.py +++ b/matrix_puppeteer_line/portal.py @@ -25,7 +25,8 @@ from mautrix.appservice import AppService, IntentAPI from mautrix.bridge import BasePortal, NotificationDisabler from mautrix.types import (EventID, MessageEventContent, RoomID, EventType, MessageType, TextMessageEventContent, MediaMessageEventContent, Membership, - ContentURI, EncryptedFile, ImageInfo) + ContentURI, EncryptedFile, ImageInfo, + RelatesTo, RelationType) from mautrix.errors import MatrixError from mautrix.util.simple_lock import SimpleLock from mautrix.util.network_retry import call_with_net_retry @@ -150,6 +151,10 @@ class Portal(DBPortal, BasePortal): await self._send_delivery_receipt(event_id) self.log.debug(f"Handled Matrix message {event_id} -> {message_id}") else: + await self.main_intent.send_notice( + self.mxid, + "Posting this message to LINE may have failed.", + relates_to=RelatesTo(rel_type=RelationType.REPLY, event_id=event_id)) self.log.warning(f"Handled Matrix message {event_id} -> {message_id}") async def handle_matrix_leave(self, user: 'u.User') -> None: @@ -514,8 +519,6 @@ class Portal(DBPortal, BasePortal): self.name = None self.icon_path = None self.icon_mxc = None - self.name_set = False - self.icon_set = False self.encrypted = False await self.update() diff --git a/puppet/src/contentscript.js b/puppet/src/contentscript.js index 7d0f7e1..2f7320c 100644 --- a/puppet/src/contentscript.js +++ b/puppet/src/contentscript.js @@ -225,16 +225,27 @@ class MautrixController { let resolve let reject + const resolveMessage = () => { + observer.disconnect() + observer = null + window.__mautrixReceiveMessageID(msgID) + resolve(msgID) + } + const invisibleTimeCallback = (changes) => { for (const change of changes) { for (const addedNode of change.addedNodes) { if (addedNode.classList.contains("mdRGT07Own")) { - const timeElement = addedNode.querySelector("time.MdNonDisp") + const timeElement = addedNode.querySelector("time") if (timeElement) { msgID = +addedNode.getAttribute("data-local-id") - observer.disconnect() - observer = new MutationObserver(visibleTimeCallback) - observer.observe(timeElement, { attributes: true, attributeFilter: ["class"] }) + if (timeElement.classList.contains(".MdNonDisp")) { + observer.disconnect() + observer = new MutationObserver(visibleTimeCallback) + observer.observe(timeElement, { attributes: true, attributeFilter: ["class"] }) + } else { + resolveMessage() + } return } } @@ -245,9 +256,7 @@ class MautrixController { const visibleTimeCallback = (changes) => { for (const change of changes) { if (!change.target.classList.contains("MdNonDisp")) { - window.__mautrixReceiveMessageID(msgID) - observer.disconnect() - resolve(msgID) + resolveMessage() return } } @@ -262,7 +271,12 @@ class MautrixController { resolve = realResolve reject = realReject // TODO Handle a timeout better than this - setTimeout(() => { observer.disconnect(); reject() }, 10000) + setTimeout(() => { + if (observer) { + observer.disconnect() + reject() + } + }, 5000) }) } diff --git a/puppet/src/puppet.js b/puppet/src/puppet.js index 9c53634..4df4ad4 100644 --- a/puppet/src/puppet.js +++ b/puppet/src/puppet.js @@ -405,13 +405,18 @@ export default class MessagesPuppeteer { const promise = this.page.evaluate( () => window.__mautrixController.promiseOwnMessage()) - // TODO Exit when the attach button is unclickable, - // like in chats with a bot - const [fileChooser] = await Promise.all([ - this.page.waitForFileChooser(), - this.page.click("#_chat_room_plus_btn") - ]) - await fileChooser.accept([filePath]) + try { + this.log(`About to ask for file chooser in ${chatID}`) + const [fileChooser] = await Promise.all([ + this.page.waitForFileChooser(), + this.page.click("#_chat_room_plus_btn") + ]) + this.log(`About to upload ${filePath}`) + await fileChooser.accept([filePath]) + } catch (e) { + this.log(`Failed to upload file to ${chatID}`) + return -1 + } // TODO Commonize with text message sending try {