From b22196ca485b5756269011bdcfd8c28864ab93fa Mon Sep 17 00:00:00 2001 From: Panoramic Date: Thu, 25 Jul 2024 17:58:44 +0300 Subject: [PATCH] Allow inviting more than 10 people to a room --- vetting_bot/bot_commands.py | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/vetting_bot/bot_commands.py b/vetting_bot/bot_commands.py index 3ef4b2b..a798ee5 100644 --- a/vetting_bot/bot_commands.py +++ b/vetting_bot/bot_commands.py @@ -9,6 +9,7 @@ from nio import ( MegolmEvent, RoomCreateError, RoomGetEventError, + RoomInviteError, RoomMessagesError, RoomMessageText, RoomPutStateResponse, @@ -199,9 +200,13 @@ class Command: "content": {"history_visibility": "shared"}, }, ] + + # Max amount of users to invite is 10 here: + invitee_chunks = divide_chunks(invitees, 10) + room_resp = await self.client.room_create( name=f"Vetting {random_string}", - invite=invitees, + invite=next(invitee_chunks), initial_state=initial_state, ) @@ -211,6 +216,18 @@ class Command: logging.error(text, stack_info=True) return + # Invite more people + for chunk in invitee_chunks: + for mxid in chunk: + invite_resp = await self.client.room_invite(room_resp.room_id, mxid) + + if isinstance(invite_resp, RoomInviteError): + text = ( + f"Unable to invite {mxid} to newly created room: {invite_resp}" + ) + await send_text_to_room(self.client, self.room.room_id, text) + logging.error(text, stack_info=True) + # Create new vetting entry self.store.cursor.execute( "INSERT INTO vetting (mxid, room_id, vetting_create_time) VALUES (?, ?, ?)", @@ -483,3 +500,10 @@ def validate_user_id(user_id): ) is not None ) + + +# Yield successive n-sized chunks from l. +def divide_chunks(l, n: int): + # looping till length l + for i in range(0, len(l), n): + yield l[i : i + n]