From 21a0d8056976a717eb4fac9bcc290b0aa3167f30 Mon Sep 17 00:00:00 2001 From: Malte E Date: Tue, 21 Mar 2023 14:07:39 +0100 Subject: [PATCH] add portal to cache also when creating a portal from the matrix side --- .../commands/portal/create_chat.py | 10 +------- mautrix_telegram/matrix.py | 14 +---------- mautrix_telegram/portal.py | 23 +++++++++++++++---- 3 files changed, 20 insertions(+), 27 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 1351cbb2..a073c5c8 100644 --- a/mautrix_telegram/portal.py +++ b/mautrix_telegram/portal.py @@ -481,8 +481,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: @@ -490,6 +491,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: @@ -505,7 +507,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": @@ -556,11 +558,20 @@ 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) + await self.az.intent.send_notice( + self.mxid, + 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.", + ) elif self.tgid: raise ValueError("Can't create Telegram chat for portal with existing Telegram chat.") @@ -610,6 +621,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(