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

Commit a910782

Browse files
Add module API method to create a room (#13429)
Co-authored-by: MattC <[email protected]> Co-authored-by: Brendan Abolivier <[email protected]>
1 parent 845732b commit a910782

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

changelog.d/13429.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add a module API method to create a room.

synapse/module_api/__init__.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,6 +1476,57 @@ async def lookup_room_alias(self, room_alias: str) -> Tuple[str, List[str]]:
14761476

14771477
return room_id.to_string(), hosts
14781478

1479+
async def create_room(
1480+
self,
1481+
user_id: str,
1482+
config: JsonDict,
1483+
ratelimit: bool = True,
1484+
creator_join_profile: Optional[JsonDict] = None,
1485+
) -> Tuple[str, Optional[str]]:
1486+
"""Creates a new room.
1487+
1488+
Added in Synapse v1.65.0.
1489+
1490+
Args:
1491+
user_id:
1492+
The user who requested the room creation.
1493+
config : A dict of configuration options. See "Request body" of:
1494+
https://spec.matrix.org/latest/client-server-api/#post_matrixclientv3createroom
1495+
ratelimit: set to False to disable the rate limiter for this specific operation.
1496+
1497+
creator_join_profile:
1498+
Set to override the displayname and avatar for the creating
1499+
user in this room. If unset, displayname and avatar will be
1500+
derived from the user's profile. If set, should contain the
1501+
values to go in the body of the 'join' event (typically
1502+
`avatar_url` and/or `displayname`.
1503+
1504+
Returns:
1505+
A tuple containing: 1) the room ID (str), 2) if an alias was requested,
1506+
the room alias (str), otherwise None if no alias was requested.
1507+
1508+
Raises:
1509+
ResourceLimitError if server is blocked to some resource being
1510+
exceeded.
1511+
RuntimeError if the user_id does not refer to a local user.
1512+
SynapseError if the user_id is invalid, room ID couldn't be stored, or
1513+
something went horribly wrong.
1514+
"""
1515+
if not self.is_mine(user_id):
1516+
raise RuntimeError(
1517+
"Tried to create a room as a user that isn't local to this homeserver",
1518+
)
1519+
1520+
requester = create_requester(user_id)
1521+
room_id_and_alias, _ = await self._hs.get_room_creation_handler().create_room(
1522+
requester=requester,
1523+
config=config,
1524+
ratelimit=ratelimit,
1525+
creator_join_profile=creator_join_profile,
1526+
)
1527+
1528+
return room_id_and_alias["room_id"], room_id_and_alias.get("room_alias", None)
1529+
14791530

14801531
class PublicRoomListManager:
14811532
"""Contains methods for adding to, removing from and querying whether a room

tests/module_api/test_api.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,57 @@ def test_lookup_room_alias(self) -> None:
654654

655655
self.assertEqual(room_id, reference_room_id)
656656

657+
def test_create_room(self) -> None:
658+
"""Test that modules can create a room."""
659+
# First test user validation (i.e. user is local).
660+
self.get_failure(
661+
self.module_api.create_room(
662+
user_id=f"@user:{self.module_api.server_name}abc",
663+
config={},
664+
ratelimit=False,
665+
),
666+
RuntimeError,
667+
)
668+
669+
# Now do the happy path.
670+
user_id = self.register_user("user", "password")
671+
access_token = self.login(user_id, "password")
672+
673+
room_id, room_alias = self.get_success(
674+
self.module_api.create_room(
675+
user_id=user_id, config={"room_alias_name": "foo-bar"}, ratelimit=False
676+
)
677+
)
678+
679+
# Check room creator.
680+
channel = self.make_request(
681+
"GET",
682+
f"/_matrix/client/v3/rooms/{room_id}/state/m.room.create",
683+
access_token=access_token,
684+
)
685+
self.assertEqual(channel.code, 200, channel.result)
686+
self.assertEqual(channel.json_body["creator"], user_id)
687+
688+
# Check room alias.
689+
self.assertEquals(room_alias, f"#foo-bar:{self.module_api.server_name}")
690+
691+
# Let's try a room with no alias.
692+
room_id, room_alias = self.get_success(
693+
self.module_api.create_room(user_id=user_id, config={}, ratelimit=False)
694+
)
695+
696+
# Check room creator.
697+
channel = self.make_request(
698+
"GET",
699+
f"/_matrix/client/v3/rooms/{room_id}/state/m.room.create",
700+
access_token=access_token,
701+
)
702+
self.assertEqual(channel.code, 200, channel.result)
703+
self.assertEqual(channel.json_body["creator"], user_id)
704+
705+
# Check room alias.
706+
self.assertIsNone(room_alias)
707+
657708

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

0 commit comments

Comments
 (0)