mirror of
https://git.anonymousland.org/anonymousland/synapse-product.git
synced 2024-09-07 17:12:15 +00:00
Add module API method to create a room (#13429)
Co-authored-by: MattC <buffless-matt@users.noreply.github.com> Co-authored-by: Brendan Abolivier <babolivier@matrix.org>
This commit is contained in:
parent
845732be45
commit
a91078200d
1
changelog.d/13429.feature
Normal file
1
changelog.d/13429.feature
Normal file
@ -0,0 +1 @@
|
|||||||
|
Add a module API method to create a room.
|
@ -1476,6 +1476,57 @@ class ModuleApi:
|
|||||||
|
|
||||||
return room_id.to_string(), hosts
|
return room_id.to_string(), hosts
|
||||||
|
|
||||||
|
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.65.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 for this specific operation.
|
||||||
|
|
||||||
|
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:
|
class PublicRoomListManager:
|
||||||
"""Contains methods for adding to, removing from and querying whether a room
|
"""Contains methods for adding to, removing from and querying whether a room
|
||||||
|
@ -654,6 +654,57 @@ class ModuleApiTestCase(HomeserverTestCase):
|
|||||||
|
|
||||||
self.assertEqual(room_id, reference_room_id)
|
self.assertEqual(room_id, reference_room_id)
|
||||||
|
|
||||||
|
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, room_alias = self.get_success(
|
||||||
|
self.module_api.create_room(
|
||||||
|
user_id=user_id, config={"room_alias_name": "foo-bar"}, ratelimit=False
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
# Check room creator.
|
||||||
|
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)
|
||||||
|
|
||||||
|
# Check room alias.
|
||||||
|
self.assertEquals(room_alias, f"#foo-bar:{self.module_api.server_name}")
|
||||||
|
|
||||||
|
# Let's try a room with no alias.
|
||||||
|
room_id, room_alias = self.get_success(
|
||||||
|
self.module_api.create_room(user_id=user_id, config={}, ratelimit=False)
|
||||||
|
)
|
||||||
|
|
||||||
|
# Check room creator.
|
||||||
|
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)
|
||||||
|
|
||||||
|
# Check room alias.
|
||||||
|
self.assertIsNone(room_alias)
|
||||||
|
|
||||||
|
|
||||||
class ModuleApiWorkerTestCase(BaseMultiWorkerStreamTestCase):
|
class ModuleApiWorkerTestCase(BaseMultiWorkerStreamTestCase):
|
||||||
"""For testing ModuleApi functionality in a multi-worker setup"""
|
"""For testing ModuleApi functionality in a multi-worker setup"""
|
||||||
|
Loading…
Reference in New Issue
Block a user