Add Matrix->Telegram sticker bridging. Fixes #105
This commit is contained in:
@@ -263,7 +263,9 @@ class MatrixHandler:
|
|||||||
evt["event_id"])
|
evt["event_id"])
|
||||||
elif membership == "join":
|
elif membership == "join":
|
||||||
await self.handle_join(evt["room_id"], evt["state_key"], evt["event_id"])
|
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"])
|
await self.handle_message(evt["room_id"], evt["sender"], content, evt["event_id"])
|
||||||
elif type == "m.room.redaction":
|
elif type == "m.room.redaction":
|
||||||
await self.handle_redaction(evt["room_id"], evt["sender"], evt["redacts"])
|
await self.handle_redaction(evt["room_id"], evt["sender"], evt["redacts"])
|
||||||
|
|||||||
@@ -682,17 +682,24 @@ class Portal:
|
|||||||
response = await client.send_message(self.peer, message, entities=entities, reply_to=reply_to)
|
response = await client.send_message(self.peer, message, entities=entities, reply_to=reply_to)
|
||||||
self._add_telegram_message_to_db(event_id, space, response)
|
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"])
|
file = await self.main_intent.download_file(message["url"])
|
||||||
|
|
||||||
info = message["info"]
|
info = message["info"]
|
||||||
mime = info["mimetype"]
|
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)
|
file_name = self._get_file_meta(message["mxtg_filename"], mime)
|
||||||
|
|
||||||
attributes = [DocumentAttributeFilename(file_name=file_name)]
|
attributes = [DocumentAttributeFilename(file_name=file_name)]
|
||||||
if "w" in info and "h" in info:
|
if w and h:
|
||||||
attributes.append(DocumentAttributeImageSize(w=info["w"], h=info["h"]))
|
attributes.append(DocumentAttributeImageSize(w, h))
|
||||||
|
|
||||||
caption = message["body"] if message["body"] != file_name else None
|
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)
|
await self._handle_matrix_text(sender_id, event_id, space, client, message, reply_to)
|
||||||
elif type == "m.location":
|
elif type == "m.location":
|
||||||
await self._handle_matrix_location(sender_id, event_id, space, client, message, reply_to)
|
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"):
|
elif type in ("m.sticker", "m.image", "m.file", "m.audio", "m.video"):
|
||||||
await self._handle_matrix_file(sender_id, event_id, space, client, message, reply_to)
|
await self._handle_matrix_file(type, sender_id, event_id, space, client, message, reply_to)
|
||||||
else:
|
else:
|
||||||
self.log.debug("Unhandled Matrix event: %s", message)
|
self.log.debug("Unhandled Matrix event: %s", message)
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
from .format_duration import format_duration
|
||||||
|
|||||||
@@ -45,20 +45,20 @@ from ..db import TelegramFile as DBTelegramFile
|
|||||||
log = logging.getLogger("mau.util")
|
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:
|
if not Image:
|
||||||
return "image/webp", file, None, None
|
return source_mime, file, None, None
|
||||||
try:
|
try:
|
||||||
image = Image.open(BytesIO(file)).convert("RGBA")
|
image = Image.open(BytesIO(file)).convert("RGBA")
|
||||||
if thumbnail_to:
|
if thumbnail_to:
|
||||||
image.thumbnail(thumbnail_to, Image.ANTIALIAS)
|
image.thumbnail(thumbnail_to, Image.ANTIALIAS)
|
||||||
new_file = BytesIO()
|
new_file = BytesIO()
|
||||||
image.save(new_file, to)
|
image.save(new_file, target_type)
|
||||||
w, h = image.size
|
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:
|
except Exception:
|
||||||
log.exception(f"Failed to convert webp to {to}")
|
log.exception(f"Failed to convert {source_mime} to {target_type}")
|
||||||
return "image/webp", file, None, None
|
return source_mime, file, None, None
|
||||||
|
|
||||||
|
|
||||||
def _temp_file_name(ext):
|
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
|
image_converted = False
|
||||||
if mime_type == "image/webp":
|
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)
|
256, 256) if is_sticker else None)
|
||||||
image_converted = new_mime_type != mime_type
|
image_converted = new_mime_type != mime_type
|
||||||
mime_type = new_mime_type
|
mime_type = new_mime_type
|
||||||
|
|||||||
Reference in New Issue
Block a user