From 45981b9c77e6510f92e2ca9fc63c5ea0b38a7494 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sun, 29 Apr 2018 01:49:19 +0300 Subject: [PATCH] Add Matrix->Telegram sticker bridging. Fixes #105 --- mautrix_telegram/matrix.py | 4 +++- mautrix_telegram/portal.py | 17 ++++++++++++----- mautrix_telegram/util/__init__.py | 2 +- mautrix_telegram/util/file_transfer.py | 14 +++++++------- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/mautrix_telegram/matrix.py b/mautrix_telegram/matrix.py index 1a055354..e4c147dd 100644 --- a/mautrix_telegram/matrix.py +++ b/mautrix_telegram/matrix.py @@ -263,7 +263,9 @@ class MatrixHandler: evt["event_id"]) elif membership == "join": await self.handle_join(evt["room_id"], evt["state_key"], evt["event_id"]) - elif type == "m.room.message": + elif type in ("m.room.message", "m.sticker"): + if type != "m.room.message": + content["msgtype"] = type await self.handle_message(evt["room_id"], evt["sender"], content, evt["event_id"]) elif type == "m.room.redaction": await self.handle_redaction(evt["room_id"], evt["sender"], evt["redacts"]) diff --git a/mautrix_telegram/portal.py b/mautrix_telegram/portal.py index ed99df56..d264b22d 100644 --- a/mautrix_telegram/portal.py +++ b/mautrix_telegram/portal.py @@ -682,17 +682,24 @@ class Portal: response = await client.send_message(self.peer, message, entities=entities, reply_to=reply_to) self._add_telegram_message_to_db(event_id, space, response) - async def _handle_matrix_file(self, sender_id, event_id, space, client, message, reply_to): + async def _handle_matrix_file(self, type, sender_id, event_id, space, client, message, reply_to): file = await self.main_intent.download_file(message["url"]) info = message["info"] mime = info["mimetype"] + if type == "m.sticker": + mime, file, w, h = util.convert_image(file, source_mime=mime, target_type="webp") + elif "w" in info and "h" in info: + w, h = info["w"], info["h"] + else: + w, h = None, None + file_name = self._get_file_meta(message["mxtg_filename"], mime) attributes = [DocumentAttributeFilename(file_name=file_name)] - if "w" in info and "h" in info: - attributes.append(DocumentAttributeImageSize(w=info["w"], h=info["h"])) + if w and h: + attributes.append(DocumentAttributeImageSize(w, h)) caption = message["body"] if message["body"] != file_name else None @@ -743,8 +750,8 @@ class Portal: await self._handle_matrix_text(sender_id, event_id, space, client, message, reply_to) elif type == "m.location": await self._handle_matrix_location(sender_id, event_id, space, client, message, reply_to) - elif type in ("m.image", "m.file", "m.audio", "m.video"): - await self._handle_matrix_file(sender_id, event_id, space, client, message, reply_to) + elif type in ("m.sticker", "m.image", "m.file", "m.audio", "m.video"): + await self._handle_matrix_file(type, sender_id, event_id, space, client, message, reply_to) else: self.log.debug("Unhandled Matrix event: %s", message) diff --git a/mautrix_telegram/util/__init__.py b/mautrix_telegram/util/__init__.py index c512529c..7d431396 100644 --- a/mautrix_telegram/util/__init__.py +++ b/mautrix_telegram/util/__init__.py @@ -1,2 +1,2 @@ -from .file_transfer import transfer_file_to_matrix +from .file_transfer import transfer_file_to_matrix, convert_image from .format_duration import format_duration diff --git a/mautrix_telegram/util/file_transfer.py b/mautrix_telegram/util/file_transfer.py index 6ca9eea3..67bf94ff 100644 --- a/mautrix_telegram/util/file_transfer.py +++ b/mautrix_telegram/util/file_transfer.py @@ -45,20 +45,20 @@ from ..db import TelegramFile as DBTelegramFile log = logging.getLogger("mau.util") -def _convert_webp(file, to="png", thumbnail_to=None): +def convert_image(file, source_mime="image/webp", target_type="png", thumbnail_to=None): if not Image: - return "image/webp", file, None, None + return source_mime, file, None, None try: image = Image.open(BytesIO(file)).convert("RGBA") if thumbnail_to: image.thumbnail(thumbnail_to, Image.ANTIALIAS) new_file = BytesIO() - image.save(new_file, to) + image.save(new_file, target_type) w, h = image.size - return f"image/{to}", new_file.getvalue(), w, h + return f"image/{target_type}", new_file.getvalue(), w, h except Exception: - log.exception(f"Failed to convert webp to {to}") - return "image/webp", file, None, None + log.exception(f"Failed to convert {source_mime} to {target_type}") + return source_mime, file, None, None def _temp_file_name(ext): @@ -163,7 +163,7 @@ async def _unlocked_transfer_file_to_matrix(db, client, intent, id, location, th image_converted = False if mime_type == "image/webp": - new_mime_type, file, width, height = _convert_webp(file, to="png", thumbnail_to=( + new_mime_type, file, width, height = convert_image(file, source_mime="image/webp", target_type="png", thumbnail_to=( 256, 256) if is_sticker else None) image_converted = new_mime_type != mime_type mime_type = new_mime_type