Remove alias when cleaning up room

This commit is contained in:
Tulir Asokan
2019-11-30 16:01:07 +02:00
parent c52df5dc36
commit 1986142db3
2 changed files with 26 additions and 20 deletions
+23 -4
View File
@@ -136,6 +136,18 @@ class BasePortal(ABC):
return str(self.tgid)
return f"{self.tg_receiver}<->{self.tgid}"
@property
def alias(self) -> Optional[RoomAlias]:
if not self.username:
return None
return RoomAlias(f"#{self.alias_localpart}:{self.hs_domain}")
@property
def alias_localpart(self) -> Optional[str]:
if not self.username:
return None
return self.alias_template.format(self.username)
@property
def peer(self) -> Union[TypePeer, TypeInputPeer]:
if self.peer_type == "user":
@@ -261,13 +273,17 @@ class BasePortal(ABC):
authenticated.append(user)
return authenticated
@staticmethod
async def cleanup_room(intent: IntentAPI, room_id: RoomID, message: str = "Portal deleted",
puppets_only: bool = False) -> None:
async def cleanup_room(self, intent: IntentAPI, room_id: RoomID,
message: str = "Portal deleted", puppets_only: bool = False) -> None:
try:
members = await intent.get_room_members(room_id)
except MatrixRequestError:
members = []
if self.username:
try:
await intent.remove_room_alias(self.alias_localpart)
except (MatrixRequestError, IntentError):
self.log.warning("Failed to remove alias when cleaning up room", exc_info=True)
for user in members:
puppet = p.Puppet.get_by_mxid(UserID(user), create=False)
if user != intent.mxid and (not puppets_only or puppet):
@@ -278,7 +294,10 @@ class BasePortal(ABC):
await intent.kick_user(room_id, user, message)
except (MatrixRequestError, IntentError):
pass
await intent.leave_room(room_id)
try:
await intent.leave_room(room_id)
except (MatrixRequestError, IntentError):
self.log.warning("Failed to leave room when cleaning up room", exc_info=True)
async def unbridge(self) -> None:
await self.cleanup_room(self.main_intent, self.mxid, "Room unbridged", puppets_only=True)
+3 -16
View File
@@ -278,8 +278,8 @@ class PortalMetadata(BasePortal, ABC):
if self.peer_type == "channel" and entity.username:
preset = RoomCreatePreset.PUBLIC
alias = self._get_alias_localpart(entity.username)
self.username = entity.username
alias = self.alias_localpart
else:
preset = RoomCreatePreset.PRIVATE
# TODO invite link alias?
@@ -438,18 +438,6 @@ class PortalMetadata(BasePortal, ABC):
if self._participants_to_power_levels(participants, levels):
await self.main_intent.set_power_levels(self.mxid, levels)
@property
def alias(self) -> Optional[RoomAlias]:
if not self.username:
return None
return RoomAlias(f"#{self._get_alias_localpart()}:{self.hs_domain}")
def _get_alias_localpart(self, username: Optional[str] = None) -> Optional[str]:
username = username or self.username
if not username:
return None
return self.alias_template.format(username)
def _add_bot_chat(self, bot: User) -> None:
if self.bot and bot.id == self.bot.tgid:
self.bot.add_chat(self.tgid, self.peer_type)
@@ -584,11 +572,10 @@ class PortalMetadata(BasePortal, ABC):
return False
if self.username:
await self.main_intent.remove_room_alias(self._get_alias_localpart())
await self.main_intent.remove_room_alias(self.alias_localpart)
self.username = username or None
if self.username:
await self.main_intent.add_room_alias(self.mxid, self._get_alias_localpart(),
override=True)
await self.main_intent.add_room_alias(self.mxid, self.alias_localpart, override=True)
if self.public_portals:
await self.main_intent.set_join_rule(self.mxid, "public")
else: