diff --git a/src/provider.rs b/src/provider.rs index d56e63c..2b5c9b6 100644 --- a/src/provider.rs +++ b/src/provider.rs @@ -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) pub async fn sync(&mut self) -> Result<(), Box> { log::info!("Starting a sync."); -/* + let cals_server = self.server.get_calendars().await?; for (id, cal_server) in cals_server { let mut cal_server = cal_server.lock().unwrap(); @@ -71,68 +71,16 @@ where }; 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. - let mut local_dels = match cal_local.get_last_sync() { - None => HashSet::new(), - Some(date) => cal_local.get_items_deleted_since(date).await?, - }; - if last_sync.is_some() { - let server_deletions = cal_server.find_deletions_from(cal_local.get_item_ids().await?).await?; - 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); - } + // Step 1 - find the differences + // let mut local_del = HashSet::new(); + // let mut remote_del = HashSet::new(); + // let mut local_changes = HashSet::new(); + // let mut remote_change = HashSet::new(); + // let mut local_additions = HashSet::new(); + // let mut remote_additions = HashSet::new(); - 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(()) } }