@@ -1058,40 +1058,74 @@ def _generate_sync_entry_for_device_list(
10581058 newly_left_rooms ,
10591059 newly_left_users ,
10601060 ):
1061+ """Generate the DeviceLists section of sync
1062+
1063+ Args:
1064+ sync_result_builder (SyncResultBuilder)
1065+ newly_joined_rooms (set[str]): Set of rooms user has joined since
1066+ previous sync
1067+ newly_joined_or_invited_users (set[str]): Set of users that have
1068+ joined or been invited to a room since previous sync.
1069+ newly_left_rooms (set[str]): Set of rooms user has left since
1070+ previous sync
1071+ newly_left_users (set[str]): Set of users that have left a room
1072+ we're in since previous sync
1073+
1074+ Returns:
1075+ Deferred[DeviceLists]
1076+ """
1077+
10611078 user_id = sync_result_builder .sync_config .user .to_string ()
10621079 since_token = sync_result_builder .since_token
10631080
1081+ # We're going to mutate these fields, so lets copy them rather than
1082+ # assume they won't get used later.
1083+ newly_joined_or_invited_users = set (newly_joined_or_invited_users )
1084+ newly_left_users = set (newly_left_users )
1085+
10641086 if since_token and since_token .device_list_key :
1065- changed = yield self .store .get_user_whose_devices_changed (
1066- since_token .device_list_key
1087+ # We want to figure out what user IDs the client should refetch
1088+ # device keys for, and which users we aren't going to track changes
1089+ # for anymore.
1090+ #
1091+ # For the first step we check:
1092+ # a. if any users we share a room with have updated their devices,
1093+ # and
1094+ # b. we also check if we've joined any new rooms, or if a user has
1095+ # joined a room we're in.
1096+ #
1097+ # For the second step we just find any users we no longer share a
1098+ # room with by looking at all users that have left a room plus users
1099+ # that were in a room we've left.
1100+
1101+ users_who_share_room = yield self .store .get_users_who_share_room_with_user (
1102+ user_id
1103+ )
1104+
1105+ # Step 1a, check for changes in devices of users we share a room with
1106+ users_that_have_changed = yield self .store .get_users_whose_devices_changed (
1107+ since_token .device_list_key , users_who_share_room
10671108 )
10681109
1069- # TODO: Be more clever than this, i.e. remove users who we already
1070- # share a room with?
1110+ # Step 1b, check for newly joined rooms
10711111 for room_id in newly_joined_rooms :
10721112 joined_users = yield self .state .get_current_users_in_room (room_id )
10731113 newly_joined_or_invited_users .update (joined_users )
10741114
1075- for room_id in newly_left_rooms :
1076- left_users = yield self .state .get_current_users_in_room (room_id )
1077- newly_left_users .update (left_users )
1078-
10791115 # TODO: Check that these users are actually new, i.e. either they
10801116 # weren't in the previous sync *or* they left and rejoined.
1081- changed .update (newly_joined_or_invited_users )
1117+ users_that_have_changed .update (newly_joined_or_invited_users )
10821118
1083- if not changed and not newly_left_users :
1084- defer .returnValue (DeviceLists (changed = [], left = newly_left_users ))
1119+ # Now find users that we no longer track
1120+ for room_id in newly_left_rooms :
1121+ left_users = yield self .state .get_current_users_in_room (room_id )
1122+ newly_left_users .update (left_users )
10851123
1086- users_who_share_room = yield self .store .get_users_who_share_room_with_user (
1087- user_id
1088- )
1124+ # Remove any users that we still share a room with.
1125+ newly_left_users -= users_who_share_room
10891126
10901127 defer .returnValue (
1091- DeviceLists (
1092- changed = users_who_share_room & changed ,
1093- left = set (newly_left_users ) - users_who_share_room ,
1094- )
1128+ DeviceLists (changed = users_that_have_changed , left = newly_left_users )
10951129 )
10961130 else :
10971131 defer .returnValue (DeviceLists (changed = [], left = []))
0 commit comments