@@ -49,7 +49,7 @@ def __nonzero__(self):
4949class JoinedSyncResult (collections .namedtuple ("JoinedSyncResult" , [
5050 "room_id" , # str
5151 "timeline" , # TimelineBatch
52- "state" , # list[ FrozenEvent]
52+ "state" , # dict[(str, str), FrozenEvent]
5353 "ephemeral" ,
5454 "private_user_data" ,
5555])):
@@ -70,7 +70,7 @@ def __nonzero__(self):
7070class ArchivedSyncResult (collections .namedtuple ("JoinedSyncResult" , [
7171 "room_id" , # str
7272 "timeline" , # TimelineBatch
73- "state" , # list[ FrozenEvent]
73+ "state" , # dict[(str, str), FrozenEvent]
7474 "private_user_data" ,
7575])):
7676 __slots__ = []
@@ -257,12 +257,11 @@ def full_state_sync_for_joined_room(self, room_id, sync_config,
257257 current_state = yield self .state_handler .get_current_state (
258258 room_id
259259 )
260- current_state_events = current_state .values ()
261260
262261 defer .returnValue (JoinedSyncResult (
263262 room_id = room_id ,
264263 timeline = batch ,
265- state = current_state_events ,
264+ state = current_state ,
266265 ephemeral = ephemeral_by_room .get (room_id , []),
267266 private_user_data = self .private_user_data_for_room (
268267 room_id , tags_by_room
@@ -361,7 +360,7 @@ def full_state_sync_for_archived_room(self, room_id, sync_config,
361360 defer .returnValue (ArchivedSyncResult (
362361 room_id = room_id ,
363362 timeline = batch ,
364- state = leave_state [leave_event_id ]. values () ,
363+ state = leave_state [leave_event_id ],
365364 private_user_data = self .private_user_data_for_room (
366365 room_id , tags_by_room
367366 ),
@@ -440,7 +439,10 @@ def incremental_sync_with_gap(self, sync_config, since_token):
440439
441440 for room_id in joined_room_ids :
442441 recents = events_by_room_id .get (room_id , [])
443- state = [event for event in recents if event .is_state ()]
442+ state = {
443+ (event .type , event .state_key ): event
444+ for event in recents if event .is_state ()}
445+
444446 if recents :
445447 prev_batch = now_token .copy_and_replace (
446448 "room_key" , recents [0 ].internal_metadata .before
@@ -575,7 +577,6 @@ def incremental_sync_with_gap_for_room(self, room_id, sync_config,
575577 current_state = yield self .state_handler .get_current_state (
576578 room_id
577579 )
578- current_state_events = current_state .values ()
579580
580581 state_at_previous_sync = yield self .get_state_at_previous_sync (
581582 room_id , since_token = since_token
@@ -584,7 +585,7 @@ def incremental_sync_with_gap_for_room(self, room_id, sync_config,
584585 state_events_delta = yield self .compute_state_delta (
585586 since_token = since_token ,
586587 previous_state = state_at_previous_sync ,
587- current_state = current_state_events ,
588+ current_state = current_state ,
588589 )
589590
590591 state_events_delta , _ = yield self .check_joined_room (
@@ -632,7 +633,7 @@ def incremental_sync_for_archived_room(self, sync_config, leave_event,
632633 [leave_event .event_id ], None
633634 )
634635
635- state_events_at_leave = leave_state [leave_event .event_id ]. values ()
636+ state_events_at_leave = leave_state [leave_event .event_id ]
636637
637638 state_at_previous_sync = yield self .get_state_at_previous_sync (
638639 leave_event .room_id , since_token = since_token
@@ -661,7 +662,7 @@ def incremental_sync_for_archived_room(self, sync_config, leave_event,
661662 def get_state_at_previous_sync (self , room_id , since_token ):
662663 """ Get the room state at the previous sync the client made.
663664 Returns:
664- A Deferred list of Events.
665+ A Deferred map from ((type, state_key)->Event)
665666 """
666667 last_events , token = yield self .store .get_recent_events_for_room (
667668 room_id , end_token = since_token .room_key , limit = 1 ,
@@ -673,33 +674,36 @@ def get_state_at_previous_sync(self, room_id, since_token):
673674 last_event
674675 )
675676 if last_event .is_state ():
676- state = [last_event ] + last_context .current_state .values ()
677+ state = last_context .current_state .copy ()
678+ state [(last_event .type , last_event .state_key )] = last_event
677679 else :
678- state = last_context .current_state . values ()
680+ state = last_context .current_state
679681 else :
680- state = ()
682+ state = {}
681683 defer .returnValue (state )
682684
683685 def compute_state_delta (self , since_token , previous_state , current_state ):
684686 """ Works out the differnce in state between the current state and the
685687 state the client got when it last performed a sync.
686688
687689 :param str since_token: the point we are comparing against
688- :param list[synapse.events.FrozenEvent] previous_state: the state to
689- compare to
690- :param list[synapse.events.FrozenEvent] current_state: the new state
690+ :param dict[(str,str), synapse.events.FrozenEvent] previous_state: the
691+ state to compare to
692+ :param dict[(str,str), synapse.events.FrozenEvent] current_state: the
693+ new state
691694
692- :returns: A list of events.
695+ :returns A new event dictionary
693696 """
694697 # TODO(mjark) Check if the state events were received by the server
695698 # after the previous sync, since we need to include those state
696699 # updates even if they occured logically before the previous event.
697700 # TODO(mjark) Check for new redactions in the state events.
698- previous_dict = {event .event_id : event for event in previous_state }
699- state_delta = []
700- for event in current_state :
701- if event .event_id not in previous_dict :
702- state_delta .append (event )
701+
702+ state_delta = {}
703+ for key , event in current_state .iteritems ():
704+ if (key not in previous_state or
705+ previous_state [key ].event_id != event .event_id ):
706+ state_delta [key ] = event
703707 return state_delta
704708
705709 @defer .inlineCallbacks
@@ -708,21 +712,25 @@ def check_joined_room(self, sync_config, room_id, state_delta):
708712 Check if the user has just joined the given room. If so, return the
709713 full state for the room, instead of the delta since the last sync.
710714
715+ :param sync_config:
716+ :param room_id:
717+ :param dict[(str,str), synapse.events.FrozenEvent] state_delta: the
718+ difference in state since the last sync
719+
711720 :returns A deferred Tuple (state_delta, limited)
712721 """
713722 joined = False
714723 limited = False
715- for event in state_delta :
716- if (
717- event .type == EventTypes .Member
718- and event .state_key == sync_config .user .to_string ()
719- ):
720- if event .content ["membership" ] == Membership .JOIN :
721- joined = True
724+
725+ join_event = state_delta .get ((
726+ EventTypes .Member , sync_config .user .to_string ()), None )
727+ if join_event is not None :
728+ if join_event .content ["membership" ] == Membership .JOIN :
729+ joined = True
722730
723731 if joined :
724- res = yield self .state_handler .get_current_state (room_id )
725- state_delta = res . values ()
732+ state_delta = yield self .state_handler .get_current_state (room_id )
733+ # the timeline is inherently limited if we've just joined
726734 limited = True
727735
728736 defer .returnValue ((state_delta , limited ))
0 commit comments