From ff6d3161592022c10db7f538c73fbd8d4a2b494a Mon Sep 17 00:00:00 2001 From: Ishita Tyagi Date: Wed, 1 Jul 2026 12:33:45 +0530 Subject: [PATCH] feat: add Room.set_name() and Room.set_topic() methods Closes #69 --- matrix/room.py | 36 ++++++++++++++++++++++++++++++++++++ tests/test_room.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) diff --git a/matrix/room.py b/matrix/room.py index 2cde9f2..4d75e73 100644 --- a/matrix/room.py +++ b/matrix/room.py @@ -345,6 +345,42 @@ async def _send_payload(self, payload: BaseMessageContent) -> Message: except Exception as e: raise MatrixError(f"Failed to send message: {e}") + async def set_name(self, name: str) -> None: + """Set the room's display name. + + ## Example + + ```python + await room.set_name("General") + ``` + """ + try: + await self.client.room_put_state( + room_id=self.room_id, + event_type="m.room.name", + content={"name": name}, + ) + except Exception as e: + raise MatrixError(f"Failed to set room name: {e}") + + async def set_topic(self, topic: str) -> None: + """Set the room's topic. + + ## Example + + ```python + await room.set_topic("Last updated: 13:00 UTC") + ``` + """ + try: + await self.client.room_put_state( + room_id=self.room_id, + event_type="m.room.topic", + content={"topic": topic}, + ) + except Exception as e: + raise MatrixError(f"Failed to set room topic: {e}") + async def fetch_event(self, event_id: str) -> Event: """Fetch a Matrix event by its ID. diff --git a/tests/test_room.py b/tests/test_room.py index 8d5e39a..02b7e82 100644 --- a/tests/test_room.py +++ b/tests/test_room.py @@ -260,6 +260,51 @@ async def test_fetch_message_with_error__expect_matrix_error(room, client): await room.fetch_message("$event123") +# SET NAME / SET TOPIC + + +@pytest.mark.asyncio +async def test_set_name__expect_room_state_updated(room, client): + client.room_put_state = AsyncMock() + + await room.set_name("General") + + client.room_put_state.assert_awaited_once_with( + room_id="!room:example.com", + event_type="m.room.name", + content={"name": "General"}, + ) + + +@pytest.mark.asyncio +async def test_set_name_with_error__expect_matrix_error(room, client): + client.room_put_state = AsyncMock(side_effect=Exception("Insufficient permissions")) + + with pytest.raises(MatrixError, match="Failed to set room name"): + await room.set_name("General") + + +@pytest.mark.asyncio +async def test_set_topic__expect_room_state_updated(room, client): + client.room_put_state = AsyncMock() + + await room.set_topic("Last updated: 13:00 UTC") + + client.room_put_state.assert_awaited_once_with( + room_id="!room:example.com", + event_type="m.room.topic", + content={"topic": "Last updated: 13:00 UTC"}, + ) + + +@pytest.mark.asyncio +async def test_set_topic_with_error__expect_matrix_error(room, client): + client.room_put_state = AsyncMock(side_effect=Exception("Insufficient permissions")) + + with pytest.raises(MatrixError, match="Failed to set room topic"): + await room.set_topic("Last updated: 13:00 UTC") + + # INVITE