From 0147475aecd7c354358cdfc5cdaab566131a5214 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Tue, 13 Feb 2018 13:28:48 +0200 Subject: [PATCH] Add optional edit handling with replies. Fixes #47 --- example-config.yaml | 7 +++++-- mautrix_telegram/formatter.py | 7 ++++--- mautrix_telegram/portal.py | 20 +++++++++++++++++--- mautrix_telegram/user.py | 23 ++++++++++++++--------- 4 files changed, 40 insertions(+), 17 deletions(-) diff --git a/example-config.yaml b/example-config.yaml index 6a117c89..fce1ac91 100644 --- a/example-config.yaml +++ b/example-config.yaml @@ -67,6 +67,9 @@ bridge: # Warning: Using this on a client with native replies will not look good: the message will have # a native quote AND a non-native quote. link_in_reply: False + # Show message editing as a reply to the original message. + # If this is false, message edits are not shown at all, as Matrix does not support editing yet. + edits_as_replies: False # The prefix for commands. Only required in non-management rooms. command_prefix: "!tg" @@ -74,13 +77,13 @@ bridge: # Whitelist of user IDs that are allowed to use this bridge. Leave empty to disable. # You can enter a domain without the localpart to allow all users from that homeserver to use the bridge. whitelist: - - "internal-hs.example.com" + - "internal.example.com" - "@user:public.example.com" # Admins can do things like delete portal rooms. Here you must specify the exact MXID, domains # are not accepted. admins: - - "@admin:internal-hs.example.com" + - "@admin:internal.example.com" # Telegram config telegram: diff --git a/mautrix_telegram/formatter.py b/mautrix_telegram/formatter.py index ce4a4c7b..91e59bf0 100644 --- a/mautrix_telegram/formatter.py +++ b/mautrix_telegram/formatter.py @@ -43,6 +43,7 @@ log = logging.getLogger("mau.formatter") # everything up. TEMP_ENC = "utf-16-le" + # region Matrix to Telegram class MessageEntityReply(MessageEntityUnknown): @@ -213,7 +214,7 @@ def matrix_to_telegram(html, tg_space=None): # region Telegram to Matrix async def telegram_event_to_matrix(evt, source, native_replies=False, message_link_in_reply=False, - main_intent=None): + main_intent=None, reply_text="Reply"): text = evt.message html = telegram_to_matrix(evt.message, evt.entities) if evt.entities else None @@ -258,11 +259,11 @@ async def telegram_event_to_matrix(evt, source, native_replies=False, message_li displayname = puppet.displayname if puppet else sender reply_to_user = (f"{displayname}") reply_to_msg = (("Reply") + + f"{msg.mx_room}/{msg.mxid}'>{reply_text}") if message_link_in_reply else "Reply") quote = f"{reply_to_msg} to {reply_to_user}
{body}
" except (ValueError, KeyError, MatrixRequestError): - quote = "Reply to unknown user (Failed to fetch message):
" + quote = "{reply_text} to unknown user (Failed to fetch message):
" if html: html = quote + html else: diff --git a/mautrix_telegram/portal.py b/mautrix_telegram/portal.py index 2138d848..b034b688 100644 --- a/mautrix_telegram/portal.py +++ b/mautrix_telegram/portal.py @@ -733,9 +733,23 @@ class Portal: async def handle_telegram_text(self, source, sender, evt): self.log.debug(f"Sending {evt.message} to {self.mxid} by {sender.id}") text, html = await formatter.telegram_event_to_matrix(evt, source, - config["bridge.native_replies"], - config["bridge.link_in_reply"], - self.main_intent) + config["bridge.native_replies"], + config["bridge.link_in_reply"], + self.main_intent) + await sender.intent.set_typing(self.mxid, is_typing=False) + return await sender.intent.send_text(self.mxid, text, html=html) + + async def handle_telegram_edit(self, source, sender, evt): + if not self.mxid: + return + elif not config["bridge.edits_as_replies"]: + self.log.debug("Edits as replies disabled, ignoring edit event...") + return + evt.reply_to_msg_id = evt.id + text, html = await formatter.telegram_event_to_matrix(evt, source, + config["bridge.native_replies"], + config["bridge.link_in_reply"], + self.main_intent, reply_text="Edit") await sender.intent.set_typing(self.mxid, is_typing=False) return await sender.intent.send_text(self.mxid, text, html=html) diff --git a/mautrix_telegram/user.py b/mautrix_telegram/user.py index d64be891..1c19dd96 100644 --- a/mautrix_telegram/user.py +++ b/mautrix_telegram/user.py @@ -281,8 +281,8 @@ class User: self.log.exception("Failed to handle Telegram update") async def update(self, update): - if isinstance(update, (UpdateShortChatMessage, UpdateShortMessage, UpdateNewMessage, - UpdateNewChannelMessage)): + if isinstance(update, (UpdateShortChatMessage, UpdateShortMessage, UpdateNewChannelMessage, + UpdateNewMessage, UpdateEditMessage, UpdateEditChannelMessage)): await self.update_message(update) elif isinstance(update, (UpdateChatUserTyping, UpdateUserTyping)): await self.update_typing(update) @@ -365,7 +365,8 @@ class User: elif isinstance(update, UpdateShortMessage): portal = po.Portal.get_by_tgid(update.user_id, self.tgid, "user") sender = pu.Puppet.get(self.tgid if update.out else update.user_id) - elif isinstance(update, (UpdateNewMessage, UpdateNewChannelMessage)): + elif isinstance(update, (UpdateNewMessage, UpdateNewChannelMessage, + UpdateEditMessage, UpdateEditChannelMessage)): update = update.message if isinstance(update.to_id, PeerUser) and not update.out: portal = po.Portal.get_by_tgid(update.from_id, peer_type="user", @@ -379,8 +380,8 @@ class User: return update, None, None return update, sender, portal - async def update_message(self, update): - update, sender, portal = self.get_message_details(update) + def update_message(self, original_update): + update, sender, portal = self.get_message_details(original_update) if isinstance(update, MessageService): if isinstance(update.action, MessageActionChannelMigrateFrom): @@ -389,10 +390,14 @@ class User: return self.log.debug("Handling action %s to %s by %d", update.action, portal.tgid_log, sender.id) - await portal.handle_telegram_action(self, sender, update.action) - else: - self.log.debug("Handling message %s to %s by %d", update, portal.tgid_log, sender.tgid) - await portal.handle_telegram_message(self, sender, update) + return portal.handle_telegram_action(self, sender, update.action) + + if isinstance(original_update, (UpdateEditMessage, UpdateEditChannelMessage)): + self.log.debug("Handling edit %s to %s by %d", update, portal.tgid_log, sender.tgid) + return portal.handle_telegram_edit(self, sender, update) + + self.log.debug("Handling message %s to %s by %d", update, portal.tgid_log, sender.tgid) + return portal.handle_telegram_message(self, sender, update) # endregion # region Class instance lookup