diff --git a/src/matrix_bot/client.py b/src/matrix_bot/client.py index c4a5b0b..693802b 100644 --- a/src/matrix_bot/client.py +++ b/src/matrix_bot/client.py @@ -60,15 +60,13 @@ class Client(Aobject): if isinstance(resp, nio.responses.LoginError): raise RuntimeError(f"Fail to connect: {resp.message}") - # TODO: Where is the async map when you need it? self.allowed_rooms = None if allowed_rooms_names: self.allowed_rooms = {} - for room_name in allowed_rooms_names: - room = await self.resolve_room(room_name) + rooms = await asyncio.gather(*(self.resolve_room(room_name) for room_name in allowed_rooms_names)) + for room in rooms: self.allowed_rooms[room.id] = room # room uniqueness is handled by self.resolve_room - async def resolve_room( self, room_name: Union[RoomAlias, RoomId] @@ -86,7 +84,9 @@ class Client(Aobject): return self.__rooms_by_id[room_name] # If it is a unknown room id: elif room_name[0] == '!': - return Room(id=room_name) + room = Room(id=room_name) + self.__rooms_by_id[room_name] = room + return room # If it is not a room id nor a room alias: elif room_name[0] != '#': raise ValueError(f"Invalid room_name: {room_name}") @@ -122,7 +122,6 @@ class Client(Aobject): if isinstance(sync_resp, nio.responses.SyncError): print(f"Error while syncronizing: {sync_resp.message}") # TODO: use proper logging continue - print(sync_resp) async def run( self, diff --git a/tests/test.py b/tests/test.py index 1aea0f0..765db95 100644 --- a/tests/test.py +++ b/tests/test.py @@ -16,7 +16,7 @@ async def main(): os.environ["PASSWD"], os.environ["ROOMS"].split(",") ) -# print(client.allowed_rooms) + print(client.allowed_rooms) await client.run()