From c456d17daf1457bb341bd3c64c616d500c6c2517 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Thu, 16 Jul 2015 15:25:29 +0100 Subject: [PATCH] Implement specifying custom initial state for /createRoom --- synapse/handlers/room.py | 100 +++++++++++++++++++++++---------------- 1 file changed, 60 insertions(+), 40 deletions(-) diff --git a/synapse/handlers/room.py b/synapse/handlers/room.py index c081efee3..7511d294f 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 config["original_invitees_have_ops"]: - for invitee in invite_list: - power_level_content["users"][invitee] = 100 + 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, + } - power_levels_event = create( - etype=EventTypes.PowerLevels, - content=power_level_content, - ) + if config["original_invitees_have_ops"]: + for invitee in invite_list: + power_level_content["users"][invitee] = 100 - join_rules_event = create( - etype=EventTypes.JoinRules, - content={"join_rule": config["join_rules"]}, - ) + power_levels_event = create( + etype=EventTypes.PowerLevels, + content=power_level_content, + ) - history_event = create( - etype=EventTypes.RoomHistoryVisibility, - content={"history_visibility": config["history_visibility"]} - ) + returned_events.append(power_levels_event) - return [ - creation_event, - join_event, - power_levels_event, - history_event, - join_rules_event, - ] + if (EventTypes.JoinRules, '') not in initial_state: + join_rules_event = create( + etype=EventTypes.JoinRules, + content={"join_rule": config["join_rules"]}, + ) + + 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):