@@ -285,7 +285,11 @@ def __bool__(self) -> bool:
285285 )
286286
287287 @staticmethod
288- def empty (next_batch : StreamToken ) -> "SyncResult" :
288+ def empty (
289+ next_batch : StreamToken ,
290+ device_one_time_keys_count : JsonMapping ,
291+ device_unused_fallback_key_types : List [str ],
292+ ) -> "SyncResult" :
289293 "Return a new empty result"
290294 return SyncResult (
291295 next_batch = next_batch ,
@@ -297,8 +301,8 @@ def empty(next_batch: StreamToken) -> "SyncResult":
297301 archived = [],
298302 to_device = [],
299303 device_lists = DeviceListUpdates (),
300- device_one_time_keys_count = {} ,
301- device_unused_fallback_key_types = [] ,
304+ device_one_time_keys_count = device_one_time_keys_count ,
305+ device_unused_fallback_key_types = device_unused_fallback_key_types ,
302306 )
303307
304308
@@ -523,7 +527,28 @@ async def _wait_for_sync_for_user(
523527 logger .warning (
524528 "Timed out waiting for worker to catch up. Returning empty response"
525529 )
526- return SyncResult .empty (since_token )
530+ device_id = sync_config .device_id
531+ one_time_keys_count : JsonMapping = {}
532+ unused_fallback_key_types : List [str ] = []
533+ if device_id :
534+ user_id = sync_config .user .to_string ()
535+ # TODO: We should have a way to let clients differentiate between the states of:
536+ # * no change in OTK count since the provided since token
537+ # * the server has zero OTKs left for this device
538+ # Spec issue: https://github.com/matrix-org/matrix-doc/issues/3298
539+ one_time_keys_count = await self .store .count_e2e_one_time_keys (
540+ user_id , device_id
541+ )
542+ unused_fallback_key_types = list (
543+ await self .store .get_e2e_unused_fallback_key_types (
544+ user_id , device_id
545+ )
546+ )
547+
548+ cache_context .should_cache = False # Don't cache empty responses
549+ return SyncResult .empty (
550+ since_token , one_time_keys_count , unused_fallback_key_types
551+ )
527552
528553 # If we've spent significant time waiting to catch up, take it off
529554 # the timeout.
0 commit comments