diff --git a/mautrix_telegram/formatter/from_matrix.py b/mautrix_telegram/formatter/from_matrix.py index c74ac1ae..72d2edfe 100644 --- a/mautrix_telegram/formatter/from_matrix.py +++ b/mautrix_telegram/formatter/from_matrix.py @@ -53,17 +53,15 @@ class MatrixParser(HTMLParser): mention = self.mention_regex.match(url) if mention: mxid = mention.group(1) - user = (pu.Puppet.get_by_mxid(mxid, create=False) + user = (pu.Puppet.get_by_mxid(mxid) or u.User.get_by_mxid(mxid, create=False)) if not user: return None, None if user.username: - entity_type = MessageEntityMention - url = f"@{user.username}" + return MessageEntityMention, f"@{user.username}" else: - entity_type = MessageEntityMentionName - args["user_id"] = user.tgid - return entity_type, url + args["user_id"] = InputUser(user.tgid, 0) + return InputMessageEntityMentionName, user.displayname or None room = self.room_regex.match(url) if room: diff --git a/mautrix_telegram/portal.py b/mautrix_telegram/portal.py index 48176407..bbd13273 100644 --- a/mautrix_telegram/portal.py +++ b/mautrix_telegram/portal.py @@ -580,13 +580,22 @@ class Portal: message["body"] = f"<{sender.displayname}> {message['body']}" return type - def _handle_matrix_text(self, client, message, reply_to): - if "format" in message and message["format"] == "org.matrix.custom.html": + async def _handle_matrix_text(self, client, message, reply_to): + is_formatted = ("format" in message + and message["format"] == "org.matrix.custom.html" + and "formatted_body" in message) + if is_formatted: message, entities = formatter.matrix_to_telegram(message["formatted_body"]) - return client.send_message(self.peer, message, entities=entities, reply_to=reply_to) + + # TODO remove this crap + for entity in entities: + if isinstance(entity, InputMessageEntityMentionName): + entity.user_id = await client.get_input_entity(entity.user_id.user_id) + + return await client.send_message(self.peer, message, entities=entities, reply_to=reply_to) else: message = formatter.matrix_text_to_telegram(message["body"]) - return client.send_message(self.peer, message, reply_to=reply_to) + return await client.send_message(self.peer, message, reply_to=reply_to) async def _handle_matrix_file(self, client, message, reply_to): file = await self.main_intent.download_file(message["url"])