From cb5c51cd27774e68e64c5ca8d889a3209144dfde Mon Sep 17 00:00:00 2001 From: Malte E <97891689+maltee1@users.noreply.github.com> Date: Sun, 7 May 2023 17:09:20 +0200 Subject: [PATCH] Add portal to cache when creating chat from Matrix side (#902) --- .../commands/portal/create_chat.py | 10 +------ mautrix_telegram/matrix.py | 14 +--------- mautrix_telegram/portal.py | 28 +++++++++++++++---- 3 files changed, 24 insertions(+), 28 deletions(-) diff --git a/mautrix_telegram/commands/portal/create_chat.py b/mautrix_telegram/commands/portal/create_chat.py index 512bd409..2edf7410 100644 --- a/mautrix_telegram/commands/portal/create_chat.py +++ b/mautrix_telegram/commands/portal/create_chat.py @@ -65,19 +65,11 @@ async def create(evt: CommandEvent) -> EventID: about=about, encrypted=encrypted, ) - invites, errors = await portal.get_telegram_users_in_matrix_room(evt.sender, pre_create=True) - if len(errors) > 0: - error_list = "\n".join(f"* [{mxid}](https://matrix.to/#/{mxid})" for mxid in errors) - await evt.reply( - f"Failed to add the following users to the chat:\n\n{error_list}\n\n" - "You can try `$cmdprefix+sp search -r ` to help the bridge find " - "those users." - ) await warn_missing_power(levels, evt) try: - await portal.create_telegram_chat(evt.sender, invites=invites, supergroup=supergroup) + await portal.create_telegram_chat(evt.sender, supergroup=supergroup) except ValueError as e: await portal.delete() return await evt.reply(e.args[0]) diff --git a/mautrix_telegram/matrix.py b/mautrix_telegram/matrix.py index 3c163d58..9c9eb0b2 100644 --- a/mautrix_telegram/matrix.py +++ b/mautrix_telegram/matrix.py @@ -135,20 +135,8 @@ async def handle_puppet_group_invite( levels.users[self.az.bot_mxid] = 100 if invited_by_level >= 100 else invited_by_level await double_puppet.intent.set_power_levels(room_id, levels) - invites, errors = await portal.get_telegram_users_in_matrix_room( - invited_by, pre_create=True - ) - if len(errors) > 0: - error_list = "\n".join(f"* [{mxid}](https://matrix.to/#/{mxid})" for mxid in errors) - await portal.az.intent.send_notice( - room_id, - f"Failed to add the following users to the chat:\n\n{error_list}\n\n" - "You can try `$cmdprefix+sp search -r ` to help the bridge find " - "those users.", - ) - try: - await portal.create_telegram_chat(invited_by, invites=invites, supergroup=True) + await portal.create_telegram_chat(invited_by, supergroup=True) except ValueError as e: await portal.delete() await portal.az.intent.send_notice(room_id, e.args[0]) diff --git a/mautrix_telegram/portal.py b/mautrix_telegram/portal.py index 088832ff..67a71d2b 100644 --- a/mautrix_telegram/portal.py +++ b/mautrix_telegram/portal.py @@ -209,7 +209,7 @@ UserID, VideoInfo, ) -from mautrix.util import background_task, magic, variation_selector +from mautrix.util import background_task, magic, markdown, variation_selector from mautrix.util.format_duration import format_duration from mautrix.util.message_send_checkpoint import MessageSendCheckpointStatus from mautrix.util.simple_lock import SimpleLock @@ -514,8 +514,9 @@ async def save(self) -> None: async def get_telegram_users_in_matrix_room( self, source: u.User, pre_create: bool = False - ) -> tuple[list[InputUser], list[UserID]]: + ) -> tuple[list[InputUser], list[UserID], list[u.User]]: user_tgids = {} + users = [] intent = self.az.intent if pre_create else self.main_intent user_mxids = await intent.get_room_members(self.mxid, (Membership.JOIN, Membership.INVITE)) for mxid in user_mxids: @@ -523,6 +524,7 @@ async def get_telegram_users_in_matrix_room( continue mx_user = await u.User.get_by_mxid(mxid, create=False) if mx_user and mx_user.tgid: + users.append(mx_user) user_tgids[mx_user.tgid] = mxid puppet_id = p.Puppet.get_id_from_mxid(mxid) if puppet_id: @@ -538,7 +540,7 @@ async def get_telegram_users_in_matrix_room( f"creating a group: {e}" ) errors.append(mxid) - return input_users, errors + return input_users, errors, users async def upgrade_telegram_chat(self, source: u.User) -> None: if self.peer_type != "chat": @@ -589,11 +591,23 @@ async def set_telegram_username(self, source: u.User, username: str) -> None: if await self._update_username(username): await self.save() - async def create_telegram_chat( - self, source: u.User, invites: list[InputUser], supergroup: bool = False - ) -> None: + async def create_telegram_chat(self, source: u.User, supergroup: bool = False) -> None: if not self.mxid: raise ValueError("Can't create Telegram chat for portal without Matrix room.") + invites, errors, users = await self.get_telegram_users_in_matrix_room( + source, pre_create=True + ) + if len(errors) > 0: + error_list = "\n".join(f"* [{mxid}](https://matrix.to/#/{mxid})" for mxid in errors) + command_prefix = self.config["bridge.command_prefix"] + message = ( + f"Failed to add the following users to the chat:\n\n{error_list}\n\n" + f"You can try `{command_prefix} search -r ` to help the bridge find " + "those users." + ) + await self.az.intent.send_notice( + self.mxid, text=message, html=markdown.render(message) + ) elif self.tgid: raise ValueError("Can't create Telegram chat for portal with existing Telegram chat.") @@ -643,6 +657,8 @@ async def create_telegram_chat( await self.main_intent.set_power_levels(self.mxid, levels) await self.handle_matrix_power_levels(source, levels.users, {}, None) await self.update_bridge_info() + for user in users: + await user.register_portal(self) await self.main_intent.send_notice(self.mxid, f"Telegram chat created. ID: {self.tgid}") async def handle_matrix_invite(