-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Implement presets at room creation #203
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,9 @@ | |
from ._base import BaseHandler | ||
|
||
from synapse.types import UserID, RoomAlias, RoomID | ||
from synapse.api.constants import EventTypes, Membership, JoinRules | ||
from synapse.api.constants import ( | ||
EventTypes, Membership, JoinRules, RoomCreationPreset, | ||
) | ||
from synapse.api.errors import StoreError, SynapseError | ||
from synapse.util import stringutils, unwrapFirstError | ||
from synapse.util.async import run_on_reactor | ||
|
@@ -33,6 +35,19 @@ | |
|
||
class RoomCreationHandler(BaseHandler): | ||
|
||
PRESETS_DICT = { | ||
RoomCreationPreset.PrivateChat: { | ||
"join_rules": JoinRules.INVITE, | ||
"history_visibility": "invited", | ||
"everyone_ops": False, | ||
}, | ||
RoomCreationPreset.PublicChat: { | ||
"join_rules": JoinRules.PUBLIC, | ||
"history_visibility": "shared", | ||
"everyone_ops": False, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be nicer to have this as a |
||
}, | ||
} | ||
|
||
@defer.inlineCallbacks | ||
def create_room(self, user_id, room_id, config): | ||
""" Creates a new room. | ||
|
@@ -121,9 +136,18 @@ def create_room(self, user_id, room_id, config): | |
servers=[self.hs.hostname], | ||
) | ||
|
||
preset_config = config.get( | ||
"preset", | ||
RoomCreationPreset.PublicChat | ||
if is_public | ||
else RoomCreationPreset.PrivateChat | ||
) | ||
|
||
user = UserID.from_string(user_id) | ||
creation_events = self._create_events_for_new_room( | ||
user, room_id, is_public=is_public | ||
user, room_id, | ||
preset_config=preset_config, | ||
invite_list=invite_list, | ||
) | ||
|
||
msg_handler = self.hs.get_handlers().message_handler | ||
|
@@ -170,7 +194,10 @@ def create_room(self, user_id, room_id, config): | |
|
||
defer.returnValue(result) | ||
|
||
def _create_events_for_new_room(self, creator, room_id, is_public=False): | ||
def _create_events_for_new_room(self, creator, room_id, preset_config, | ||
invite_list): | ||
config = RoomCreationHandler.PRESETS_DICT[preset_config] | ||
|
||
creator_id = creator.to_string() | ||
|
||
event_keys = { | ||
|
@@ -203,37 +230,48 @@ def create(etype, content, **kwargs): | |
}, | ||
) | ||
|
||
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["everyone_ops"]: | ||
for invitee in invite_list: | ||
power_level_content["users"][invitee] = 100 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's a little unclear that |
||
|
||
power_levels_event = create( | ||
etype=EventTypes.PowerLevels, | ||
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, | ||
}, | ||
content=power_level_content, | ||
) | ||
|
||
join_rule = JoinRules.PUBLIC if is_public else JoinRules.INVITE | ||
join_rules_event = create( | ||
etype=EventTypes.JoinRules, | ||
content={"join_rule": join_rule}, | ||
content={"join_rule": config["join_rules"]}, | ||
) | ||
|
||
history_event = create( | ||
etype=EventTypes.RoomHistoryVisibility, | ||
content={"history_visibility": config["history_visibility"]} | ||
) | ||
|
||
return [ | ||
creation_event, | ||
join_event, | ||
power_levels_event, | ||
history_event, | ||
join_rules_event, | ||
] | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would really prefer
CONSTANT_NAMES
here :)