summary refs log tree commit diff
path: root/synapse
diff options
context:
space:
mode:
authorErik Johnston <erik@matrix.org>2015-07-16 15:25:29 +0100
committerErik Johnston <erik@matrix.org>2015-07-16 15:25:29 +0100
commitc456d17daf1457bb341bd3c64c616d500c6c2517 (patch)
treedcb06fe6edc9cc66fef6ae1f9832fd1cd923fd15 /synapse
parents/everyone_ops/original_invitees_have_ops/ (diff)
downloadsynapse-c456d17daf1457bb341bd3c64c616d500c6c2517.tar.xz
Implement specifying custom initial state for /createRoom
Diffstat (limited to 'synapse')
-rw-r--r--synapse/handlers/room.py100
1 files changed, 60 insertions, 40 deletions
diff --git a/synapse/handlers/room.py b/synapse/handlers/room.py
index c081efee3a..7511d294f3 100644
--- a/synapse/handlers/room.py
+++ b/synapse/handlers/room.py
@@ -27,6 +27,7 @@ from synapse.util import stringutils, unwrapFirstError
 from synapse.util.async import run_on_reactor
 from synapse.events.utils import serialize_event
 
+from collections import OrderedDict
 import logging
 import string
 
@@ -143,11 +144,18 @@ class RoomCreationHandler(BaseHandler):
             else RoomCreationPreset.PRIVATE_CHAT
         )
 
+        raw_initial_state = config.get("initial_state", [])
+
+        initial_state = OrderedDict()
+        for val in raw_initial_state:
+            initial_state[(val["type"], val.get("state_key", ""))] = val["content"]
+
         user = UserID.from_string(user_id)
         creation_events = self._create_events_for_new_room(
             user, room_id,
             preset_config=preset_config,
             invite_list=invite_list,
+            initial_state=initial_state,
         )
 
         msg_handler = self.hs.get_handlers().message_handler
@@ -195,7 +203,7 @@ class RoomCreationHandler(BaseHandler):
         defer.returnValue(result)
 
     def _create_events_for_new_room(self, creator, room_id, preset_config,
-                                    invite_list):
+                                    invite_list, initial_state):
         config = RoomCreationHandler.PRESETS_DICT[preset_config]
 
         creator_id = creator.to_string()
@@ -230,50 +238,62 @@ class RoomCreationHandler(BaseHandler):
             },
         )
 
-        power_level_content = {
-            "users": {
-                creator.to_string(): 100,
-            },
-            "users_default": 0,
-            "events": {
-                EventTypes.Name: 100,
-                EventTypes.PowerLevels: 100,
-                EventTypes.RoomHistoryVisibility: 100,
-            },
-            "events_default": 0,
-            "state_default": 50,
-            "ban": 50,
-            "kick": 50,
-            "redact": 50,
-            "invite": 0,
-        }
+        returned_events = [creation_event, join_event]
+
+        if (EventTypes.PowerLevels, '') not in initial_state:
+            power_level_content = {
+                "users": {
+                    creator.to_string(): 100,
+                },
+                "users_default": 0,
+                "events": {
+                    EventTypes.Name: 100,
+                    EventTypes.PowerLevels: 100,
+                    EventTypes.RoomHistoryVisibility: 100,
+                },
+                "events_default": 0,
+                "state_default": 50,
+                "ban": 50,
+                "kick": 50,
+                "redact": 50,
+                "invite": 0,
+            }
 
-        if config["original_invitees_have_ops"]:
-            for invitee in invite_list:
-                power_level_content["users"][invitee] = 100
+            if config["original_invitees_have_ops"]:
+                for invitee in invite_list:
+                    power_level_content["users"][invitee] = 100
 
-        power_levels_event = create(
-            etype=EventTypes.PowerLevels,
-            content=power_level_content,
-        )
+            power_levels_event = create(
+                etype=EventTypes.PowerLevels,
+                content=power_level_content,
+            )
 
-        join_rules_event = create(
-            etype=EventTypes.JoinRules,
-            content={"join_rule": config["join_rules"]},
-        )
+            returned_events.append(power_levels_event)
 
-        history_event = create(
-            etype=EventTypes.RoomHistoryVisibility,
-            content={"history_visibility": config["history_visibility"]}
-        )
+        if (EventTypes.JoinRules, '') not in initial_state:
+            join_rules_event = create(
+                etype=EventTypes.JoinRules,
+                content={"join_rule": config["join_rules"]},
+            )
 
-        return [
-            creation_event,
-            join_event,
-            power_levels_event,
-            history_event,
-            join_rules_event,
-        ]
+            returned_events.append(join_rules_event)
+
+        if (EventTypes.RoomHistoryVisibility, '') not in initial_state:
+            history_event = create(
+                etype=EventTypes.RoomHistoryVisibility,
+                content={"history_visibility": config["history_visibility"]}
+            )
+
+            returned_events.append(history_event)
+
+        for (etype, state_key), content in initial_state.items():
+            returned_events.append(create(
+                etype=etype,
+                state_key=state_key,
+                content=content,
+            ))
+
+        return returned_events
 
 
 class RoomMemberHandler(BaseHandler):