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
//! This module provides a local cache for CalDAV data

use std::path::PathBuf;
use std::path::Path;
use std::error::Error;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::ffi::OsStr;

use serde::{Deserialize, Serialize};
use async_trait::async_trait;
use csscolorparser::Color;
use url::Url;

use crate::traits::CalDavSource;
use crate::traits::BaseCalendar;
use crate::traits::CompleteCalendar;
use crate::calendar::cached_calendar::CachedCalendar;
use crate::calendar::SupportedComponents;

#[cfg(feature = "local_calendar_mocks_remote_calendars")]
use crate::mock_behaviour::MockBehaviour;

const MAIN_FILE: &str = "data.json";

/// A CalDAV source that stores its items in a local folder.
///
/// It automatically updates the content of the folder when dropped (see its `Drop` implementation), but you can also manually call [`Cache::save_to_folder`]
///
/// Most of its methods are part of the `CalDavSource` trait implementation
#[derive(Debug)]
pub struct Cache {
    backing_folder: PathBuf,
    data: CachedData,

    /// In tests, we may add forced errors to this object
    #[cfg(feature = "local_calendar_mocks_remote_calendars")]
    mock_behaviour: Option<Arc<Mutex<MockBehaviour>>>,
}

#[derive(Default, Debug, Serialize, Deserialize)]
struct CachedData {
    #[serde(skip)]
    calendars: HashMap<Url, Arc<Mutex<CachedCalendar>>>,
}

impl Cache {
    /// Activate the "mocking remote source" features (i.e. tell its children calendars that they are mocked remote calendars)
    #[cfg(feature = "local_calendar_mocks_remote_calendars")]
    pub fn set_mock_behaviour(&mut self, mock_behaviour: Option<Arc<Mutex<MockBehaviour>>>) {
        self.mock_behaviour = mock_behaviour;
    }


    /// Get the path to the cache folder
    pub fn cache_folder() -> PathBuf {
        return PathBuf::from(String::from("~/.config/my-tasks/cache/"))
    }

    /// Initialize a cache from the content of a valid backing folder if it exists.
    /// Returns an error otherwise
    pub fn from_folder(folder: &Path) -> Result<Self, Box<dyn Error>> {
        // Load shared data...
        let main_file = folder.join(MAIN_FILE);
        let mut data: CachedData = match std::fs::File::open(&main_file) {
            Err(err) => {
                return Err(format!("Unable to open file {:?}: {}", main_file, err).into());
            },
            Ok(file) => serde_json::from_reader(file)?,
        };

        // ...and every calendar
        for entry in std::fs::read_dir(folder)? {
            match entry {
                Err(err) => {
                    log::error!("Unable to read dir: {:?}", err);
                    continue;
                },
                Ok(entry) => {
                    let cal_path = entry.path();
                    log::debug!("Considering {:?}", cal_path);
                    if cal_path.extension() == Some(OsStr::new("cal")) {
                        match Self::load_calendar(&cal_path) {
                            Err(err) => {
                                log::error!("Unable to load calendar {:?} from cache: {:?}", cal_path, err);
                                continue;
                            },
                            Ok(cal) =>
                                data.calendars.insert(cal.url().clone(), Arc::new(Mutex::new(cal))),
                        };
                    }
                },
            }
        }

        Ok(Self{
            backing_folder: PathBuf::from(folder),
            data,

            #[cfg(feature = "local_calendar_mocks_remote_calendars")]
            mock_behaviour: None,
        })
    }

    fn load_calendar(path: &Path) -> Result<CachedCalendar, Box<dyn Error>> {
        let file = std::fs::File::open(&path)?;
        Ok(serde_json::from_reader(file)?)
    }

    /// Initialize a cache with the default contents
    pub fn new(folder_path: &Path) -> Self {
        Self{
            backing_folder: PathBuf::from(folder_path),
            data: CachedData::default(),

            #[cfg(feature = "local_calendar_mocks_remote_calendars")]
            mock_behaviour: None,
        }
    }

    /// Store the current Cache to its backing folder
    ///
    /// Note that this is automatically called when `self` is `drop`ped
    pub fn save_to_folder(&self) -> Result<(), std::io::Error> {
        let folder = &self.backing_folder;
        std::fs::create_dir_all(folder)?;

        // Save the general data
        let main_file_path = folder.join(MAIN_FILE);
        let file = std::fs::File::create(&main_file_path)?;
        serde_json::to_writer(file, &self.data)?;

        // Save each calendar
        for (cal_url, cal_mutex) in &self.data.calendars {
            let file_name = sanitize_filename::sanitize(cal_url.as_str()) + ".cal";
            let cal_file = folder.join(file_name);
            let file = std::fs::File::create(&cal_file)?;
            let cal = cal_mutex.lock().unwrap();
            serde_json::to_writer(file, &*cal)?;
        }

        Ok(())
    }


    /// Compares two Caches to check they have the same current content
    ///
    /// This is not a complete equality test: some attributes (sync status...) may differ. This should mostly be used in tests
    #[cfg(any(test, feature = "integration_tests"))]
    pub async fn has_same_observable_content_as(&self, other: &Self) -> Result<bool, Box<dyn Error>> {
        let calendars_l = self.get_calendars().await?;
        let calendars_r = other.get_calendars().await?;

        if crate::utils::keys_are_the_same(&calendars_l, &calendars_r) == false {
            log::debug!("Different keys for calendars");
            return Ok(false);
        }

        for (calendar_url, cal_l) in calendars_l {
            log::debug!("Comparing calendars {}", calendar_url);
            let cal_l = cal_l.lock().unwrap();
            let cal_r = match calendars_r.get(&calendar_url) {
                Some(c) => c.lock().unwrap(),
                None => return Err("should not happen, we've just tested keys are the same".into()),
            };

            // TODO: check calendars have the same names/ID/whatever
            if cal_l.has_same_observable_content_as(&cal_r).await? == false {
                log::debug!("Different calendars");
                return Ok(false)
            }

        }
        Ok(true)
    }
}

impl Drop for Cache {
    fn drop(&mut self) {
        if let Err(err) = self.save_to_folder() {
            log::error!("Unable to automatically save the cache when it's no longer required: {}", err);
        }
    }
}

impl Cache {
    /// The non-async version of [`crate::traits::CalDavSource::get_calendars`]
    pub fn get_calendars_sync(&self) -> Result<HashMap<Url, Arc<Mutex<CachedCalendar>>>, Box<dyn Error>> {
        #[cfg(feature = "local_calendar_mocks_remote_calendars")]
        self.mock_behaviour.as_ref().map_or(Ok(()), |b| b.lock().unwrap().can_get_calendars())?;

        Ok(self.data.calendars.iter()
            .map(|(url, cal)| (url.clone(), cal.clone()))
            .collect()
        )
    }

    /// The non-async version of [`crate::traits::CalDavSource::get_calendar`]
    pub fn get_calendar_sync(&self, url: &Url) -> Option<Arc<Mutex<CachedCalendar>>> {
        self.data.calendars.get(url).map(|arc| arc.clone())
    }
}

#[async_trait]
impl CalDavSource<CachedCalendar> for Cache {
    async fn get_calendars(&self) -> Result<HashMap<Url, Arc<Mutex<CachedCalendar>>>, Box<dyn Error>> {
        self.get_calendars_sync()
    }

    async fn get_calendar(&self, url: &Url) -> Option<Arc<Mutex<CachedCalendar>>> {
        self.get_calendar_sync(url)
    }

    async fn create_calendar(&mut self, url: Url, name: String, supported_components: SupportedComponents, color: Option<Color>) -> Result<Arc<Mutex<CachedCalendar>>, Box<dyn Error>> {
        log::debug!("Inserting local calendar {}", url);
        #[cfg(feature = "local_calendar_mocks_remote_calendars")]
        self.mock_behaviour.as_ref().map_or(Ok(()), |b| b.lock().unwrap().can_create_calendar())?;

        let new_calendar = CachedCalendar::new(name, url.clone(), supported_components, color);
        let arc = Arc::new(Mutex::new(new_calendar));

        #[cfg(feature = "local_calendar_mocks_remote_calendars")]
        if let Some(behaviour) = &self.mock_behaviour {
            arc.lock().unwrap().set_mock_behaviour(Some(Arc::clone(behaviour)));
        };

        match self.data.calendars.insert(url, arc.clone()) {
            Some(_) => Err("Attempt to insert calendar failed: there is alredy such a calendar.".into()),
            None => Ok(arc),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use url::Url;
    use crate::calendar::SupportedComponents;
    use crate::item::Item;
    use crate::task::Task;

    async fn populate_cache(cache_path: &Path) -> Cache {
        let mut cache = Cache::new(&cache_path);

        let _shopping_list = cache.create_calendar(
            Url::parse("https://caldav.com/shopping").unwrap(),
            "My shopping list".to_string(),
            SupportedComponents::TODO,
            Some(csscolorparser::parse("lime").unwrap()),
        ).await.unwrap();

        let bucket_list = cache.create_calendar(
            Url::parse("https://caldav.com/bucket-list").unwrap(),
            "My bucket list".to_string(),
            SupportedComponents::TODO,
            Some(csscolorparser::parse("#ff8000").unwrap()),
        ).await.unwrap();

        {
            let mut bucket_list = bucket_list.lock().unwrap();
            let cal_url = bucket_list.url().clone();
            bucket_list.add_item(Item::Task(Task::new(
                String::from("Attend a concert of JS Bach"), false, &cal_url
            ))).await.unwrap();

            bucket_list.add_item(Item::Task(Task::new(
                String::from("Climb the Lighthouse of Alexandria"), true, &cal_url
            ))).await.unwrap();
        }

        cache
    }

    #[tokio::test]
    async fn cache_serde() {
        let _ = env_logger::builder().is_test(true).try_init();
        let cache_path = PathBuf::from(String::from("test_cache/serde_test"));
        let cache = populate_cache(&cache_path).await;

        cache.save_to_folder().unwrap();

        let retrieved_cache = Cache::from_folder(&cache_path).unwrap();
        assert_eq!(cache.backing_folder, retrieved_cache.backing_folder);
        let test = cache.has_same_observable_content_as(&retrieved_cache).await;
        println!("Equal? {:?}", test);
        assert_eq!(test.unwrap(), true);
    }

    #[tokio::test]
    async fn cache_sanity_checks() {
        let _ = env_logger::builder().is_test(true).try_init();
        let cache_path = PathBuf::from(String::from("test_cache/sanity_tests"));
        let mut cache = populate_cache(&cache_path).await;

        // We should not be able to add a second calendar with the same URL
        let second_addition_same_calendar = cache.create_calendar(
            Url::parse("https://caldav.com/shopping").unwrap(),
            "My shopping list".to_string(),
            SupportedComponents::TODO,
            None,
        ).await;
        assert!(second_addition_same_calendar.is_err());
    }
}