Removed sync implementation

This commit is contained in:
daladim 2021-03-22 22:37:29 +01:00
parent 86f3566532
commit 647199af93

View file

@ -57,7 +57,7 @@ where
/// In case of conflicts (the same item has been modified on both ends since the last sync, `server` always wins) /// In case of conflicts (the same item has been modified on both ends since the last sync, `server` always wins)
pub async fn sync(&mut self) -> Result<(), Box<dyn Error>> { pub async fn sync(&mut self) -> Result<(), Box<dyn Error>> {
log::info!("Starting a sync."); log::info!("Starting a sync.");
/*
let cals_server = self.server.get_calendars().await?; let cals_server = self.server.get_calendars().await?;
for (id, cal_server) in cals_server { for (id, cal_server) in cals_server {
let mut cal_server = cal_server.lock().unwrap(); let mut cal_server = cal_server.lock().unwrap();
@ -71,68 +71,16 @@ where
}; };
let mut cal_local = cal_local.lock().unwrap(); let mut cal_local = cal_local.lock().unwrap();
// Step 1 - "Server always wins", so a deletion from the server must be applied locally, even if it was locally modified. // Step 1 - find the differences
let mut local_dels = match cal_local.get_last_sync() { // let mut local_del = HashSet::new();
None => HashSet::new(), // let mut remote_del = HashSet::new();
Some(date) => cal_local.get_items_deleted_since(date).await?, // let mut local_changes = HashSet::new();
}; // let mut remote_change = HashSet::new();
if last_sync.is_some() { // let mut local_additions = HashSet::new();
let server_deletions = cal_server.find_deletions_from(cal_local.get_item_ids().await?).await?; // let mut remote_additions = HashSet::new();
for server_del_id in server_deletions {
// Even in case of conflicts, "the server always wins", so it is safe to remove tasks from the local cache as soon as now
if let Err(err) = cal_local.delete_item(&server_del_id).await {
log::error!("Unable to remove local item {}: {}", server_del_id, err);
}
if local_dels.contains(&server_del_id) {
local_dels.remove(&server_del_id);
}
}
}
// Step 2 - Compare both changesets...
let server_mods = cal_server.get_items_modified_since(last_sync, None).await?;
let mut local_mods = cal_local.get_items_modified_since(last_sync, None).await?;
// ...import remote changes,...
let mut conflicting_tasks = Vec::new();
let mut tasks_to_add = Vec::new();
for (server_mod_id, server_mod) in server_mods {
if local_mods.contains_key(&server_mod_id) {
log::warn!("Conflict for task {} (modified in both sources). Using the server version", server_mod_id);
conflicting_tasks.push(server_mod_id.clone());
local_mods.remove(&server_mod_id);
}
if local_dels.contains(&server_mod_id) {
log::warn!("Conflict for task {} (modified in the server, deleted locally). Reverting to the server version", server_mod_id);
local_dels.remove(&server_mod_id);
}
tasks_to_add.push(server_mod.clone());
}
// ...upload local deletions,...
for local_del_id in local_dels {
if let Err(err) = cal_server.delete_item(&local_del_id).await {
log::error!("Unable to remove remote item {}: {}", local_del_id, err);
}
}
// ...and upload local changes
for (local_mod_id, local_mod) in local_mods {
// Conflicts are no longer in local_mods
if let Err(err) = cal_server.delete_item(&local_mod_id).await {
log::error!("Unable to remove remote item (before an update) {}: {}", local_mod_id, err);
}
// TODO: should I add a .update_item()?
cal_server.add_item(local_mod.clone()).await;
}
remove_from_calendar(&conflicting_tasks, &mut (*cal_local)).await;
move_to_calendar(&mut tasks_to_add, &mut (*cal_local)).await;
} }
self.local.update_last_sync(None);
*/
Ok(()) Ok(())
} }
} }