From 71ec8fe3238e025b1fda617d9007ea476efe74ab Mon Sep 17 00:00:00 2001 From: daladim Date: Mon, 19 Apr 2021 23:23:39 +0200 Subject: [PATCH] Sanity checks --- src/client.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/client.rs b/src/client.rs index c03fb18..b298fc8 100644 --- a/src/client.rs +++ b/src/client.rs @@ -226,6 +226,11 @@ impl CalDavSource for Client { } async fn get_calendar(&self, id: &CalendarId) -> Option>> { + if let Err(err) = self.populate_calendars().await { + log::warn!("Unable to fetch calendars: {}", err); + return None; + } + self.cached_replies.lock().unwrap() .calendars .as_ref() @@ -234,6 +239,17 @@ impl CalDavSource for Client { } async fn create_calendar(&mut self, id: CalendarId, name: String, supported_components: SupportedComponents) -> Result>, Box> { + self.populate_calendars().await?; + + match self.cached_replies.lock().unwrap().calendars.as_ref() { + None => return Err("No calendars have been fetched".into()), + Some(cals) => { + if cals.contains_key(&id) { + return Err("This calendar already exists".into()); + } + }, + } + let creation_body = calendar_body(name, supported_components); reqwest::Client::new() @@ -244,7 +260,6 @@ impl CalDavSource for Client { .send() .await?; - self.populate_calendars().await?; self.get_calendar(&id).await.ok_or(format!("Unable to insert calendar {:?}", id).into()) } }