Add support for cross-room replies from Telegram

This commit is contained in:
Tulir Asokan
2023-10-29 02:11:11 +03:00
parent 6809ebcde9
commit bf2cef424b
3 changed files with 19 additions and 1 deletions
+1
View File
@@ -157,6 +157,7 @@ class Config(BaseBridgeConfig):
if base["bridge.private_chat_portal_meta"] not in ("default", "always", "never"): if base["bridge.private_chat_portal_meta"] not in ("default", "always", "never"):
base["bridge.private_chat_portal_meta"] = "default" base["bridge.private_chat_portal_meta"] = "default"
copy("bridge.disable_reply_fallbacks") copy("bridge.disable_reply_fallbacks")
copy("bridge.cross_room_replies")
copy("bridge.delivery_receipts") copy("bridge.delivery_receipts")
copy("bridge.delivery_error_reports") copy("bridge.delivery_error_reports")
copy("bridge.incoming_bridge_error_reports") copy("bridge.incoming_bridge_error_reports")
+2
View File
@@ -342,6 +342,8 @@ bridge:
# Disable generating reply fallbacks? Some extremely bad clients still rely on them, # Disable generating reply fallbacks? Some extremely bad clients still rely on them,
# but they're being phased out and will be completely removed in the future. # but they're being phased out and will be completely removed in the future.
disable_reply_fallbacks: false disable_reply_fallbacks: false
# Should cross-chat replies from Telegram be bridged? Most servers and clients don't support this.
cross_room_replies: false
# Whether or not the bridge should send a read receipt from the bridge bot when a message has # Whether or not the bridge should send a read receipt from the bridge bot when a message has
# been sent to Telegram. # been sent to Telegram.
delivery_receipts: false delivery_receipts: false
@@ -267,16 +267,29 @@ class TelegramMessageConverter:
if isinstance(evt, Message) and isinstance(evt.peer_id, PeerChannel) if isinstance(evt, Message) and isinstance(evt.peer_id, PeerChannel)
else source.tgid else source.tgid
) )
if evt.reply_to.reply_to_peer_id and evt.reply_to.reply_to_peer_id != evt.peer_id:
if not self.config["bridge.cross_room_replies"]:
return
space = (
evt.reply_to.reply_to_peer_id.channel_id
if isinstance(evt.reply_to.reply_to_peer_id, PeerChannel)
else source.tgid
)
reply_to_id = TelegramID(evt.reply_to.reply_to_msg_id) reply_to_id = TelegramID(evt.reply_to.reply_to_msg_id)
msg = await DBMessage.get_one_by_tgid(reply_to_id, space) msg = await DBMessage.get_one_by_tgid(reply_to_id, space)
no_fallback = no_fallback or self.config["bridge.disable_reply_fallbacks"] no_fallback = no_fallback or self.config["bridge.disable_reply_fallbacks"]
if not msg or msg.mx_room != self.portal.mxid: if not msg:
# TODO try to find room ID when generating deterministic ID for cross-room reply
if deterministic_id: if deterministic_id:
content.set_reply(self.deterministic_event_id(space, reply_to_id)) content.set_reply(self.deterministic_event_id(space, reply_to_id))
return return
elif msg.mx_room != self.portal.mxid and not self.config["bridge.cross_room_replies"]:
return
elif not isinstance(content, TextMessageEventContent) or no_fallback: elif not isinstance(content, TextMessageEventContent) or no_fallback:
# Not a text message, just set the reply metadata and return # Not a text message, just set the reply metadata and return
content.set_reply(msg.mxid) content.set_reply(msg.mxid)
if msg.mx_room != self.portal.mxid:
content.relates_to.in_reply_to["room_id"] = msg.mx_room
return return
# Text message, try to fetch original message to generate reply fallback. # Text message, try to fetch original message to generate reply fallback.
@@ -291,6 +304,8 @@ class TelegramMessageConverter:
except Exception: except Exception:
self.log.exception("Failed to get event to add reply fallback") self.log.exception("Failed to get event to add reply fallback")
content.set_reply(msg.mxid) content.set_reply(msg.mxid)
if msg.mx_room != self.portal.mxid:
content.relates_to.in_reply_to["room_id"] = msg.mx_room
@staticmethod @staticmethod
def _photo_size_key(photo: TypePhotoSize) -> int: def _photo_size_key(photo: TypePhotoSize) -> int: