Skip to content

Commit

Permalink
Split fixing of existing pls in 2 different settings (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
MatMaul authored May 24, 2024
1 parent aba62ed commit ad4eef0
Showing 1 changed file with 35 additions and 29 deletions.
64 changes: 35 additions & 29 deletions room_access_rules/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ class AccessRules:
class RoomAccessRulesConfig:
id_server: str
domains_forbidden_when_restricted: List[str] = []
fix_existing_rooms_power_levels: bool = False
fix_admins_for_dm_power_levels: bool = False
add_live_location_power_levels: bool = False


class RoomAccessRules(object):
Expand Down Expand Up @@ -99,7 +100,10 @@ def __init__(

# This will schedule a resumable long running task to fix power levels of existing rooms.
# Only schedules if we are the main process, and if we can't find an existing task in the queue.
if config.fix_existing_rooms_power_levels and api.worker_name is None:
if (
config.fix_admins_for_dm_power_levels
or config.add_live_location_power_levels
) and api.worker_name is None:

async def schedule_task_if_needed() -> None:
existing_tasks = await self.task_scheduler.get_tasks(
Expand Down Expand Up @@ -186,7 +190,7 @@ def get_room_ids_from(
result={"last_room_id": last_room_id},
)

logger.info(f"Fixing power levels of existing rooms complete !")
logger.info("Fixing power levels of existing rooms complete !")

return TaskStatus.COMPLETE, None, None

Expand Down Expand Up @@ -221,35 +225,37 @@ async def fix_room_power_levels(self, room_id: str) -> None:
# Let's patch the power levels with it
changed = False

# Set location live share needed pl to default events pl
default_events_pl = content.get("events_default", 0)
if content["events"].get(LOCATION_LIVE_SHARE_TYPE, None) is None:
content["events"][LOCATION_LIVE_SHARE_TYPE] = default_events_pl
changed = True
if content["events"].get(LOCATION_LIVE_SHARE_MSC_TYPE, None) is None:
content["events"][LOCATION_LIVE_SHARE_MSC_TYPE] = default_events_pl
changed = True

res = await self.module_api.get_room_state(
room_id, [("im.vector.room.access_rules", "")]
)

is_dm = False

access_rules_event = res.get(("im.vector.room.access_rules", ""))
if access_rules_event:
if access_rules_event.content.get("rule", None) == "direct":
is_dm = True
if self.config.add_live_location_power_levels:
# Set location live share needed pl to default events pl
default_events_pl = content.get("events_default", 0)
if content["events"].get(LOCATION_LIVE_SHARE_TYPE, None) is None:
content["events"][LOCATION_LIVE_SHARE_TYPE] = default_events_pl
changed = True
if content["events"].get(LOCATION_LIVE_SHARE_MSC_TYPE, None) is None:
content["events"][LOCATION_LIVE_SHARE_MSC_TYPE] = default_events_pl
changed = True

if is_dm:
# it's a DM, let's try to fix it by putting everyone admins
res = await self.module_api.get_room_state(
room_id, [("m.room.member", None)]
room_id, [("im.vector.room.access_rules", "")]
)
for _, member in res:
if content["users"].get(member) != 100:
content["users"][member] = 100
changed = True

if self.config.fix_admins_for_dm_power_levels:
is_dm = False

access_rules_event = res.get(("im.vector.room.access_rules", ""))
if access_rules_event:
if access_rules_event.content.get("rule", None) == "direct":
is_dm = True

if is_dm:
# it's a DM, let's try to fix it by putting everyone admins
res = await self.module_api.get_room_state(
room_id, [("m.room.member", None)]
)
for _, member in res:
if content["users"].get(member) != 100:
content["users"][member] = 100
changed = True

# Send the updated pl event to the room with a local admin
if changed:
Expand Down

0 comments on commit ad4eef0

Please sign in to comment.