1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//! This modules abstracts data sources and merges them in a single virtual one
//!
//! It is also responsible for syncing them together

use std::error::Error;
use std::collections::HashSet;
use std::marker::PhantomData;
use std::sync::{Arc, Mutex};
use url::Url;

use crate::traits::{BaseCalendar, CalDavSource, DavCalendar};
use crate::traits::CompleteCalendar;
use crate::item::SyncStatus;

pub mod sync_progress;
use sync_progress::SyncProgress;
use sync_progress::{FeedbackSender, SyncEvent};

/// A data source that combines two `CalDavSource`s, which is able to sync both sources.
///
/// Usually, you will only need to use a provider between a server and a local cache, that is to say a [`CalDavProvider`](crate::CalDavProvider), i.e. a `Provider<Cache, CachedCalendar, Client, RemoteCalendar>`. \
/// However, providers can be used for integration tests, where the remote source is mocked by a `Cache`.
#[derive(Debug)]
pub struct Provider<L, T, R, U>
where
    L: CalDavSource<T>,
    T: CompleteCalendar + Sync + Send,
    R: CalDavSource<U>,
    U: DavCalendar + Sync + Send,
{
    /// The remote source (usually a server)
    remote: R,
    /// The local cache
    local: L,

    phantom_t: PhantomData<T>,
    phantom_u: PhantomData<U>,
}

impl<L, T, R, U> Provider<L, T, R, U>
where
    L: CalDavSource<T>,
    T: CompleteCalendar + Sync + Send,
    R: CalDavSource<U>,
    U: DavCalendar + Sync + Send,
{
    /// Create a provider.
    ///
    /// `remote` is usually a [`Client`](crate::client::Client), `local` is usually a [`Cache`](crate::cache::Cache).
    /// However, both can be interchangeable. The only difference is that `remote` always wins in case of a sync conflict
    pub fn new(remote: R, local: L) -> Self {
        Self { remote, local,
            phantom_t: PhantomData, phantom_u: PhantomData,
        }
    }

    /// Returns the data source described as `local`
    pub fn local(&self)  -> &L { &self.local }
    /// Returns the data source described as `local`
    pub fn local_mut(&mut self)  -> &mut L { &mut self.local }
    /// Returns the data source described as `remote`.
    ///
    /// Apart from tests, there are very few (if any) reasons to access `remote` directly.
    /// Usually, you should rather use the `local` source, which (usually) is a much faster local cache.
    /// To be sure `local` accurately mirrors the `remote` source, you can run [`Provider::sync`]
    pub fn remote(&self) -> &R { &self.remote }

    /// Performs a synchronisation between `local` and `remote`, and provide feeedback to the user about the progress.
    ///
    /// This bidirectional sync applies additions/deletions made on a source to the other source.
    /// In case of conflicts (the same item has been modified on both ends since the last sync, `remote` always wins).
    ///
    /// It returns whether the sync was totally successful (details about errors are logged using the `log::*` macros).
    /// In case errors happened, the sync might have been partially executed but your data will never be correupted (either locally nor in the server).
    /// Simply run this function again, it will re-start a sync, picking up where it failed.
    pub async fn sync_with_feedback(&mut self, feedback_sender: FeedbackSender) -> bool {
        let mut progress = SyncProgress::new_with_feedback_channel(feedback_sender);
        self.run_sync(&mut progress).await
    }

    /// Performs a synchronisation between `local` and `remote`, without giving any feedback.
    ///
    /// See [`Self::sync_with_feedback`]
    pub async fn sync(&mut self) -> bool {
        let mut progress = SyncProgress::new();
        self.run_sync(&mut progress).await
    }

    async fn run_sync(&mut self, progress: &mut SyncProgress) -> bool {
        if let Err(err) = self.run_sync_inner(progress).await {
            progress.error(&format!("Sync terminated because of an error: {}", err));
        }
        progress.feedback(SyncEvent::Finished{ success: progress.is_success() });
        progress.is_success()
    }

    async fn run_sync_inner(&mut self, progress: &mut SyncProgress) -> Result<(), Box<dyn Error>> {
        progress.info("Starting a sync.");
        progress.feedback(SyncEvent::Started);

        let mut handled_calendars = HashSet::new();

        // Sync every remote calendar
        let cals_remote = self.remote.get_calendars().await?;
        for (cal_url, cal_remote) in cals_remote {
            let counterpart = match self.get_or_insert_local_counterpart_calendar(&cal_url, cal_remote.clone()).await {
                Err(err) => {
                    progress.warn(&format!("Unable to get or insert local counterpart calendar for {} ({}). Skipping this time", cal_url, err));
                    continue;
                },
                Ok(arc) => arc,
            };

            if let Err(err) = Self::sync_calendar_pair(counterpart, cal_remote, progress).await {
                progress.warn(&format!("Unable to sync calendar {}: {}, skipping this time.", cal_url, err));
                continue;
            }
            handled_calendars.insert(cal_url);
        }

        // Sync every local calendar that would not be in the remote yet
        let cals_local = self.local.get_calendars().await?;
        for (cal_url, cal_local) in cals_local {
            if handled_calendars.contains(&cal_url) {
                continue;
            }

            let counterpart = match self.get_or_insert_remote_counterpart_calendar(&cal_url, cal_local.clone()).await {
                Err(err) => {
                    progress.warn(&format!("Unable to get or insert remote counterpart calendar for {} ({}). Skipping this time", cal_url, err));
                    continue;
                },
                Ok(arc) => arc,
            };

            if let Err(err) = Self::sync_calendar_pair(cal_local, counterpart, progress).await {
                progress.warn(&format!("Unable to sync calendar {}: {}, skipping this time.", cal_url, err));
                continue;
            }
        }

        progress.info("Sync ended");

        Ok(())
    }


    async fn get_or_insert_local_counterpart_calendar(&mut self, cal_url: &Url, needle: Arc<Mutex<U>>) -> Result<Arc<Mutex<T>>, Box<dyn Error>> {
        get_or_insert_counterpart_calendar("local", &mut self.local, cal_url, needle).await
    }
    async fn get_or_insert_remote_counterpart_calendar(&mut self, cal_url: &Url, needle: Arc<Mutex<T>>) -> Result<Arc<Mutex<U>>, Box<dyn Error>> {
        get_or_insert_counterpart_calendar("remote", &mut self.remote, cal_url, needle).await
    }


    async fn sync_calendar_pair(cal_local: Arc<Mutex<T>>, cal_remote: Arc<Mutex<U>>, progress: &mut SyncProgress) -> Result<(), Box<dyn Error>> {
        let mut cal_remote = cal_remote.lock().unwrap();
        let mut cal_local = cal_local.lock().unwrap();
        let cal_name = cal_local.name().to_string();

        progress.info(&format!("Syncing calendar {}", cal_name));
        progress.feedback(SyncEvent::InProgress{
            calendar: cal_name.clone(),
            details: "started".to_string()
        });

        // Step 1 - find the differences
        progress.debug("Finding the differences to sync...");
        let mut local_del = HashSet::new();
        let mut remote_del = HashSet::new();
        let mut local_changes = HashSet::new();
        let mut remote_changes = HashSet::new();
        let mut local_additions = HashSet::new();
        let mut remote_additions = HashSet::new();

        let remote_items = cal_remote.get_item_version_tags().await?;
        progress.feedback(SyncEvent::InProgress{
            calendar: cal_name.clone(),
            details: format!("{} remote items", remote_items.len()),
        });

        let mut local_items_to_handle = cal_local.get_item_urls().await?;
        for (url, remote_tag) in remote_items {
            progress.trace(&format!("***** Considering remote item {}...", url));
            match cal_local.get_item_by_url(&url).await {
                None => {
                    // This was created on the remote
                    progress.debug(&format!("*   {} is a remote addition", url));
                    remote_additions.insert(url);
                },
                Some(local_item) => {
                    if local_items_to_handle.remove(&url) == false {
                        progress.error(&format!("Inconsistent state: missing task {} from the local tasks", url));
                    }

                    match local_item.sync_status() {
                        SyncStatus::NotSynced => {
                            progress.error(&format!("URL reuse between remote and local sources ({}). Ignoring this item in the sync", url));
                            continue;
                        },
                        SyncStatus::Synced(local_tag) => {
                            if &remote_tag != local_tag {
                                // This has been modified on the remote
                                progress.debug(&format!("*   {} is a remote change", url));
                                remote_changes.insert(url);
                            }
                        },
                        SyncStatus::LocallyModified(local_tag) => {
                            if &remote_tag == local_tag {
                                // This has been changed locally
                                progress.debug(&format!("*   {} is a local change", url));
                                local_changes.insert(url);
                            } else {
                                progress.info(&format!("Conflict: task {} has been modified in both sources. Using the remote version.", url));
                                progress.debug(&format!("*   {} is considered a remote change", url));
                                remote_changes.insert(url);
                            }
                        },
                        SyncStatus::LocallyDeleted(local_tag) => {
                            if &remote_tag == local_tag {
                                // This has been locally deleted
                                progress.debug(&format!("*   {} is a local deletion", url));
                                local_del.insert(url);
                            } else {
                                progress.info(&format!("Conflict: task {} has been locally deleted and remotely modified. Reverting to the remote version.", url));
                                progress.debug(&format!("*   {} is a considered a remote change", url));
                                remote_changes.insert(url);
                            }
                        },
                    }
                }
            }
        }

        // Also iterate on the local tasks that are not on the remote
        for url in local_items_to_handle {
            progress.trace(&format!("##### Considering local item {}...", url));
            let local_item = match cal_local.get_item_by_url(&url).await {
                None => {
                    progress.error(&format!("Inconsistent state: missing task {} from the local tasks", url));
                    continue;
                },
                Some(item) => item,
            };

            match local_item.sync_status() {
                SyncStatus::Synced(_) => {
                    // This item has been removed from the remote
                    progress.debug(&format!("#   {} is a deletion from the server", url));
                    remote_del.insert(url);
                },
                SyncStatus::NotSynced => {
                    // This item has just been locally created
                    progress.debug(&format!("#   {} has been locally created", url));
                    local_additions.insert(url);
                },
                SyncStatus::LocallyDeleted(_) => {
                    // This item has been deleted from both sources
                    progress.debug(&format!("#   {} has been deleted from both sources", url));
                    remote_del.insert(url);
                },
                SyncStatus::LocallyModified(_) => {
                    progress.info(&format!("Conflict: item {} has been deleted from the server and locally modified. Deleting the local copy", url));
                    remote_del.insert(url);
                },
            }
        }


        // Step 2 - commit changes
        progress.trace("Committing changes...");
        for url_del in local_del {
            progress.debug(&format!("> Pushing local deletion {} to the server", url_del));
            progress.feedback(SyncEvent::InProgress{
                calendar: cal_name.clone(),
                details: Self::item_name(&cal_local, &url_del).await,
            });
            match cal_remote.delete_item(&url_del).await {
                Err(err) => {
                    progress.warn(&format!("Unable to delete remote item {}: {}", url_del, err));
                },
                Ok(()) => {
                    // Change the local copy from "marked to deletion" to "actually deleted"
                    if let Err(err) = cal_local.immediately_delete_item(&url_del).await {
                        progress.error(&format!("Unable to permanently delete local item {}: {}", url_del, err));
                    }
                },
            }
        }

        for url_del in remote_del {
            progress.debug(&format!("> Applying remote deletion {} locally", url_del));
            progress.feedback(SyncEvent::InProgress{
                calendar: cal_name.clone(),
                details: Self::item_name(&cal_local, &url_del).await,
            });
            if let Err(err) = cal_local.immediately_delete_item(&url_del).await {
                progress.warn(&format!("Unable to delete local item {}: {}", url_del, err));
            }
        }

        for url_add in remote_additions {
            progress.debug(&format!("> Applying remote addition {} locally", url_add));
            progress.feedback(SyncEvent::InProgress{
                calendar: cal_name.clone(),
                details: Self::item_name(&cal_local, &url_add).await,
            });
            match cal_remote.get_item_by_url(&url_add).await {
                Err(err) => {
                    progress.warn(&format!("Unable to get remote item {}: {}. Skipping it.", url_add, err));
                    continue;
                },
                Ok(item) => match item {
                    None => {
                        progress.error(&format!("Inconsistency: new item {} has vanished from the remote end", url_add));
                        continue;
                    },
                    Some(new_item) => {
                        if let Err(err) = cal_local.add_item(new_item.clone()).await {
                            progress.error(&format!("Not able to add item {} to local calendar: {}", url_add, err));
                        }
                    },
                },
            }
        }

        for url_change in remote_changes {
            progress.debug(&format!("> Applying remote change {} locally", url_change));
            progress.feedback(SyncEvent::InProgress{
                calendar: cal_name.clone(),
                details: Self::item_name(&cal_local, &url_change).await,
            });
            match cal_remote.get_item_by_url(&url_change).await {
                Err(err) => {
                    progress.warn(&format!("Unable to get remote item {}: {}. Skipping it", url_change, err));
                    continue;
                },
                Ok(item) => match item {
                    None => {
                        progress.error(&format!("Inconsistency: modified item {} has vanished from the remote end", url_change));
                        continue;
                    },
                    Some(item) => {
                        if let Err(err) = cal_local.update_item(item.clone()).await {
                            progress.error(&format!("Unable to update item {} in local calendar: {}", url_change, err));
                        }
                    },
                }
            }
        }


        for url_add in local_additions {
            progress.debug(&format!("> Pushing local addition {} to the server", url_add));
            progress.feedback(SyncEvent::InProgress{
                calendar: cal_name.clone(),
                details: Self::item_name(&cal_local, &url_add).await,
            });
            match cal_local.get_item_by_url_mut(&url_add).await {
                None => {
                    progress.error(&format!("Inconsistency: created item {} has been marked for upload but is locally missing", url_add));
                    continue;
                },
                Some(item) => {
                    match cal_remote.add_item(item.clone()).await {
                        Err(err) => progress.error(&format!("Unable to add item {} to remote calendar: {}", url_add, err)),
                        Ok(new_ss) => {
                            // Update local sync status
                            item.set_sync_status(new_ss);
                        },
                    }
                },
            };
        }

        for url_change in local_changes {
            progress.debug(&format!("> Pushing local change {} to the server", url_change));
            progress.feedback(SyncEvent::InProgress{
                calendar: cal_name.clone(),
                details: Self::item_name(&cal_local, &url_change).await,
            });
            match cal_local.get_item_by_url_mut(&url_change).await {
                None => {
                    progress.error(&format!("Inconsistency: modified item {} has been marked for upload but is locally missing", url_change));
                    continue;
                },
                Some(item) => {
                    match cal_remote.update_item(item.clone()).await {
                        Err(err) => progress.error(&format!("Unable to update item {} in remote calendar: {}", url_change, err)),
                        Ok(new_ss) => {
                            // Update local sync status
                            item.set_sync_status(new_ss);
                        },
                    };
                }
            };
        }

        Ok(())
    }


    async fn item_name(cal: &T, url: &Url) -> String {
        cal.get_item_by_url(url).await.map(|item| item.name()).unwrap_or_default().to_string()
    }

}


async fn get_or_insert_counterpart_calendar<H, N, I>(haystack_descr: &str, haystack: &mut H, cal_url: &Url, needle: Arc<Mutex<N>>)
    -> Result<Arc<Mutex<I>>, Box<dyn Error>>
where
    H: CalDavSource<I>,
    I: BaseCalendar,
    N: BaseCalendar,
{
    loop {
        if let Some(cal) = haystack.get_calendar(&cal_url).await {
            break Ok(cal);
        }

        // This calendar does not exist locally yet, let's add it
        log::debug!("Adding a {} calendar {}", haystack_descr, cal_url);
        let src = needle.lock().unwrap();
        let name = src.name().to_string();
        let supported_comps = src.supported_components();
        let color = src.color();
        if let Err(err) = haystack.create_calendar(
            cal_url.clone(),
            name,
            supported_comps,
            color.cloned(),
        ).await{
            return Err(err);
        }
    }
}