Remove alias when cleaning up room
This commit is contained in:
@@ -136,6 +136,18 @@ class BasePortal(ABC):
|
|||||||
return str(self.tgid)
|
return str(self.tgid)
|
||||||
return f"{self.tg_receiver}<->{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
|
@property
|
||||||
def peer(self) -> Union[TypePeer, TypeInputPeer]:
|
def peer(self) -> Union[TypePeer, TypeInputPeer]:
|
||||||
if self.peer_type == "user":
|
if self.peer_type == "user":
|
||||||
@@ -261,13 +273,17 @@ class BasePortal(ABC):
|
|||||||
authenticated.append(user)
|
authenticated.append(user)
|
||||||
return authenticated
|
return authenticated
|
||||||
|
|
||||||
@staticmethod
|
async def cleanup_room(self, intent: IntentAPI, room_id: RoomID,
|
||||||
async def cleanup_room(intent: IntentAPI, room_id: RoomID, message: str = "Portal deleted",
|
message: str = "Portal deleted", puppets_only: bool = False) -> None:
|
||||||
puppets_only: bool = False) -> None:
|
|
||||||
try:
|
try:
|
||||||
members = await intent.get_room_members(room_id)
|
members = await intent.get_room_members(room_id)
|
||||||
except MatrixRequestError:
|
except MatrixRequestError:
|
||||||
members = []
|
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:
|
for user in members:
|
||||||
puppet = p.Puppet.get_by_mxid(UserID(user), create=False)
|
puppet = p.Puppet.get_by_mxid(UserID(user), create=False)
|
||||||
if user != intent.mxid and (not puppets_only or puppet):
|
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)
|
await intent.kick_user(room_id, user, message)
|
||||||
except (MatrixRequestError, IntentError):
|
except (MatrixRequestError, IntentError):
|
||||||
pass
|
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:
|
async def unbridge(self) -> None:
|
||||||
await self.cleanup_room(self.main_intent, self.mxid, "Room unbridged", puppets_only=True)
|
await self.cleanup_room(self.main_intent, self.mxid, "Room unbridged", puppets_only=True)
|
||||||
|
|||||||
@@ -278,8 +278,8 @@ class PortalMetadata(BasePortal, ABC):
|
|||||||
|
|
||||||
if self.peer_type == "channel" and entity.username:
|
if self.peer_type == "channel" and entity.username:
|
||||||
preset = RoomCreatePreset.PUBLIC
|
preset = RoomCreatePreset.PUBLIC
|
||||||
alias = self._get_alias_localpart(entity.username)
|
|
||||||
self.username = entity.username
|
self.username = entity.username
|
||||||
|
alias = self.alias_localpart
|
||||||
else:
|
else:
|
||||||
preset = RoomCreatePreset.PRIVATE
|
preset = RoomCreatePreset.PRIVATE
|
||||||
# TODO invite link alias?
|
# TODO invite link alias?
|
||||||
@@ -438,18 +438,6 @@ class PortalMetadata(BasePortal, ABC):
|
|||||||
if self._participants_to_power_levels(participants, levels):
|
if self._participants_to_power_levels(participants, levels):
|
||||||
await self.main_intent.set_power_levels(self.mxid, 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:
|
def _add_bot_chat(self, bot: User) -> None:
|
||||||
if self.bot and bot.id == self.bot.tgid:
|
if self.bot and bot.id == self.bot.tgid:
|
||||||
self.bot.add_chat(self.tgid, self.peer_type)
|
self.bot.add_chat(self.tgid, self.peer_type)
|
||||||
@@ -584,11 +572,10 @@ class PortalMetadata(BasePortal, ABC):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
if self.username:
|
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
|
self.username = username or None
|
||||||
if self.username:
|
if self.username:
|
||||||
await self.main_intent.add_room_alias(self.mxid, self._get_alias_localpart(),
|
await self.main_intent.add_room_alias(self.mxid, self.alias_localpart, override=True)
|
||||||
override=True)
|
|
||||||
if self.public_portals:
|
if self.public_portals:
|
||||||
await self.main_intent.set_join_rule(self.mxid, "public")
|
await self.main_intent.set_join_rule(self.mxid, "public")
|
||||||
else:
|
else:
|
||||||
|
|||||||
Reference in New Issue
Block a user