Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions matrix/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

@PenguinBoi12 PenguinBoi12 Jul 2, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A new way to handle matrix calls has been added. Basically, the whole body of the function would become:

await matrix_call( 
    self.client.room_put_state(
        room_id=self.room_id,
        event_type="m.room.name",
        content={"name": name},
    ),
    error_message="Failed to set room name"
)

Same thing for set_topic

"""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.

Expand Down
45 changes: 45 additions & 0 deletions tests/test_room.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
Loading