feat(bot): log user joined rooms on lock/unlock

Enhanced user lock/unlock feature to log the rooms a user is joined to. The list of joined rooms is fetched and included in the success message if the lock/unlock operation succeeds, providing better context for administrative actions.
This commit is contained in:
Kumi 2024-09-01 09:45:41 +02:00
parent 5c1d165774
commit ca855d6d64
Signed by: kumi
GPG key ID: ECBCC9082395383F

View file

@ -248,16 +248,51 @@ class RoombaBot:
payload = {"locked": locked} payload = {"locked": locked}
async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession() as session:
async with session.get(
f"{self.homeserver}/_synapse/admin/v1/users/{user_id}/joined_rooms",
headers=headers,
) as resp:
if resp.status == 200:
joined_rooms = await resp.json()
joined_rooms = joined_rooms.get("joined_rooms", [])
else:
joined_rooms = []
async with session.put(url, headers=headers, json=payload) as resp: async with session.put(url, headers=headers, json=payload) as resp:
if resp.status == 200: if resp.status == 200:
self.logger.debug(f"User {user_id} {'locked' if locked else 'unlocked'} successfully") self.logger.debug(
await self.send_message( f"User {user_id} {'locked' if locked else 'unlocked'} successfully"
self.moderation_room_id, f"User {user_id} {'locked' if locked else 'unlocked'} successfully."
) )
message = f"User {user_id} {'locked' if locked else 'unlocked'} successfully."
if joined_rooms:
message += "\n\nJoined rooms:\n"
for room_id in joined_rooms:
room_url = (
f"{self.homeserver}/_synapse/admin/v1/rooms/{room_id}"
)
async with session.get(
room_url, headers=headers
) as room_resp:
if room_resp.status == 200:
room_name = (await room_resp.json()).get("name")
else:
room_name = None
message += (
f"\n- {room_id}{f' ({room_name})' if room_name else ''}"
)
await self.send_message(self.moderation_room_id, message)
else: else:
self.logger.error(f"Failed to {'lock' if locked else 'unlock'} user {user_id}: {resp.status}") self.logger.error(
f"Failed to {'lock' if locked else 'unlock'} user {user_id}: {resp.status}"
)
await self.send_message( await self.send_message(
self.moderation_room_id, f"Failed to {'lock' if locked else 'unlock'} user {user_id}." self.moderation_room_id,
f"Failed to {'lock' if locked else 'unlock'} user {user_id}.",
) )
async def send_message(self, room_id, message): async def send_message(self, room_id, message):