From 89d2c3fcef81441e12dae62293b1398aa9002442 Mon Sep 17 00:00:00 2001 From: daladim Date: Sun, 28 Feb 2021 00:18:26 +0100 Subject: [PATCH] Sync: server wins for conflicts --- src/provider.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/provider.rs b/src/provider.rs index e2663b0..935bb27 100644 --- a/src/provider.rs +++ b/src/provider.rs @@ -7,6 +7,7 @@ use chrono::{DateTime, Utc}; use crate::traits::CalDavSource; use crate::Calendar; use crate::Task; +use crate::task::TaskId; pub struct Provider @@ -61,7 +62,12 @@ where let local_mod = cal_local.get_tasks_modified_since(Some(self.last_sync), None); let mut tasks_to_add_to_local = Vec::new(); + let mut tasks_id_to_remove_from_local = Vec::new(); for (new_id, new_item) in &server_mod { + if server_mod.contains_key(new_id) { + log::warn!("Conflict for task {} ({}). Using the server version.", new_item.name(), new_id); + tasks_id_to_remove_from_local.push(new_id.clone()); + } tasks_to_add_to_local.push((*new_item).clone()); } @@ -74,6 +80,7 @@ where tasks_to_add_to_server.push((*new_item).clone()); } + remove_from_calendar(&tasks_id_to_remove_from_local, cal_local); move_to_calendar(&mut tasks_to_add_to_local, cal_local); move_to_calendar(&mut tasks_to_add_to_server, cal_server); } @@ -99,3 +106,9 @@ fn move_to_calendar(tasks: &mut Vec, calendar: &mut Calendar) { } } +fn remove_from_calendar(ids: &Vec, calendar: &mut Calendar) { + for id in ids { + log::info!(" Removing {:?} from local calendar", id); + calendar.delete_task(id); + } +}