From b30358f439aeec4e249c0d21d761cf6742e65578 Mon Sep 17 00:00:00 2001 From: Mark Haines Date: Wed, 27 Aug 2014 15:31:04 +0100 Subject: add _get_room_member, fix datastore methods --- synapse/api/auth.py | 4 +++- synapse/handlers/federation.py | 5 +++-- synapse/handlers/room.py | 2 +- synapse/storage/__init__.py | 12 ++++++------ synapse/storage/roommember.py | 22 ++++++++++++++++++++-- 5 files changed, 33 insertions(+), 12 deletions(-) diff --git a/synapse/api/auth.py b/synapse/api/auth.py index ef3604077d..2473a2b2bb 100644 --- a/synapse/api/auth.py +++ b/synapse/api/auth.py @@ -78,7 +78,9 @@ class Auth(object): def _check_joined_room(self, member, user_id, room_id): if not member or member.membership != Membership.JOIN: - raise AuthError(403, "User %s not in room %s" % (user_id, room_id)) + raise AuthError(403, "User %s not in room %s (%s)" % ( + user_id, room_id, repr(member) + )) @defer.inlineCallbacks def is_membership_change_allowed(self, event): diff --git a/synapse/handlers/federation.py b/synapse/handlers/federation.py index 4f773f6e58..7253f56322 100644 --- a/synapse/handlers/federation.py +++ b/synapse/handlers/federation.py @@ -214,7 +214,7 @@ class FederationHandler(BaseHandler): @log_function @defer.inlineCallbacks - def do_invite_join(self, target_host, room_id, joinee, content): + def do_invite_join(self, target_host, room_id, joinee, content, snapshot): hosts = yield self.store.get_joined_hosts_for_room(room_id) if self.hs.hostname in hosts: @@ -244,7 +244,8 @@ class FederationHandler(BaseHandler): new_event.destinations = [target_host] - yield self.handle_new_event(new_event) + snapshot.fill_out_prev_events(new_event) + yield self.handle_new_event(new_event, snapshot) # TODO (erikj): Time out here. d = defer.Deferred() diff --git a/synapse/handlers/room.py b/synapse/handlers/room.py index 47dcc6544d..7b4b051888 100644 --- a/synapse/handlers/room.py +++ b/synapse/handlers/room.py @@ -637,7 +637,7 @@ class RoomMemberHandler(BaseRoomHandler): if should_do_dance: handler = self.hs.get_handlers().federation_handler have_joined = yield handler.do_invite_join( - room_host, room_id, event.user_id, event.content + room_host, room_id, event.user_id, event.content, snapshot ) # We want to do the _do_update inside the room lock. diff --git a/synapse/storage/__init__.py b/synapse/storage/__init__.py index 8c2c82c1bb..514d7eeb69 100644 --- a/synapse/storage/__init__.py +++ b/synapse/storage/__init__.py @@ -94,11 +94,11 @@ class DataStore(RoomMemberStore, RoomStore, def _persist_pdu_event_txn(self, txn, pdu=None, event=None, backfilled=False, stream_ordering=None): if pdu is not None: - self._persist_pdu_txn(txn, pdu) + self._persist_event_pdu_txn(txn, pdu) if event is not None: self._persist_event_txn(txn, event, backfilled, stream_ordering) - def _persist_pdu_txn(self, txn, pdu): + def _persist_event_pdu_txn(self, txn, pdu): cols = dict(pdu.__dict__) unrec_keys = dict(pdu.unrecognized_keys) del cols["content"] @@ -185,7 +185,7 @@ class DataStore(RoomMemberStore, RoomStore, } ) - return self._get_room_events_max_id_(txn) + return self._get_room_events_max_id_txn(txn) @defer.inlineCallbacks def get_current_state(self, room_id, event_type=None, state_key=""): @@ -232,7 +232,7 @@ class DataStore(RoomMemberStore, RoomStore, synapse.storage.Snapshot: A snapshot of the state of the room. """ def _snapshot(txn): - membership_state = self._get_room_member(txn, user_id) + membership_state = self._get_room_member(txn, user_id, room_id) prev_pdus = self._get_latest_pdus_in_context( txn, room_id ) @@ -279,7 +279,7 @@ class Snapshot(object): self.room_id = room_id self.user_id = user_id self.prev_pdus = prev_pdus - self.membership_state + self.membership_state = membership_state self.state_type = state_type self.state_key = state_key self.prev_state_pdu = prev_state_pdu @@ -295,7 +295,7 @@ class Snapshot(object): event.prev_events = [e for e in es if e != event.event_id] if self.prev_pdus: - event.depth = max([int(v) for _, _, v in results]) + 1 + event.depth = max([int(v) for _, _, v in self.prev_pdus]) + 1 else: event.depth = 0 diff --git a/synapse/storage/roommember.py b/synapse/storage/roommember.py index 5038aeea03..2746126e85 100644 --- a/synapse/storage/roommember.py +++ b/synapse/storage/roommember.py @@ -50,13 +50,13 @@ class RoomMemberStore(SQLBaseStore): "INSERT OR IGNORE INTO room_hosts (room_id, host) " "VALUES (?, ?)" ) - txn.execute(sql, event.room_id, domain) + txn.execute(sql, (event.room_id, domain)) else: sql = ( "DELETE FROM room_hosts WHERE room_id = ? AND host = ?" ) - txn.execute(sql, event.room_id, domain) + txn.execute(sql, (event.room_id, domain)) @defer.inlineCallbacks def get_room_member(self, user_id, room_id): @@ -75,6 +75,24 @@ class RoomMemberStore(SQLBaseStore): defer.returnValue(rows[0] if rows else None) + def _get_room_member(self, txn, user_id, room_id): + sql = ( + "SELECT e.* FROM events as e" + " INNER JOIN room_memberships as m" + " ON e.event_id = m.event_id" + " INNER JOIN current_state_events as c" + " ON m.event_id = c.event_id" + " WHERE m.user_id = ? and e.room_id = ?" + " LIMIT 1" + ) + txn.execute(sql, (user_id, room_id)) + rows = self.cursor_to_dict(txn) + if rows: + return self._parse_event_from_row(rows[0]) + else: + return None + + def get_room_members(self, room_id, membership=None): """Retrieve the current room member list for a room. -- cgit 1.4.1