@@ -254,9 +254,7 @@ def full_state_sync_for_joined_room(self, room_id, sync_config,
254254 room_id , sync_config , now_token , since_token = timeline_since_token
255255 )
256256
257- current_state = yield self .state_handler .get_current_state (
258- room_id
259- )
257+ current_state = yield self .get_state_at (room_id , now_token )
260258
261259 defer .returnValue (JoinedSyncResult (
262260 room_id = room_id ,
@@ -353,14 +351,12 @@ def full_state_sync_for_archived_room(self, room_id, sync_config,
353351 room_id , sync_config , leave_token , since_token = timeline_since_token
354352 )
355353
356- leave_state = yield self .store .get_state_for_events (
357- [leave_event_id ], None
358- )
354+ leave_state = yield self .store .get_state_for_event (leave_event_id )
359355
360356 defer .returnValue (ArchivedSyncResult (
361357 room_id = room_id ,
362358 timeline = batch ,
363- state = leave_state [ leave_event_id ] ,
359+ state = leave_state ,
364360 private_user_data = self .private_user_data_for_room (
365361 room_id , tags_by_room
366362 ),
@@ -424,6 +420,9 @@ def incremental_sync_with_gap(self, sync_config, since_token):
424420 if len (room_events ) <= timeline_limit :
425421 # There is no gap in any of the rooms. Therefore we can just
426422 # partition the new events by room and return them.
423+ logger .debug ("Got %i events for incremental sync - not limited" ,
424+ len (room_events ))
425+
427426 invite_events = []
428427 leave_events = []
429428 events_by_room_id = {}
@@ -439,9 +438,11 @@ def incremental_sync_with_gap(self, sync_config, since_token):
439438
440439 for room_id in joined_room_ids :
441440 recents = events_by_room_id .get (room_id , [])
441+ logger .debug ("Events for room %s: %r" , room_id , recents )
442442 state = {
443443 (event .type , event .state_key ): event
444444 for event in recents if event .is_state ()}
445+ limited = False
445446
446447 if recents :
447448 prev_batch = now_token .copy_and_replace (
@@ -450,9 +451,13 @@ def incremental_sync_with_gap(self, sync_config, since_token):
450451 else :
451452 prev_batch = now_token
452453
453- state , limited = yield self .check_joined_room (
454- sync_config , room_id , state
455- )
454+ just_joined = yield self .check_joined_room (sync_config , state )
455+ if just_joined :
456+ logger .debug ("User has just joined %s: needs full state" ,
457+ room_id )
458+ state = yield self .get_state_at (room_id , now_token )
459+ # the timeline is inherently limited if we've just joined
460+ limited = True
456461
457462 room_sync = JoinedSyncResult (
458463 room_id = room_id ,
@@ -467,10 +472,15 @@ def incremental_sync_with_gap(self, sync_config, since_token):
467472 room_id , tags_by_room
468473 ),
469474 )
475+ logger .debug ("Result for room %s: %r" , room_id , room_sync )
476+
470477 if room_sync :
471478 joined .append (room_sync )
472479
473480 else :
481+ logger .debug ("Got %i events for incremental sync - hit limit" ,
482+ len (room_events ))
483+
474484 invite_events = yield self .store .get_invites_for_user (
475485 sync_config .user .to_string ()
476486 )
@@ -563,6 +573,8 @@ def incremental_sync_with_gap_for_room(self, room_id, sync_config,
563573 Returns:
564574 A Deferred JoinedSyncResult
565575 """
576+ logger .debug ("Doing incremental sync for room %s between %s and %s" ,
577+ room_id , since_token , now_token )
566578
567579 # TODO(mjark): Check for redactions we might have missed.
568580
@@ -572,30 +584,26 @@ def incremental_sync_with_gap_for_room(self, room_id, sync_config,
572584
573585 logging .debug ("Recents %r" , batch )
574586
575- # TODO(mjark): This seems racy since this isn't being passed a
576- # token to indicate what point in the stream this is
577- current_state = yield self .state_handler .get_current_state (
578- room_id
579- )
587+ current_state = yield self .get_state_at (room_id , now_token )
580588
581- state_at_previous_sync = yield self .get_state_at_previous_sync (
582- room_id , since_token = since_token
589+ state_at_previous_sync = yield self .get_state_at (
590+ room_id , stream_position = since_token
583591 )
584592
585- state_events_delta = yield self .compute_state_delta (
593+ state = yield self .compute_state_delta (
586594 since_token = since_token ,
587595 previous_state = state_at_previous_sync ,
588596 current_state = current_state ,
589597 )
590598
591- state_events_delta , _ = yield self .check_joined_room (
592- sync_config , room_id , state_events_delta
593- )
599+ just_joined = yield self .check_joined_room (sync_config , state )
600+ if just_joined :
601+ state = yield self . get_state_at ( room_id , now_token )
594602
595603 room_sync = JoinedSyncResult (
596604 room_id = room_id ,
597605 timeline = batch ,
598- state = state_events_delta ,
606+ state = state ,
599607 ephemeral = ephemeral_by_room .get (room_id , []),
600608 private_user_data = self .private_user_data_for_room (
601609 room_id , tags_by_room
@@ -627,16 +635,12 @@ def incremental_sync_for_archived_room(self, sync_config, leave_event,
627635
628636 logging .debug ("Recents %r" , batch )
629637
630- # TODO(mjark): This seems racy since this isn't being passed a
631- # token to indicate what point in the stream this is
632- leave_state = yield self .store .get_state_for_events (
633- [leave_event .event_id ], None
638+ state_events_at_leave = yield self .store .get_state_for_event (
639+ leave_event .event_id
634640 )
635641
636- state_events_at_leave = leave_state [leave_event .event_id ]
637-
638- state_at_previous_sync = yield self .get_state_at_previous_sync (
639- leave_event .room_id , since_token = since_token
642+ state_at_previous_sync = yield self .get_state_at (
643+ leave_event .room_id , stream_position = since_token
640644 )
641645
642646 state_events_delta = yield self .compute_state_delta (
@@ -659,26 +663,36 @@ def incremental_sync_for_archived_room(self, sync_config, leave_event,
659663 defer .returnValue (room_sync )
660664
661665 @defer .inlineCallbacks
662- def get_state_at_previous_sync (self , room_id , since_token ):
663- """ Get the room state at the previous sync the client made.
664- Returns:
665- A Deferred map from ((type, state_key)->Event)
666+ def get_state_after_event (self , event ):
667+ """
668+ Get the room state after the given event
669+
670+ :param synapse.events.EventBase event: event of interest
671+ :return: A Deferred map from ((type, state_key)->Event)
672+ """
673+ state = yield self .store .get_state_for_event (event .event_id )
674+ if event .is_state ():
675+ state = state .copy ()
676+ state [(event .type , event .state_key )] = event
677+ defer .returnValue (state )
678+
679+ @defer .inlineCallbacks
680+ def get_state_at (self , room_id , stream_position ):
681+ """ Get the room state at a particular stream position
682+ :param str room_id: room for which to get state
683+ :param StreamToken stream_position: point at which to get state
684+ :returns: A Deferred map from ((type, state_key)->Event)
666685 """
667686 last_events , token = yield self .store .get_recent_events_for_room (
668- room_id , end_token = since_token .room_key , limit = 1 ,
687+ room_id , end_token = stream_position .room_key , limit = 1 ,
669688 )
670689
671690 if last_events :
672- last_event = last_events [0 ]
673- last_context = yield self .state_handler .compute_event_context (
674- last_event
675- )
676- if last_event .is_state ():
677- state = last_context .current_state .copy ()
678- state [(last_event .type , last_event .state_key )] = last_event
679- else :
680- state = last_context .current_state
691+ last_event = last_events [- 1 ]
692+ state = yield self .get_state_after_event (last_event )
693+
681694 else :
695+ # no events in this room - so presumably no state
682696 state = {}
683697 defer .returnValue (state )
684698
@@ -706,31 +720,20 @@ def compute_state_delta(self, since_token, previous_state, current_state):
706720 state_delta [key ] = event
707721 return state_delta
708722
709- @defer .inlineCallbacks
710- def check_joined_room (self , sync_config , room_id , state_delta ):
723+ def check_joined_room (self , sync_config , state_delta ):
711724 """
712- Check if the user has just joined the given room. If so, return the
713- full state for the room, instead of the delta since the last sync.
725+ Check if the user has just joined the given room (so should
726+ be given the full state)
714727
715728 :param sync_config:
716- :param room_id:
717729 :param dict[(str,str), synapse.events.FrozenEvent] state_delta: the
718730 difference in state since the last sync
719731
720732 :returns A deferred Tuple (state_delta, limited)
721733 """
722- joined = False
723- limited = False
724-
725734 join_event = state_delta .get ((
726735 EventTypes .Member , sync_config .user .to_string ()), None )
727736 if join_event is not None :
728737 if join_event .content ["membership" ] == Membership .JOIN :
729- joined = True
730-
731- if joined :
732- state_delta = yield self .state_handler .get_current_state (room_id )
733- # the timeline is inherently limited if we've just joined
734- limited = True
735-
736- defer .returnValue ((state_delta , limited ))
738+ return True
739+ return False
0 commit comments