Repository: matrix-org/synapse

Issue:
Synapse selects `"preset": "public_chat"` by default, while it should be "private_chat"
### Description

I noticed this while looking at the spec;

While creating rooms, [`/createRooms`](https://spec.matrix.org/v1.1/client-server-api/#post_matrixclientv3createroom) recommends - for a default `"preset"` - to use `"private_chat"`, recommending it to look at `"visibility"`, which defaults to `"private"`.

However, synapse creates `public_chat` rooms when given no options.

I know that the spec is only saying "should" here, but since Dendrite and Conduit also display this behaviour, it could cause confusion that a "known good" server such as Synapse is going against spec here, creating bugs in software where previously there were none.

### Version information

- **Version**: v1.48.0

- **Install method**: Docker

- **Platform**: Linux



Discussion:
Even our test helpers say it should be private by default (of course the tests in practice rely on them being public):
```python
            is_public: If True, the `visibility` parameter will be set to
                "public". If False, it will be set to "private". If left
                unspecified, the server will set it to an appropriate default
                (which should be "private" as per the CS spec).
```

Resolving patch:
diff --git a/synapse/handlers/room.py b/synapse/handlers/room.py
--- a/synapse/handlers/room.py
+++ b/synapse/handlers/room.py
@@ -771,7 +771,9 @@ async def create_room(
                 % (user_id,),
             )
 
-        visibility = config.get("visibility", None)
+        # The spec says rooms should default to private visibility if
+        # `visibility` is not specified.
+        visibility = config.get("visibility", "private")
         is_public = visibility == "public"
 
         room_id = await self._generate_room_id(


Test patch:
diff --git a/tests/module_api/test_api.py b/tests/module_api/test_api.py
--- a/tests/module_api/test_api.py
+++ b/tests/module_api/test_api.py
@@ -292,7 +292,7 @@ def test_public_rooms(self):
         # Create a user and room to play with
         user_id = self.register_user("kermit", "monkey")
         tok = self.login("kermit", "monkey")
-        room_id = self.helper.create_room_as(user_id, tok=tok)
+        room_id = self.helper.create_room_as(user_id, tok=tok, is_public=False)
 
         # The room should not currently be in the public rooms directory
         is_in_public_rooms = self.get_success(
diff --git a/tests/rest/client/utils.py b/tests/rest/client/utils.py
--- a/tests/rest/client/utils.py
+++ b/tests/rest/client/utils.py
@@ -88,7 +88,7 @@ def create_room_as(
     def create_room_as(
         self,
         room_creator: Optional[str] = None,
-        is_public: Optional[bool] = None,
+        is_public: Optional[bool] = True,
         room_version: Optional[str] = None,
         tok: Optional[str] = None,
         expect_code: int = HTTPStatus.OK,
@@ -101,9 +101,12 @@ def create_room_as(
         Args:
             room_creator: The user ID to create the room with.
             is_public: If True, the `visibility` parameter will be set to
-                "public". If False, it will be set to "private". If left
-                unspecified, the server will set it to an appropriate default
-                (which should be "private" as per the CS spec).
+                "public". If False, it will be set to "private".
+                If None, doesn't specify the `visibility` parameter in which
+                case the server is supposed to make the room private according to
+                the CS API.
+                Defaults to public, since that is commonly needed in tests
+                for convenience where room privacy is not a problem.
             room_version: The room version to create the room as. Defaults to Synapse's
                 default room version.
             tok: The access token to use in the request.
diff --git a/tests/storage/test_cleanup_extrems.py b/tests/storage/test_cleanup_extrems.py
--- a/tests/storage/test_cleanup_extrems.py
+++ b/tests/storage/test_cleanup_extrems.py
@@ -250,7 +250,9 @@ def prepare(self, reactor, clock, homeserver):
         self.user = UserID.from_string(self.register_user("user1", "password"))
         self.token1 = self.login("user1", "password")
         self.requester = create_requester(self.user)
-        info, _ = self.get_success(self.room_creator.create_room(self.requester, {}))
+        info, _ = self.get_success(
+            self.room_creator.create_room(self.requester, {"visibility": "public"})
+        )
         self.room_id = info["room_id"]
         self.event_creator = homeserver.get_event_creation_handler()
         homeserver.config.consent.user_consent_version = self.CONSENT_VERSION