Skip to content

Commit bf2cef4

Browse files
committed
Add support for cross-room replies from Telegram
1 parent 6809ebc commit bf2cef4

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

mautrix_telegram/config.py

+1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ def do_update(self, helper: ConfigUpdateHelper) -> None:
157157
if base["bridge.private_chat_portal_meta"] not in ("default", "always", "never"):
158158
base["bridge.private_chat_portal_meta"] = "default"
159159
copy("bridge.disable_reply_fallbacks")
160+
copy("bridge.cross_room_replies")
160161
copy("bridge.delivery_receipts")
161162
copy("bridge.delivery_error_reports")
162163
copy("bridge.incoming_bridge_error_reports")

mautrix_telegram/example-config.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,8 @@ bridge:
342342
# Disable generating reply fallbacks? Some extremely bad clients still rely on them,
343343
# but they're being phased out and will be completely removed in the future.
344344
disable_reply_fallbacks: false
345+
# Should cross-chat replies from Telegram be bridged? Most servers and clients don't support this.
346+
cross_room_replies: false
345347
# Whether or not the bridge should send a read receipt from the bridge bot when a message has
346348
# been sent to Telegram.
347349
delivery_receipts: false

mautrix_telegram/portal_util/message_convert.py

+16-1
Original file line numberDiff line numberDiff line change
@@ -267,16 +267,29 @@ async def _set_reply(
267267
if isinstance(evt, Message) and isinstance(evt.peer_id, PeerChannel)
268268
else source.tgid
269269
)
270+
if evt.reply_to.reply_to_peer_id and evt.reply_to.reply_to_peer_id != evt.peer_id:
271+
if not self.config["bridge.cross_room_replies"]:
272+
return
273+
space = (
274+
evt.reply_to.reply_to_peer_id.channel_id
275+
if isinstance(evt.reply_to.reply_to_peer_id, PeerChannel)
276+
else source.tgid
277+
)
270278
reply_to_id = TelegramID(evt.reply_to.reply_to_msg_id)
271279
msg = await DBMessage.get_one_by_tgid(reply_to_id, space)
272280
no_fallback = no_fallback or self.config["bridge.disable_reply_fallbacks"]
273-
if not msg or msg.mx_room != self.portal.mxid:
281+
if not msg:
282+
# TODO try to find room ID when generating deterministic ID for cross-room reply
274283
if deterministic_id:
275284
content.set_reply(self.deterministic_event_id(space, reply_to_id))
276285
return
286+
elif msg.mx_room != self.portal.mxid and not self.config["bridge.cross_room_replies"]:
287+
return
277288
elif not isinstance(content, TextMessageEventContent) or no_fallback:
278289
# Not a text message, just set the reply metadata and return
279290
content.set_reply(msg.mxid)
291+
if msg.mx_room != self.portal.mxid:
292+
content.relates_to.in_reply_to["room_id"] = msg.mx_room
280293
return
281294

282295
# Text message, try to fetch original message to generate reply fallback.
@@ -291,6 +304,8 @@ async def _set_reply(
291304
except Exception:
292305
self.log.exception("Failed to get event to add reply fallback")
293306
content.set_reply(msg.mxid)
307+
if msg.mx_room != self.portal.mxid:
308+
content.relates_to.in_reply_to["room_id"] = msg.mx_room
294309

295310
@staticmethod
296311
def _photo_size_key(photo: TypePhotoSize) -> int:

0 commit comments

Comments
 (0)