diff --git a/mautrix_telegram/formatter/from_telegram.py b/mautrix_telegram/formatter/from_telegram.py index 641e6693..40ce1403 100644 --- a/mautrix_telegram/formatter/from_telegram.py +++ b/mautrix_telegram/formatter/from_telegram.py @@ -81,9 +81,6 @@ async def telegram_reply_to_matrix(evt: Message, source: au.AbstractUser) -> Rel async def _add_forward_header( source: au.AbstractUser, content: TextMessageEventContent, fwd_from: MessageFwdHeader ) -> None: - if not content.formatted_body or content.format != Format.HTML: - content.format = Format.HTML - content.formatted_body = escape(content.body) fwd_from_html, fwd_from_text = None, None if isinstance(fwd_from.from_id, PeerUser): user = await u.User.get_by_tgid(TelegramID(fwd_from.from_id.user_id)) @@ -139,6 +136,7 @@ async def _add_forward_header( fwd_from_text = "unknown source" fwd_from_html = f"unknown source" + content.ensure_has_html() content.body = "\n".join([f"> {line}" for line in content.body.split("\n")]) content.body = f"Forwarded from {fwd_from_text}:\n{content.body}" content.formatted_body = ( @@ -195,16 +193,11 @@ async def telegram_to_matrix( html = await _telegram_entities_to_matrix_catch(add_surrogate(content.body), entities) content.formatted_body = del_surrogate(html) - def force_html(): - if not content.formatted_body: - content.format = Format.HTML - content.formatted_body = escape(content.body).replace("\n", "
") - if require_html: - force_html() + content.ensure_has_html() if prefix_html: - force_html() + content.ensure_has_html() content.formatted_body = prefix_html + content.formatted_body if prefix_text: content.body = prefix_text + content.body @@ -216,7 +209,7 @@ async def telegram_to_matrix( await _add_reply_header(source, content, evt, main_intent) if isinstance(evt, Message) and evt.post and evt.post_author: - force_html() + content.ensure_has_html() content.body += f"\n- {evt.post_author}" content.formatted_body += f"
- {evt.post_author}" diff --git a/mautrix_telegram/portal.py b/mautrix_telegram/portal.py index 04ebea50..a50b57c2 100644 --- a/mautrix_telegram/portal.py +++ b/mautrix_telegram/portal.py @@ -1503,7 +1503,9 @@ class Portal(DBPortal, BasePortal): return ruds[self.hash_user_id(user_id) % len(ruds)] if ruds else "" async def _apply_msg_format(self, sender: u.User, content: MessageEventContent) -> None: - if not isinstance(content, TextMessageEventContent) or content.format != Format.HTML: + if isinstance(content, TextMessageEventContent): + content.ensure_has_html() + else: content.format = Format.HTML content.formatted_body = escape_html(content.body).replace("\n", "
") @@ -1524,9 +1526,7 @@ class Portal(DBPortal, BasePortal): content.formatted_body = Template(tpl).safe_substitute(tpl_args) async def _apply_emote_format(self, sender: u.User, content: TextMessageEventContent) -> None: - if content.format != Format.HTML: - content.format = Format.HTML - content.formatted_body = escape_html(content.body).replace("\n", "
") + content.ensure_has_html() tpl = self.get_config("emote_format") puppet = await p.Puppet.get_by_tgid(sender.tgid) diff --git a/requirements.txt b/requirements.txt index d28b1511..edf66f7f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,7 +3,7 @@ python-magic>=0.4,<0.5 commonmark>=0.8,<0.10 aiohttp>=3,<4 yarl>=1,<2 -mautrix>=0.15.0,<0.16 +mautrix>=0.15.1,<0.16 #telethon>=1.24,<1.25 # Fork to make session storage async and update to layer 138 tulir-telethon==1.25.0a5