From f30c03a727b7b3ae767559503c7e82a1943bc238 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Tue, 21 Feb 2023 22:34:21 +0200 Subject: [PATCH] Block creating rooms for deactivated chats (ref #894) --- mautrix_telegram/abstract_user.py | 12 ++++++++---- mautrix_telegram/portal.py | 12 +++++++++++- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/mautrix_telegram/abstract_user.py b/mautrix_telegram/abstract_user.py index a4ea65fb..c1da7f14 100644 --- a/mautrix_telegram/abstract_user.py +++ b/mautrix_telegram/abstract_user.py @@ -633,17 +633,21 @@ class AbstractUser(ABC): await portal.invite_to_matrix(self.mxid) async def _delayed_create_channel(self, chan: Channel) -> None: - self.log.debug("Waiting 5 seconds before handling UpdateChannel for non-existent portal") + self.log.debug( + f"Waiting 5 seconds before handling UpdateChannel for non-existent portal {chan.id}" + ) await asyncio.sleep(5) portal = await po.Portal.get_by_tgid(TelegramID(chan.id)) if portal.mxid: self.log.debug( - "Portal started existing after waiting 5 seconds, dropping UpdateChannel" + "Portal started existing after waiting 5 seconds, " + f"dropping UpdateChannel for {portal.tgid}" ) return else: self.log.info( - "Creating Matrix room with data fetched by Telethon due to UpdateChannel" + f"Creating Matrix room for {portal.tgid}" + " with data fetched by Telethon due to UpdateChannel" ) await portal.create_matrix_room(self, chan, invites=[self.mxid]) @@ -692,7 +696,7 @@ class AbstractUser(ABC): await self.unregister_portal(update.action.chat_id, update.action.chat_id) await self.register_portal(portal) return - self.log.trace( + self.log.debug( "Handling action %s to %s by %d", update.action, portal.tgid_log, diff --git a/mautrix_telegram/portal.py b/mautrix_telegram/portal.py index 70c05250..e14a151c 100644 --- a/mautrix_telegram/portal.py +++ b/mautrix_telegram/portal.py @@ -805,6 +805,12 @@ class Portal(DBPortal, BasePortal): participants_count = 2 if isinstance(entity, Chat): participants_count = entity.participants_count + if entity.deactivated or entity.migrated_to: + self.log.error( + "Throwing error for attempted portal creation " + f"({entity.deactivated=}, {entity.migrated_to=})" + ) + raise RuntimeError("Tried to create portal for deactivated chat") elif isinstance(entity, Channel) and not entity.broadcast: participants_count = entity.participants_count if participants_count is None and self.config["bridge.max_member_count"] > 0: @@ -3288,7 +3294,7 @@ class Portal(DBPortal, BasePortal): self, source: au.AbstractUser, sender: p.Puppet | None, evt: Message ) -> None: if not self.mxid: - self.log.trace("Got telegram message %d, but no room exists, creating...", evt.id) + self.log.debug("Got telegram message %d, but no room exists, creating...", evt.id) await self.create_matrix_room(source, invites=[source.mxid], update_if_exists=False) if not self.mxid: self.log.warning("Room doesn't exist even after creating, dropping %d", evt.id) @@ -3454,6 +3460,10 @@ class Portal(DBPortal, BasePortal): MessageActionChatJoinedByRequest, ) if isinstance(action, create_and_exit) or isinstance(action, create_and_continue): + self.log.debug( + f"Got telegram action of type {type(action).__name__}," + " but no room exists, creating..." + ) await self.create_matrix_room( source, invites=[source.mxid], update_if_exists=isinstance(action, create_and_exit) )