Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

"Freeze" a room when the last admin of that room leaves #59

Merged
merged 13 commits into from
Oct 13, 2020
Merged
22 changes: 14 additions & 8 deletions synapse/third_party_rules/access_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,21 +472,27 @@ async def _freeze_room_if_last_admin_is_leaving(
if not power_level_state_event:
return
power_level_content = power_level_state_event.content.copy()
if not isinstance(power_level_content, dict):
# The power level content has been set to something other than a dict...
# bail out.

# Do some validation checks on the power level state event
if (
not isinstance(power_level_content, dict)
or "users" not in power_level_content
or not isinstance(power_level_content["users"], dict)
):
# We can't use this power level event to determine whether the room should be
# frozen. Bail out.
return

user_id = event.get("sender")
if not user_id:
return

# Get every admin user defined in the room's state
admin_users = [
admin_users = {
user
for user, power_level in power_level_content["users"].items()
anoadragon453 marked this conversation as resolved.
Show resolved Hide resolved
if power_level >= 100
richvdh marked this conversation as resolved.
Show resolved Hide resolved
]
}

if user_id not in admin_users:
# This user is not an admin, ignore them
Expand All @@ -499,12 +505,14 @@ async def _freeze_room_if_last_admin_is_leaving(
if event_type == EventTypes.Member
and event.membership in [Membership.JOIN, Membership.INVITE]
]
admin_users = [user for user in admin_users if user in joined_members]
admin_users = {user for user in admin_users if user in joined_members}

if len(admin_users) > 1:
# There's another admin user in, or invited to, the room
return

logger.info("Freezing room '%s'", event.room_id)

# Modify the existing power levels to raise all required types to 100
#
# This changes a power level state event's content from something like:
Expand Down Expand Up @@ -561,8 +569,6 @@ async def _freeze_room_if_last_admin_is_leaving(
power_level_content["events"] = {}

# Freeze the room by raising the required power level to send events to 100
anoadragon453 marked this conversation as resolved.
Show resolved Hide resolved
logger.info("Freezing room '%s'", event.room_id)

await self.module_api.create_and_send_event_into_room(
{
"room_id": event.room_id,
Expand Down