feat: include local user details in block/unblock messages

Added functionality to retrieve and include local user details
in block/unblock room messages for clarity. Implemented a new
method to fetch local users in a room by their IDs, enhancing
the feedback provided to moderators.
This commit is contained in:
Kumi 2024-08-07 10:41:21 +02:00
parent 504e2399cc
commit 840ae90376
Signed by: kumi
GPG key ID: ECBCC9082395383F

31
bot.py
View file

@ -70,9 +70,10 @@ class RoombaBot:
self.logger.debug(
f"Room {room_id} {'blocked' if block else 'unblocked'} successfully: {response}"
)
local_users = await self.get_local_users(room_id)
await self.send_message(
self.moderation_room_id,
f"Room {room_id} {'blocked' if block else 'unblocked'} successfully.",
f"Room {room_id} {'blocked' if block else 'unblocked'} successfully. Local users: {', '.join(local_users)}",
)
else:
self.logger.error(
@ -83,6 +84,34 @@ class RoombaBot:
f"Failed to {'block' if block else 'unblock'} room {room_id}.",
)
async def get_local_users(self, room_id):
"""Get the local users in a room.
Args:
room_id (str): The room ID to get the local users from.
Returns:
list: The list of local users in the room.
"""
members_url = f"{self.client.homeserver}/_matrix/client/r0/rooms/{room_id}/members"
headers = {
"Authorization": f"Bearer {self.client.access_token}",
"Content-Type": "application/json",
}
local_users = []
async with aiohttp.ClientSession() as session:
async with session.get(members_url, headers=headers) as resp:
if resp.status == 200:
members = await resp.json()
for member in members.get("chunk", []):
user_id = member.get("user_id")
if user_id and user_id.endswith(self.client.user_id.split(":")[1]):
local_users.append(user_id)
return local_users
async def send_message(self, room_id, message):
"""Send a message to a room.