From 992abfab8f5af62715fd2aae8ebfb5d596750f2e Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Fri, 25 Jul 2025 18:17:00 +0000 Subject: [PATCH] Separate cached body from request body in snake-sync; cleanup. Signed-off-by: Jason Volk --- src/api/client/sync/v5.rs | 42 ++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/src/api/client/sync/v5.rs b/src/api/client/sync/v5.rs index 20fad035..fb3a97cd 100644 --- a/src/api/client/sync/v5.rs +++ b/src/api/client/sync/v5.rs @@ -61,31 +61,16 @@ pub(crate) async fn sync_events_v5_route( body: Ruma, ) -> Result { debug_assert!(DEFAULT_BUMP_TYPES.is_sorted(), "DEFAULT_BUMP_TYPES is not sorted"); - let sender_user = body - .sender_user - .as_ref() - .expect("user is authenticated"); - let sender_device = body - .sender_device - .as_ref() - .expect("user is authenticated"); - let mut body = body.body; - - // Setup watchers, so if there's no response, we can wait for them - let watcher = services.sync.watch(sender_user, sender_device); - - let next_batch = services.globals.next_count(); - - let conn_id = body.conn_id.clone(); + let sender_user = body.sender_user(); + let sender_device = body.sender_device(); + let snake_key = into_snake_key(sender_user, sender_device, body.conn_id.clone()); let globalsince = body .pos .as_ref() .and_then(|string| string.parse().ok()) .unwrap_or(0); - let snake_key = into_snake_key(sender_user, sender_device, conn_id); - if globalsince != 0 && !services.sync.snake_connection_cached(&snake_key) { return Err!(Request(UnknownPos( "Connection data unknown to server; restarting sync stream." @@ -99,10 +84,16 @@ pub(crate) async fn sync_events_v5_route( .forget_snake_sync_connection(&snake_key); } + // Setup watchers, so if there's no response, we can wait for them + let watcher = services.sync.watch(sender_user, sender_device); + + let next_batch = services.globals.next_count(); + // Get sticky parameters from cache + let mut cached = body.body.clone(); let known_rooms = services .sync - .update_snake_sync_request_with_cache(&snake_key, &mut body); + .update_snake_sync_request_with_cache(&snake_key, &mut cached); let all_joined_rooms = services .rooms @@ -140,13 +131,13 @@ pub(crate) async fn sync_events_v5_route( let mut todo_rooms: TodoRooms = BTreeMap::new(); - let sync_info: SyncInfo<'_> = (sender_user, sender_device, globalsince, &body); + let sync_info: SyncInfo<'_> = (sender_user, sender_device, globalsince, &cached); let account_data = collect_account_data(services, sync_info).map(Ok); let e2ee = collect_e2ee(services, sync_info, all_joined_rooms.clone()); - let to_device = collect_to_device(services, sync_info, *next_batch).map(Ok); + let to_device = collect_to_device(services, sync_info, next_batch).map(Ok); let receipts = collect_receipts(services).map(Ok); @@ -162,7 +153,7 @@ pub(crate) async fn sync_events_v5_route( }; let mut response = sync_events::v5::Response { - txn_id: body.txn_id.clone(), + txn_id: cached.txn_id.clone(), pos, lists: BTreeMap::new(), rooms: BTreeMap::new(), @@ -182,18 +173,18 @@ pub(crate) async fn sync_events_v5_route( .await; response.extensions.typing = - collect_typing_events(services, sender_user, &body, all_joined_rooms.clone()).await?; + collect_typing_events(services, sender_user, &cached, all_joined_rooms.clone()).await?; fetch_subscriptions(services, sync_info, &known_rooms, &mut todo_rooms).await; response.rooms = process_rooms( services, sender_user, - *next_batch, + next_batch, all_invited_rooms.clone(), &todo_rooms, &mut response, - &body, + &cached, ) .await?; @@ -703,6 +694,7 @@ where } Ok(rooms) } + async fn collect_account_data( services: &Services, (sender_user, _, globalsince, body): (&UserId, &DeviceId, u64, &sync_events::v5::Request),