Implemented RemoteCalendar::get_item_ids

This commit is contained in:
daladim 2021-03-22 00:20:04 +01:00
parent f2349dbd0a
commit 7af147e417

View file

@ -1,7 +1,6 @@
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::error::Error; use std::error::Error;
use url::Url;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use async_trait::async_trait; use async_trait::async_trait;
@ -10,19 +9,36 @@ use crate::calendar::SupportedComponents;
use crate::calendar::CalendarId; use crate::calendar::CalendarId;
use crate::item::ItemId; use crate::item::ItemId;
use crate::item::Item; use crate::item::Item;
use crate::resource::Resource;
static TASKS_BODY: &str = r#"
<c:calendar-query xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:caldav">
<d:prop>
<d:getetag />
</d:prop>
<c:filter>
<c:comp-filter name="VCALENDAR">
<c:comp-filter name="VTODO" />
</c:comp-filter>
</c:filter>
</c:calendar-query>
"#;
/// A CalDAV calendar created by a [`Client`](crate::client::Client). /// A CalDAV calendar created by a [`Client`](crate::client::Client).
#[derive(Clone)] #[derive(Clone)]
pub struct RemoteCalendar { pub struct RemoteCalendar {
name: String, name: String,
url: Url, resource: Resource,
supported_components: SupportedComponents supported_components: SupportedComponents
} }
impl RemoteCalendar { 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 { Self {
name, url, supported_components name, resource, supported_components,
} }
} }
} }
@ -30,23 +46,40 @@ impl RemoteCalendar {
#[async_trait] #[async_trait]
impl PartialCalendar for RemoteCalendar { impl PartialCalendar for RemoteCalendar {
fn name(&self) -> &str { &self.name } 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 { fn supported_components(&self) -> crate::calendar::SupportedComponents {
self.supported_components self.supported_components
} }
/// Returns the items that have been last-modified after `since` /// Returns the items that have been last-modified after `since`
async fn get_items_modified_since(&self, since: Option<DateTime<Utc>>, _filter: Option<crate::calendar::SearchFilter>) async fn get_items_modified_since(&self, since: Option<DateTime<Utc>>, _filter: Option<crate::calendar::SearchFilter>)
-> HashMap<ItemId, &Item> -> Result<HashMap<ItemId, &Item>, Box<dyn Error>>
{ {
log::error!("Not implemented"); log::error!("Not implemented");
HashMap::new() Ok(HashMap::new())
} }
/// Get the IDs of all current items in this calendar /// Get the IDs of all current items in this calendar
async fn get_item_ids(&mut self) -> HashSet<ItemId> { async fn get_item_ids(&self) -> Result<HashSet<ItemId>, Box<dyn Error>> {
log::error!("Not implemented"); let responses = crate::client::sub_request_and_extract_elems(&self.resource, "REPORT", TASKS_BODY.to_string(), "response").await?;
HashSet::new()
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 /// Returns a particular item