summary refs log tree commit diff
diff options
context:
space:
mode:
authorAndrew Morgan <andrewm@element.io>2022-07-07 17:00:14 +0100
committerAndrew Morgan <andrewm@element.io>2022-07-07 17:00:14 +0100
commit67b5b013eb9067fdad66f49971a2156c2ff1a3ad (patch)
tree8aed46404a113f8aff76604252177b462f231090
parentAdd --build-only option to complement.sh to prevent actually running Compleme... (diff)
downloadsynapse-67b5b013eb9067fdad66f49971a2156c2ff1a3ad.tar.xz
wip
-rw-r--r--synapse/config/experimental.py3
-rw-r--r--synapse/handlers/room.py16
-rw-r--r--synapse/module_api/__init__.py27
3 files changed, 40 insertions, 6 deletions
diff --git a/synapse/config/experimental.py b/synapse/config/experimental.py
index ee443cea00..6e16c61532 100644
--- a/synapse/config/experimental.py
+++ b/synapse/config/experimental.py
@@ -90,3 +90,6 @@ class ExperimentalConfig(Config):
 
         # MSC3827: Filtering of /publicRooms by room type
         self.msc3827_enabled: bool = experimental.get("msc3827_enabled", False)
+
+        # MSCXXXX: Custom room presets.
+        self.mscxxxx_enabled: bool = experimental.get("mscxxxx_enabled", False)
\ No newline at end of file
diff --git a/synapse/handlers/room.py b/synapse/handlers/room.py
index 75c0be8c36..14189b98e8 100644
--- a/synapse/handlers/room.py
+++ b/synapse/handlers/room.py
@@ -117,11 +117,14 @@ class RoomCreationHandler:
         self.event_creation_handler = hs.get_event_creation_handler()
         self.room_member_handler = hs.get_room_member_handler()
         self._event_auth_handler = hs.get_event_auth_handler()
-        self.config = hs.config
         self.request_ratelimiter = hs.get_request_ratelimiter()
 
+        self.config = hs.config
+        self._allow_custom_room_presets = hs.config.experimental.mscxxxx_enabled
+        self.custom_room_presets: Dict[str, Tuple] = {}
+
         # Room state based off defined presets
-        self._presets_dict: Dict[str, Dict[str, Any]] = {
+        self._default_presets_dict: Dict[str, Dict[str, Any]] = {
             RoomCreationPreset.PRIVATE_CHAT: {
                 "join_rules": JoinRules.INVITE,
                 "history_visibility": HistoryVisibility.SHARED,
@@ -146,7 +149,7 @@ class RoomCreationHandler:
         }
 
         # Modify presets to selectively enable encryption by default per homeserver config
-        for preset_name, preset_config in self._presets_dict.items():
+        for preset_name, preset_config in self._default_presets_dict.items():
             encrypted = (
                 preset_name
                 in self.config.room.encryption_enabled_by_default_for_room_presets
@@ -1035,9 +1038,10 @@ class RoomCreationHandler:
             )
             return last_stream_id
 
-        try:
-            config = self._presets_dict[preset_config]
-        except KeyError:
+        config = self._default_presets_dict.get(preset_config)
+        if config is None and self._allow_custom_room_presets:
+            config = self._default_presets_dict[preset_config]
+
             raise SynapseError(
                 400, f"'{preset_config}' is not a valid preset", errcode=Codes.BAD_JSON
             )
diff --git a/synapse/module_api/__init__.py b/synapse/module_api/__init__.py
index 6191c2dc96..a302890db9 100644
--- a/synapse/module_api/__init__.py
+++ b/synapse/module_api/__init__.py
@@ -14,6 +14,7 @@
 # limitations under the License.
 import email.utils
 import logging
+import inspect
 from typing import (
     TYPE_CHECKING,
     Any,
@@ -23,6 +24,7 @@ from typing import (
     Iterable,
     List,
     Optional,
+    Sequence,
     Tuple,
     TypeVar,
     Union,
@@ -206,6 +208,7 @@ class ModuleApi:
         self._registration_handler = hs.get_registration_handler()
         self._send_email_handler = hs.get_send_email_handler()
         self._push_rules_handler = hs.get_push_rules_handler()
+        self._room_creation_handler = hs.get_room_creation_handler()
         self.custom_template_dir = hs.config.server.custom_template_directory
 
         try:
@@ -860,6 +863,30 @@ class ModuleApi:
             client_redirect_url,
         )
 
+    async def define_supported_custom_room_presets(
+        self,
+        room_preset_names: Sequence,
+    ) -> None:
+        """Define the list of custom room presets that your module supports.
+
+        Custom room presets names defined with this method will be available
+        for use by clients. If not defined, clients will receive an error indicating
+        the room preset name is not recognised.
+
+        Added in Synapse v1.XX.Y.
+        """
+        # We need a unique identifier for each calling module, so that when
+        # this method is called again, the previous list of room presets can
+        # be identified and replaced.
+
+        # Get the calling module's ...
+        caller_frame = inspect.stack()[1]
+        module_filepath = inspect.getmodule(caller_frame[0]).__file__
+        if module_filepath:
+            self._room_creation_handler.custom_room_presets.update(
+                {module_filepath: room_preset_names}
+            )
+
     async def complete_sso_login_async(
         self,
         registered_user_id: str,