From a77aa6b65ac5402a637116dbdde03a84148aea6d Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Fri, 22 Jan 2021 10:00:25 +1000 Subject: [PATCH] handle non-chunked graves from AnkiDroid --- rslib/src/backend/http_sync_server.rs | 6 +++++- rslib/src/sync/http.rs | 3 +++ rslib/src/sync/http_client.rs | 12 +++++++++--- rslib/src/sync/mod.rs | 2 +- rslib/src/sync/server.rs | 21 ++++++++++++++++++--- 5 files changed, 36 insertions(+), 8 deletions(-) diff --git a/rslib/src/backend/http_sync_server.rs b/rslib/src/backend/http_sync_server.rs index 49b6c57e5..971e21ddd 100644 --- a/rslib/src/backend/http_sync_server.rs +++ b/rslib/src/backend/http_sync_server.rs @@ -99,7 +99,11 @@ impl Backend { self.with_sync_server(|server| { let mut rt = Runtime::new().unwrap(); - rt.block_on(server.start(input.client_usn, input.local_is_newer)) + rt.block_on(server.start( + input.client_usn, + input.local_is_newer, + input.deprecated_client_graves, + )) }) } diff --git a/rslib/src/sync/http.rs b/rslib/src/sync/http.rs index b3f04d7ba..7259d5afb 100644 --- a/rslib/src/sync/http.rs +++ b/rslib/src/sync/http.rs @@ -98,6 +98,9 @@ pub struct StartIn { pub client_usn: Usn, #[serde(rename = "lnewer")] pub local_is_newer: bool, + /// Unfortunately AnkiDroid is still using this + #[serde(rename = "graves", default)] + pub deprecated_client_graves: Option, } #[derive(Serialize, Deserialize, Debug)] diff --git a/rslib/src/sync/http_client.rs b/rslib/src/sync/http_client.rs index 50550bc0d..5b5faf3a7 100644 --- a/rslib/src/sync/http_client.rs +++ b/rslib/src/sync/http_client.rs @@ -71,10 +71,16 @@ impl SyncServer for HTTPSyncClient { self.json_request(input).await } - async fn start(&mut self, client_usn: Usn, local_is_newer: bool) -> Result { + async fn start( + &mut self, + client_usn: Usn, + local_is_newer: bool, + deprecated_client_graves: Option, + ) -> Result { let input = SyncRequest::Start(StartIn { client_usn, local_is_newer, + deprecated_client_graves, }); self.json_request(input).await } @@ -362,13 +368,13 @@ mod test { }) )); - let _graves = syncer.start(Usn(1), true).await?; + let _graves = syncer.start(Usn(1), true, None).await?; // aborting should now work syncer.abort().await?; // start again, and continue - let _graves = syncer.start(Usn(1), true).await?; + let _graves = syncer.start(Usn(1), true, None).await?; syncer.apply_graves(Graves::default()).await?; diff --git a/rslib/src/sync/mod.rs b/rslib/src/sync/mod.rs index 0fab5b323..7bef2c751 100644 --- a/rslib/src/sync/mod.rs +++ b/rslib/src/sync/mod.rs @@ -407,7 +407,7 @@ where async fn start_and_process_deletions(&mut self, state: &SyncState) -> Result<()> { let remote: Graves = self .remote - .start(state.usn_at_last_sync, state.local_is_newer) + .start(state.usn_at_last_sync, state.local_is_newer, None) .await?; debug!(self.col.log, "removed on remote"; diff --git a/rslib/src/sync/server.rs b/rslib/src/sync/server.rs index 8082c87e9..6385b8a6b 100644 --- a/rslib/src/sync/server.rs +++ b/rslib/src/sync/server.rs @@ -18,7 +18,12 @@ use super::ChunkableIDs; #[async_trait(?Send)] pub trait SyncServer { async fn meta(&self) -> Result; - async fn start(&mut self, client_usn: Usn, local_is_newer: bool) -> Result; + async fn start( + &mut self, + client_usn: Usn, + local_is_newer: bool, + deprecated_client_graves: Option, + ) -> Result; async fn apply_graves(&mut self, client_chunk: Graves) -> Result<()>; async fn apply_changes(&mut self, client_changes: UnchunkedChanges) -> Result; @@ -84,13 +89,23 @@ impl SyncServer for LocalServer { }) } - async fn start(&mut self, client_usn: Usn, client_is_newer: bool) -> Result { + async fn start( + &mut self, + client_usn: Usn, + client_is_newer: bool, + deprecated_client_graves: Option, + ) -> Result { self.server_usn = self.col.usn()?; self.client_usn = client_usn; self.client_is_newer = client_is_newer; self.col.storage.begin_rust_trx()?; - self.col.storage.pending_graves(client_usn) + let server_graves = self.col.storage.pending_graves(client_usn)?; + // Handle AnkiDroid using old protocol + if let Some(graves) = deprecated_client_graves { + self.col.apply_graves(graves, self.server_usn)?; + } + Ok(server_graves) } async fn apply_graves(&mut self, client_chunk: Graves) -> Result<()> {