Skip to content

Commit

Permalink
Include partial quote target text in Matrix event
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Oct 29, 2023
1 parent bf2cef4 commit 90a8583
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion mautrix_telegram/formatter/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .from_matrix import matrix_reply_to_telegram, matrix_to_telegram
from .from_telegram import telegram_to_matrix
from .from_telegram import telegram_text_to_matrix_html, telegram_to_matrix
21 changes: 18 additions & 3 deletions mautrix_telegram/formatter/from_telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,21 @@ async def _convert_custom_emoji(
entities[i] = ReuploadedCustomEmoji(entity, custom_emojis[entity.document_id])


async def telegram_text_to_matrix_html(
source: au.AbstractUser,
text: str,
entities: list[TypeMessageEntity],
client: MautrixTelegramClient | None = None,
) -> str:
if not entities:
return escape(text).replace("\n", "<br/>")
await _convert_custom_emoji(source, entities, client=client)
text = add_surrogate(text)
html = await _telegram_entities_to_matrix_catch(text, entities)
html = del_surrogate(html)
return html


async def telegram_to_matrix(
evt: Message | SponsoredMessage,
source: au.AbstractUser,
Expand All @@ -192,10 +207,10 @@ async def telegram_to_matrix(
)
entities = override_entities or evt.entities
if entities:
await _convert_custom_emoji(source, entities, client=client)
content.format = Format.HTML
html = await _telegram_entities_to_matrix_catch(add_surrogate(content.body), entities)
content.formatted_body = del_surrogate(html)
content.formatted_body = await telegram_text_to_matrix_html(
source, content.body, entities, client=client
)

if require_html:
content.ensure_has_html()
Expand Down
12 changes: 12 additions & 0 deletions mautrix_telegram/portal_util/message_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,17 @@ async def _set_reply(
return
elif isinstance(evt.reply_to, MessageReplyStoryHeader):
return

if evt.reply_to.quote and content.msgtype.is_text:
content.ensure_has_html()
quote_html = await formatter.telegram_text_to_matrix_html(
source, evt.reply_to.quote_text, evt.reply_to.quote_entities
)
content.formatted_body = (
f"<blockquote data-telegram-partial-reply>{quote_html}</blockquote>"
f"{content.formatted_body}"
)

space = (
evt.peer_id.channel_id
if isinstance(evt, Message) and isinstance(evt.peer_id, PeerChannel)
Expand All @@ -275,6 +286,7 @@ async def _set_reply(
if isinstance(evt.reply_to.reply_to_peer_id, PeerChannel)
else source.tgid
)

reply_to_id = TelegramID(evt.reply_to.reply_to_msg_id)
msg = await DBMessage.get_one_by_tgid(reply_to_id, space)
no_fallback = no_fallback or self.config["bridge.disable_reply_fallbacks"]
Expand Down

0 comments on commit 90a8583

Please sign in to comment.