Implement specifying custom initial state for /createRoom
This commit is contained in:
parent
002a44ac1a
commit
c456d17daf
|
@ -27,6 +27,7 @@ from synapse.util import stringutils, unwrapFirstError
|
||||||
from synapse.util.async import run_on_reactor
|
from synapse.util.async import run_on_reactor
|
||||||
from synapse.events.utils import serialize_event
|
from synapse.events.utils import serialize_event
|
||||||
|
|
||||||
|
from collections import OrderedDict
|
||||||
import logging
|
import logging
|
||||||
import string
|
import string
|
||||||
|
|
||||||
|
@ -143,11 +144,18 @@ class RoomCreationHandler(BaseHandler):
|
||||||
else RoomCreationPreset.PRIVATE_CHAT
|
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)
|
user = UserID.from_string(user_id)
|
||||||
creation_events = self._create_events_for_new_room(
|
creation_events = self._create_events_for_new_room(
|
||||||
user, room_id,
|
user, room_id,
|
||||||
preset_config=preset_config,
|
preset_config=preset_config,
|
||||||
invite_list=invite_list,
|
invite_list=invite_list,
|
||||||
|
initial_state=initial_state,
|
||||||
)
|
)
|
||||||
|
|
||||||
msg_handler = self.hs.get_handlers().message_handler
|
msg_handler = self.hs.get_handlers().message_handler
|
||||||
|
@ -195,7 +203,7 @@ class RoomCreationHandler(BaseHandler):
|
||||||
defer.returnValue(result)
|
defer.returnValue(result)
|
||||||
|
|
||||||
def _create_events_for_new_room(self, creator, room_id, preset_config,
|
def _create_events_for_new_room(self, creator, room_id, preset_config,
|
||||||
invite_list):
|
invite_list, initial_state):
|
||||||
config = RoomCreationHandler.PRESETS_DICT[preset_config]
|
config = RoomCreationHandler.PRESETS_DICT[preset_config]
|
||||||
|
|
||||||
creator_id = creator.to_string()
|
creator_id = creator.to_string()
|
||||||
|
@ -230,50 +238,62 @@ class RoomCreationHandler(BaseHandler):
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
power_level_content = {
|
returned_events = [creation_event, join_event]
|
||||||
"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"]:
|
if (EventTypes.PowerLevels, '') not in initial_state:
|
||||||
for invitee in invite_list:
|
power_level_content = {
|
||||||
power_level_content["users"][invitee] = 100
|
"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(
|
if config["original_invitees_have_ops"]:
|
||||||
etype=EventTypes.PowerLevels,
|
for invitee in invite_list:
|
||||||
content=power_level_content,
|
power_level_content["users"][invitee] = 100
|
||||||
)
|
|
||||||
|
|
||||||
join_rules_event = create(
|
power_levels_event = create(
|
||||||
etype=EventTypes.JoinRules,
|
etype=EventTypes.PowerLevels,
|
||||||
content={"join_rule": config["join_rules"]},
|
content=power_level_content,
|
||||||
)
|
)
|
||||||
|
|
||||||
history_event = create(
|
returned_events.append(power_levels_event)
|
||||||
etype=EventTypes.RoomHistoryVisibility,
|
|
||||||
content={"history_visibility": config["history_visibility"]}
|
|
||||||
)
|
|
||||||
|
|
||||||
return [
|
if (EventTypes.JoinRules, '') not in initial_state:
|
||||||
creation_event,
|
join_rules_event = create(
|
||||||
join_event,
|
etype=EventTypes.JoinRules,
|
||||||
power_levels_event,
|
content={"join_rule": config["join_rules"]},
|
||||||
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):
|
class RoomMemberHandler(BaseHandler):
|
||||||
|
|
Loading…
Reference in New Issue