Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Add module API method to create a room #13429

Merged
Show file tree
Hide file tree
Changes from 8 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
1 change: 1 addition & 0 deletions changelog.d/13429.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add a module API method to create a room.
51 changes: 51 additions & 0 deletions synapse/module_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1452,6 +1452,57 @@ async def get_monthly_active_users_by_service(
start_timestamp, end_timestamp
)

async def create_room(
self,
user_id: str,
config: JsonDict,
ratelimit: bool = True,
creator_join_profile: Optional[JsonDict] = None,
) -> Tuple[str, Optional[str]]:
"""Creates a new room.

Added in Synapse v1.66.0.

Args:
user_id:
The user who requested the room creation.
config : A dict of configuration options. See "Request body" of:
https://spec.matrix.org/latest/client-server-api/#post_matrixclientv3createroom
ratelimit: set to False to disable the rate limiter.

creator_join_profile:
Set to override the displayname and avatar for the creating
user in this room. If unset, displayname and avatar will be
derived from the user's profile. If set, should contain the
values to go in the body of the 'join' event (typically
`avatar_url` and/or `displayname`.

Returns:
A tuple containing: 1) the room ID (str), 2) if an alias was requested,
the room alias (str), otherwise None if no alias was requested.

Raises:
ResourceLimitError if server is blocked to some resource being
exceeded.
RuntimeError if the user_id does not refer to a local user.
SynapseError if the user_id is invalid, room ID couldn't be stored, or
something went horribly wrong.
"""
if not self.is_mine(user_id):
raise RuntimeError(
"Tried to create a room as a user that isn't local to this homeserver",
)

requester = create_requester(user_id)
room_id_and_alias, _ = await self._hs.get_room_creation_handler().create_room(
requester=requester,
config=config,
ratelimit=ratelimit,
creator_join_profile=creator_join_profile,
)

return room_id_and_alias["room_id"], room_id_and_alias.get("room_alias", None)


class PublicRoomListManager:
"""Contains methods for adding to, removing from and querying whether a room
Expand Down
31 changes: 30 additions & 1 deletion tests/module_api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from synapse.handlers.presence import UserPresenceState
from synapse.handlers.push_rules import InvalidRuleException
from synapse.rest import admin
from synapse.rest.client import login, notifications, presence, profile, room
from synapse.rest.client import directory, login, notifications, presence, profile, room
from synapse.types import create_requester

from tests.events.test_presence_router import send_presence_update, sync_presence
Expand All @@ -40,6 +40,7 @@ class ModuleApiTestCase(HomeserverTestCase):
presence.register_servlets,
profile.register_servlets,
notifications.register_servlets,
directory.register_servlets,
]

def prepare(self, reactor, clock, homeserver):
Expand Down Expand Up @@ -635,6 +636,34 @@ def test_check_push_rules_actions(self) -> None:
[{"set_tweak": "sound", "value": "default"}]
)

def test_create_room(self) -> None:
"""Test that modules can create a room."""
# First test user validation (i.e. user is local).
self.get_failure(
self.module_api.create_room(
user_id=f"@user:{self.module_api.server_name}abc",
config={},
ratelimit=False,
),
RuntimeError,
)

# Now do the happy path.
user_id = self.register_user("user", "password")
access_token = self.login(user_id, "password")

room_id, _ = self.get_success(
self.module_api.create_room(user_id=user_id, config={}, ratelimit=False)
)

channel = self.make_request(
"GET",
f"/_matrix/client/v3/rooms/{room_id}/state/m.room.create",
access_token=access_token,
)
self.assertEqual(channel.code, 200, channel.result)
self.assertEqual(channel.json_body["creator"], user_id)


class ModuleApiWorkerTestCase(BaseMultiWorkerStreamTestCase):
"""For testing ModuleApi functionality in a multi-worker setup"""
Expand Down