Create the correct events with the right configuration when creating a new room.

This commit is contained in:
Erik Johnston 2014-08-28 10:59:15 +01:00
parent f3f32addca
commit d2bc5d6f29
5 changed files with 104 additions and 21 deletions

View File

@ -42,3 +42,10 @@ class PresenceState(object):
UNAVAILABLE = u"unavailable" UNAVAILABLE = u"unavailable"
ONLINE = u"online" ONLINE = u"online"
FREE_FOR_CHAT = u"free_for_chat" FREE_FOR_CHAT = u"free_for_chat"
class JoinRules(object):
PUBLIC = u"public"
KNOCK = u"knock"
INVITE = u"invite"
PRIVATE = u"private"

View File

@ -152,3 +152,10 @@ class SynapseEvent(JsonEncodedObject):
msg = self._check_json(entry, template[key][0]) msg = self._check_json(entry, template[key][0])
if msg: if msg:
return msg return msg
class SynapseStateEvent(SynapseEvent):
def __init__(self, **kwargs):
if "state_key" not in kwargs:
kwargs["state_key"] = ""
super(SynapseStateEvent, self).__init__(**kwargs)

View File

@ -16,6 +16,8 @@
from synapse.api.events.room import ( from synapse.api.events.room import (
RoomTopicEvent, MessageEvent, RoomMemberEvent, FeedbackEvent, RoomTopicEvent, MessageEvent, RoomMemberEvent, FeedbackEvent,
InviteJoinEvent, RoomConfigEvent, RoomNameEvent, GenericEvent, InviteJoinEvent, RoomConfigEvent, RoomNameEvent, GenericEvent,
RoomPowerLevelsEvent, RoomDefaultLevelEvent, RoomJoinRulesEvent,
RoomCreateEvent,
) )
from synapse.util.stringutils import random_string from synapse.util.stringutils import random_string
@ -30,7 +32,11 @@ class EventFactory(object):
RoomMemberEvent, RoomMemberEvent,
FeedbackEvent, FeedbackEvent,
InviteJoinEvent, InviteJoinEvent,
RoomConfigEvent RoomConfigEvent,
RoomPowerLevelsEvent,
RoomDefaultLevelEvent,
RoomJoinRulesEvent,
RoomCreateEvent,
] ]
def __init__(self, hs): def __init__(self, hs):

View File

@ -15,7 +15,7 @@
from synapse.api.constants import Feedback, Membership from synapse.api.constants import Feedback, Membership
from synapse.api.errors import SynapseError from synapse.api.errors import SynapseError
from . import SynapseEvent from . import SynapseEvent, SynapseStateEvent
class GenericEvent(SynapseEvent): class GenericEvent(SynapseEvent):
@ -132,3 +132,31 @@ class RoomConfigEvent(SynapseEvent):
def get_content_template(self): def get_content_template(self):
return {} return {}
class RoomCreateEvent(SynapseStateEvent):
TYPE = "m.room.create"
def get_content_template(self):
return {}
class RoomJoinRulesEvent(SynapseStateEvent):
TYPE = "m.room.join_rules"
def get_content_template(self):
return {}
class RoomPowerLevelsEvent(SynapseStateEvent):
TYPE = "m.room.power_levels"
def get_content_template(self):
return {}
class RoomDefaultLevelEvent(SynapseStateEvent):
TYPE = "m.room.default_level"
def get_content_template(self):
return {}

View File

@ -17,10 +17,11 @@
from twisted.internet import defer from twisted.internet import defer
from synapse.types import UserID, RoomAlias, RoomID from synapse.types import UserID, RoomAlias, RoomID
from synapse.api.constants import Membership from synapse.api.constants import Membership, JoinRules
from synapse.api.errors import StoreError, SynapseError from synapse.api.errors import StoreError, SynapseError
from synapse.api.events.room import ( from synapse.api.events.room import (
RoomMemberEvent, RoomConfigEvent RoomMemberEvent, RoomCreateEvent, RoomPowerLevelsEvent,
RoomJoinRulesEvent, RoomDefaultLevelEvent,
) )
from synapse.util import stringutils from synapse.util import stringutils
from ._base import BaseRoomHandler from ._base import BaseRoomHandler
@ -62,6 +63,8 @@ class RoomCreationHandler(BaseRoomHandler):
else: else:
room_alias = None room_alias = None
is_public = config.get("visibility", None) == "public"
if room_id: if room_id:
# Ensure room_id is the correct type # Ensure room_id is the correct type
room_id_obj = RoomID.from_string(room_id, self.hs) room_id_obj = RoomID.from_string(room_id, self.hs)
@ -71,7 +74,7 @@ class RoomCreationHandler(BaseRoomHandler):
yield self.store.store_room( yield self.store.store_room(
room_id=room_id, room_id=room_id,
room_creator_user_id=user_id, room_creator_user_id=user_id,
is_public=config["visibility"] == "public" is_public=is_public
) )
else: else:
# autogen room IDs and try to create it. We may clash, so just # autogen room IDs and try to create it. We may clash, so just
@ -85,7 +88,7 @@ class RoomCreationHandler(BaseRoomHandler):
yield self.store.store_room( yield self.store.store_room(
room_id=gen_room_id.to_string(), room_id=gen_room_id.to_string(),
room_creator_user_id=user_id, room_creator_user_id=user_id,
is_public=config["visibility"] == "public" is_public=is_public
) )
room_id = gen_room_id.to_string() room_id = gen_room_id.to_string()
break break
@ -94,18 +97,10 @@ class RoomCreationHandler(BaseRoomHandler):
if not room_id: if not room_id:
raise StoreError(500, "Couldn't generate a room ID.") raise StoreError(500, "Couldn't generate a room ID.")
config_event = self.event_factory.create_event(
etype=RoomConfigEvent.TYPE,
room_id=room_id,
user_id=user_id,
content=config,
)
snapshot = yield self.store.snapshot_room( user = self.hs.parse_userid(user_id)
room_id=room_id, creation_events = self._create_events_for_new_room(
user_id=user_id, user, room_id, is_public=is_public
state_type=RoomConfigEvent.TYPE,
state_key="",
) )
if room_alias: if room_alias:
@ -115,11 +110,18 @@ class RoomCreationHandler(BaseRoomHandler):
servers=[self.hs.hostname], servers=[self.hs.hostname],
) )
yield self.state_handler.handle_new_event(config_event, snapshot)
# store_id = persist...
federation_handler = self.hs.get_handlers().federation_handler federation_handler = self.hs.get_handlers().federation_handler
yield federation_handler.handle_new_event(config_event, snapshot)
for event in creation_events:
snapshot = yield self.store.snapshot_room(
room_id=room_id,
user_id=user_id,
)
logger.debug("Event: %s", event)
yield self.state_handler.handle_new_event(event, snapshot)
yield self._on_new_room_event(event, snapshot, extra_users=[user])
content = {"membership": Membership.JOIN} content = {"membership": Membership.JOIN}
join_event = self.event_factory.create_event( join_event = self.event_factory.create_event(
@ -142,6 +144,39 @@ class RoomCreationHandler(BaseRoomHandler):
defer.returnValue(result) defer.returnValue(result)
def _create_events_for_new_room(self, creator, room_id, is_public=False):
event_keys = {
"room_id": room_id,
"user_id": creator.to_string(),
}
creation_event = self.event_factory.create_event(
etype=RoomCreateEvent.TYPE,
content={"creator": creator.to_string()},
**event_keys
)
power_levels_event = self.event_factory.create_event(
etype=RoomPowerLevelsEvent.TYPE,
content={creator.to_string(): 10},
**event_keys
)
default_level_event = self.event_factory.create_event(
etype=RoomDefaultLevelEvent.TYPE,
content={"default_level": 0},
**event_keys
)
join_rule = JoinRules.PUBLIC if is_public else JoinRules.INVITE
join_rules_event = self.event_factory.create_event(
etype=RoomJoinRulesEvent.TYPE,
content={"join_rule": join_rule},
**event_keys
)
return [creation_event, power_levels_event, default_level_event, join_rules_event]
class RoomMemberHandler(BaseRoomHandler): class RoomMemberHandler(BaseRoomHandler):
# TODO(paul): This handler currently contains a messy conflation of # TODO(paul): This handler currently contains a messy conflation of