From a5d9cc5ebce7c2977ce667031788c8b37cc0bf80 Mon Sep 17 00:00:00 2001 From: Andrew Ferrazzutti Date: Sat, 27 Mar 2021 01:12:43 -0400 Subject: [PATCH] Inbound images --- matrix_puppeteer_line/portal.py | 29 +++++++++++++++++++---------- matrix_puppeteer_line/rpc/types.py | 2 +- puppet/src/contentscript.js | 5 +++-- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/matrix_puppeteer_line/portal.py b/matrix_puppeteer_line/portal.py index f568b24..148ff00 100644 --- a/matrix_puppeteer_line/portal.py +++ b/matrix_puppeteer_line/portal.py @@ -23,7 +23,7 @@ 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) + ContentURI, EncryptedFile, ImageInfo) from mautrix.errors import MatrixError from mautrix.util.simple_lock import SimpleLock from mautrix.util.network_retry import call_with_net_retry @@ -194,11 +194,10 @@ class Portal(DBPortal, BasePortal): return event_id = None - if evt.image: + if evt.image_url: content = await self._handle_remote_photo(source, intent, evt) - if content: - event_id = await self._send_message(intent, content, timestamp=evt.timestamp) - if evt.text and not evt.text.isspace(): + event_id = await self._send_message(intent, content, timestamp=evt.timestamp) + elif evt.text and not evt.text.isspace(): content = TextMessageEventContent(msgtype=MessageType.TEXT, body=evt.text) event_id = await self._send_message(intent, content, timestamp=evt.timestamp) if event_id: @@ -209,12 +208,22 @@ class Portal(DBPortal, BasePortal): async def _handle_remote_photo(self, source: 'u.User', intent: IntentAPI, message: Message ) -> Optional[MediaMessageEventContent]: - # TODO - pass + resp = await source.client.read_image(message.image_url) + media_info = await self._reupload_remote_media(resp.data, intent, resp.mime) + return MediaMessageEventContent(url=media_info.mxc, file=media_info.decryption_info, + msgtype=MessageType.IMAGE, body=media_info.file_name, + info=ImageInfo(mimetype=media_info.mime_type, size=media_info.size)) + + async def _reupload_remote_media(self, data: bytes, intent: IntentAPI, + mime_type: str = None, file_name: str = None + ) -> ReuploadedMediaInfo: + if not mime_type: + mime_type = magic.from_buffer(data, mime=True) + upload_mime_type = mime_type + if not file_name: + file_name = f"image{mimetypes.guess_extension(mime_type)}" + upload_file_name = file_name - async def _reupload_remote_media(self, data: bytes, intent: IntentAPI) -> ReuploadedMediaInfo: - upload_mime_type = mime_type = magic.from_buffer(data, mime=True) - upload_file_name = file_name = f"image{mimetypes.guess_extension(mime_type)}" decryption_info = None if self.encrypted and encrypt_attachment: data, decryption_info = encrypt_attachment(data) diff --git a/matrix_puppeteer_line/rpc/types.py b/matrix_puppeteer_line/rpc/types.py index bfaf2ad..109513f 100644 --- a/matrix_puppeteer_line/rpc/types.py +++ b/matrix_puppeteer_line/rpc/types.py @@ -59,7 +59,7 @@ class Message(SerializableAttrs['Message']): sender: Optional[Participant] timestamp: int = None text: Optional[str] = None - image: Optional[str] = None + image_url: Optional[str] = None @dataclass diff --git a/puppet/src/contentscript.js b/puppet/src/contentscript.js index 729c3f8..7d0f7e1 100644 --- a/puppet/src/contentscript.js +++ b/puppet/src/contentscript.js @@ -211,8 +211,9 @@ class MautrixController { // TODO Use "Inner" or not? messageData.text = messageElement.querySelector(".mdRGT07MsgTextInner")?.innerText } else if (messageElement.classList.contains("mdRGT07Image")) { - // TODO Doesn't this need to be a URL? - messageData.image = true + // TODO Probably need a MutationObserver to wait for image to load. + // Should also catch "#_chat_message_image_failure" + messageData.image_url = messageElement.querySelector(".mdRGT07MsgImg > img")?.src } return messageData }