diff --git a/src/calendar/remote_calendar.rs b/src/calendar/remote_calendar.rs index 0fb6cde..883c6c8 100644 --- a/src/calendar/remote_calendar.rs +++ b/src/calendar/remote_calendar.rs @@ -1,7 +1,6 @@ use std::collections::{HashMap, HashSet}; use std::error::Error; -use url::Url; use chrono::{DateTime, Utc}; use async_trait::async_trait; @@ -10,19 +9,36 @@ use crate::calendar::SupportedComponents; use crate::calendar::CalendarId; use crate::item::ItemId; use crate::item::Item; +use crate::resource::Resource; + +static TASKS_BODY: &str = r#" + + + + + + + + + + +"#; + + + /// A CalDAV calendar created by a [`Client`](crate::client::Client). #[derive(Clone)] pub struct RemoteCalendar { name: String, - url: Url, + resource: Resource, supported_components: SupportedComponents } impl RemoteCalendar { - pub fn new(name: String, url: Url, supported_components: SupportedComponents) -> Self { + pub fn new(name: String, resource: Resource, supported_components: SupportedComponents) -> Self { Self { - name, url, supported_components + name, resource, supported_components, } } } @@ -30,23 +46,40 @@ impl RemoteCalendar { #[async_trait] impl PartialCalendar for RemoteCalendar { fn name(&self) -> &str { &self.name } - fn id(&self) -> &CalendarId { &self.url } + fn id(&self) -> &CalendarId { &self.resource.url() } fn supported_components(&self) -> crate::calendar::SupportedComponents { self.supported_components } /// Returns the items that have been last-modified after `since` async fn get_items_modified_since(&self, since: Option>, _filter: Option) - -> HashMap + -> Result, Box> { log::error!("Not implemented"); - HashMap::new() + Ok(HashMap::new()) } /// Get the IDs of all current items in this calendar - async fn get_item_ids(&mut self) -> HashSet { - log::error!("Not implemented"); - HashSet::new() + async fn get_item_ids(&self) -> Result, Box> { + let responses = crate::client::sub_request_and_extract_elems(&self.resource, "REPORT", TASKS_BODY.to_string(), "response").await?; + + let mut item_ids = HashSet::new(); + for response in responses { + let item_url = crate::utils::find_elem(&response, "href") + .map(|elem| self.resource.combine(&elem.text())); + + match item_url { + None => { + log::warn!("Unable to extract HREF"); + continue; + }, + Some(resource) => { + item_ids.insert(ItemId::from(&resource)); + }, + }; + } + + Ok(item_ids) } /// Returns a particular item