Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Implement presets at room creation #203

Merged
merged 4 commits into from
Jul 16, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions synapse/api/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,8 @@ class RejectedReason(object):
AUTH_ERROR = "auth_error"
REPLACED = "replaced"
NOT_ANCESTOR = "not_ancestor"


class RoomCreationPreset(object):
PrivateChat = "private_chat"
PublicChat = "public_chat"
Copy link
Member

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 :)

82 changes: 60 additions & 22 deletions synapse/handlers/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be nicer to have this as a namedtuple rather than an arbitrary dict? This would ensure than any new presets we add MUST specify all fields, rather than risking people missing one off and have it crash at runtime with KeyErrors due to direct config[key] accesses.

},
}

@defer.inlineCallbacks
def create_room(self, user_id, room_id, config):
""" Creates a new room.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a little unclear that everyone_ops really just means the room creator and the people in the initial invite list, no one else who subsequently joins/is invited. Surely if we really mean "everyone" then users_default should be 100?


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,
]

Expand Down