Some cleanup

I'm not particularly happy with the "action" switching, but there's no
convenient way to defer the work that needs to happen after it, so... :(
This commit is contained in:
Daniel Wagner-Hall 2016-02-17 15:50:13 +00:00
parent a4e278bfe7
commit 591af2d074
2 changed files with 59 additions and 67 deletions

View File

@ -397,7 +397,7 @@ class RoomMemberHandler(BaseHandler):
room_id, room_id,
action, action,
txn_id=None, txn_id=None,
room_hosts=None, remote_room_hosts=None,
ratelimit=True, ratelimit=True,
): ):
effective_membership_state = action effective_membership_state = action
@ -448,7 +448,7 @@ class RoomMemberHandler(BaseHandler):
context, context,
is_guest=requester.is_guest, is_guest=requester.is_guest,
ratelimit=ratelimit, ratelimit=ratelimit,
room_hosts=room_hosts, remote_room_hosts=remote_room_hosts,
from_client=True, from_client=True,
) )
@ -461,11 +461,12 @@ class RoomMemberHandler(BaseHandler):
event, event,
context, context,
is_guest=False, is_guest=False,
room_hosts=None, remote_room_hosts=None,
ratelimit=True, ratelimit=True,
from_client=True, from_client=True,
): ):
""" Change the membership status of a user in a room. """
Change the membership status of a user in a room.
Args: Args:
event (SynapseEvent): The membership event. event (SynapseEvent): The membership event.
@ -482,78 +483,64 @@ class RoomMemberHandler(BaseHandler):
Raises: Raises:
SynapseError if there was a problem changing the membership. SynapseError if there was a problem changing the membership.
""" """
user = UserID.from_string(event.sender) target_user = UserID.from_string(event.state_key)
room_id = event.room_id
if from_client: if from_client:
assert self.hs.is_mine(user), "User must be our own: %s" % (user,) sender = UserID.from_string(event.sender)
assert self.hs.is_mine(sender), "Sender must be our own: %s" % (sender,)
if event.is_state(): if event.is_state():
message_handler = self.hs.get_handlers().message_handler message_handler = self.hs.get_handlers().message_handler
prev_state = message_handler.deduplicate_state_event(event, context) prev_event = message_handler.deduplicate_state_event(event, context)
if prev_state is not None: if prev_event is not None:
return return
target_user = UserID.from_string(event.state_key) action = "send"
prev_state = context.current_state.get(
(EventTypes.Member, target_user.to_string()),
None
)
room_id = event.room_id
handled = False
if event.membership == Membership.JOIN: if event.membership == Membership.JOIN:
if is_guest and not self._can_guest_join(context.current_state): if is_guest and not self._can_guest_join(context.current_state):
# This should be an auth check, but guests are a local concept, # This should be an auth check, but guests are a local concept,
# so don't really fit into the general auth process. # so don't really fit into the general auth process.
raise AuthError(403, "Guest access not allowed") raise AuthError(403, "Guest access not allowed")
do_remote_join_dance, remote_room_hosts = self._should_do_dance(
should_do_dance, room_hosts = self._should_do_dance(
context, context,
(self.get_inviter(target_user.to_string(), context.current_state)), (self.get_inviter(event.state_key, context.current_state)),
room_hosts, remote_room_hosts,
) )
if do_remote_join_dance:
if should_do_dance: action = "remote_join"
if len(room_hosts) == 0: elif event.membership == Membership.LEAVE:
# return the same error as join_room_alias does
raise SynapseError(404, "No known servers")
# We don't do an auth check if we are doing an invite
# join dance for now, since we're kinda implicitly checking
# that we are allowed to join when we decide whether or not we
# need to do the invite/join dance.
yield self.hs.get_handlers().federation_handler.do_invite_join(
room_hosts,
room_id,
event.user_id,
event.content,
)
handled = True
if event.membership == Membership.LEAVE:
is_host_in_room = self.is_host_in_room(context.current_state) is_host_in_room = self.is_host_in_room(context.current_state)
if not is_host_in_room: if not is_host_in_room:
# Rejecting an invite, rather than leaving a joined room action = "remote_reject"
handler = self.hs.get_handlers().federation_handler
inviter = self.get_inviter(target_user.to_string(), context.current_state)
if not inviter:
# return the same error as join_room_alias does
raise SynapseError(404, "No known servers")
yield handler.do_remotely_reject_invite(
[inviter.domain],
room_id,
event.user_id
)
handled = True
# FIXME: This isn't idempotency. federation_handler = self.hs.get_handlers().federation_handler
if prev_state and prev_state.membership == event.membership:
# double same action, treat this event as a NOOP.
return
if not handled: if action == "remote_join":
if len(remote_room_hosts) == 0:
raise SynapseError(404, "No known servers")
# We don't do an auth check if we are doing an invite
# join dance for now, since we're kinda implicitly checking
# that we are allowed to join when we decide whether or not we
# need to do the invite/join dance.
yield federation_handler.do_invite_join(
remote_room_hosts,
event.room_id,
event.user_id,
event.content,
)
elif action == "remote_reject":
inviter = self.get_inviter(target_user.to_string(), context.current_state)
if not inviter:
raise SynapseError(404, "No known servers")
yield federation_handler.do_remotely_reject_invite(
[inviter.domain],
room_id,
event.user_id
)
else:
yield self.handle_new_client_event( yield self.handle_new_client_event(
event, event,
context, context,
@ -561,14 +548,19 @@ class RoomMemberHandler(BaseHandler):
ratelimit=ratelimit, ratelimit=ratelimit,
) )
prev_member_event = context.current_state.get(
(EventTypes.Member, target_user.to_string()),
None
)
if event.membership == Membership.JOIN: if event.membership == Membership.JOIN:
if not prev_state or prev_state.membership != Membership.JOIN: if not prev_member_event or prev_member_event.membership != Membership.JOIN:
# Only fire user_joined_room if the user has acutally joined the # Only fire user_joined_room if the user has acutally joined the
# room. Don't bother if the user is just changing their profile # room. Don't bother if the user is just changing their profile
# info. # info.
yield user_joined_room(self.distributor, target_user, room_id) yield user_joined_room(self.distributor, target_user, room_id)
elif event.membership == Membership.LEAVE: elif event.membership == Membership.LEAVE:
if prev_state and prev_state.membership == Membership.JOIN: if prev_member_event and prev_member_event.membership == Membership.JOIN:
user_left_room(self.distributor, target_user, room_id) user_left_room(self.distributor, target_user, room_id)
def _can_guest_join(self, current_state): def _can_guest_join(self, current_state):
@ -604,7 +596,9 @@ class RoomMemberHandler(BaseHandler):
Args: Args:
room_alias (RoomAlias): The alias to look up. room_alias (RoomAlias): The alias to look up.
Returns: Returns:
The room ID as a RoomID object. A tuple of:
The room ID as a RoomID object.
Hosts likely to be participating in the room ([str]).
Raises: Raises:
SynapseError if room alias could not be found. SynapseError if room alias could not be found.
""" """
@ -615,11 +609,9 @@ class RoomMemberHandler(BaseHandler):
raise SynapseError(404, "No such room alias") raise SynapseError(404, "No such room alias")
room_id = mapping["room_id"] room_id = mapping["room_id"]
hosts = mapping["servers"] servers = mapping["servers"]
if not hosts:
raise SynapseError(404, "No known servers")
defer.returnValue((RoomID.from_string(room_id), hosts)) defer.returnValue((RoomID.from_string(room_id), servers))
def get_inviter(self, user_id, current_state): def get_inviter(self, user_id, current_state):
prev_state = current_state.get((EventTypes.Member, user_id)) prev_state = current_state.get((EventTypes.Member, user_id))

View File

@ -230,11 +230,11 @@ class JoinRoomAliasServlet(ClientV1RestServlet):
if RoomID.is_valid(room_identifier): if RoomID.is_valid(room_identifier):
room_id = room_identifier room_id = room_identifier
room_hosts = None remote_room_hosts = None
elif RoomAlias.is_valid(room_identifier): elif RoomAlias.is_valid(room_identifier):
handler = self.handlers.room_member_handler handler = self.handlers.room_member_handler
room_alias = RoomAlias.from_string(room_identifier) room_alias = RoomAlias.from_string(room_identifier)
room_id, room_hosts = yield handler.lookup_room_alias(room_alias) room_id, remote_room_hosts = yield handler.lookup_room_alias(room_alias)
room_id = room_id.to_string() room_id = room_id.to_string()
else: else:
raise SynapseError(400, "%s was not legal room ID or room alias" % ( raise SynapseError(400, "%s was not legal room ID or room alias" % (
@ -247,7 +247,7 @@ class JoinRoomAliasServlet(ClientV1RestServlet):
room_id=room_id, room_id=room_id,
action="join", action="join",
txn_id=txn_id, txn_id=txn_id,
room_hosts=room_hosts, remote_room_hosts=remote_room_hosts,
) )
defer.returnValue((200, {"room_id": room_id})) defer.returnValue((200, {"room_id": room_id}))