Add optional edit handling with replies. Fixes #47

This commit is contained in:
Tulir Asokan
2018-02-13 13:28:48 +02:00
parent 4207e30a17
commit 0147475aec
4 changed files with 40 additions and 17 deletions
+5 -2
View File
@@ -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:
+4 -3
View File
@@ -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"<a href='https://matrix.to/#/{sender}'>{displayname}</a>")
reply_to_msg = (("<a href='https://matrix.to/#/"
+ f"{msg.mx_room}/{msg.mxid}'>Reply</a>")
+ f"{msg.mx_room}/{msg.mxid}'>{reply_text}</a>")
if message_link_in_reply else "Reply")
quote = f"{reply_to_msg} to {reply_to_user}<blockquote>{body}</blockquote>"
except (ValueError, KeyError, MatrixRequestError):
quote = "Reply to unknown user <em>(Failed to fetch message)</em>:<br/>"
quote = "{reply_text} to unknown user <em>(Failed to fetch message)</em>:<br/>"
if html:
html = quote + html
else:
+17 -3
View File
@@ -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)
+14 -9
View File
@@ -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