Use new ensure_has_html method instead of duplicating code
This commit is contained in:
@@ -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", "<br/>")
|
||||
|
||||
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"<br/><i>- <u>{evt.post_author}</u></i>"
|
||||
|
||||
|
||||
@@ -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", "<br/>")
|
||||
|
||||
@@ -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", "<br/>")
|
||||
content.ensure_has_html()
|
||||
|
||||
tpl = self.get_config("emote_format")
|
||||
puppet = await p.Puppet.get_by_tgid(sender.tgid)
|
||||
|
||||
Reference in New Issue
Block a user