mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-13 22:52:22 +00:00
Add more efficient time-range calendar querying
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
use std::ops::Deref;
|
||||
|
||||
use actix_web::HttpRequest;
|
||||
use rustical_dav::{
|
||||
resource::Resource,
|
||||
xml::{MultistatusElement, PropElement, PropfindType},
|
||||
};
|
||||
use rustical_store::{auth::User, calendar::UtcDateTime, CalendarObject, CalendarStore};
|
||||
use rustical_store::{
|
||||
auth::User, calendar::UtcDateTime, calendar_store::CalendarQuery, CalendarObject, CalendarStore,
|
||||
};
|
||||
use rustical_xml::XmlDeserialize;
|
||||
use std::ops::Deref;
|
||||
|
||||
use crate::{
|
||||
calendar_object::resource::{CalendarObjectPropWrapper, CalendarObjectResource},
|
||||
@@ -141,6 +142,7 @@ impl CompFilterElement {
|
||||
#[allow(dead_code)]
|
||||
// https://datatracker.ietf.org/doc/html/rfc4791#section-9.7
|
||||
pub(crate) struct FilterElement {
|
||||
// This comp-filter matches on VCALENDAR
|
||||
#[xml(ns = "rustical_dav::namespace::NS_CALDAV")]
|
||||
pub(crate) comp_filter: CompFilterElement,
|
||||
}
|
||||
@@ -151,6 +153,27 @@ impl FilterElement {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&FilterElement> for CalendarQuery {
|
||||
fn from(value: &FilterElement) -> Self {
|
||||
let comp_filter_vcalendar = &value.comp_filter;
|
||||
for comp_filter in comp_filter_vcalendar.comp_filter.iter() {
|
||||
// A calendar object cannot contain both VEVENT and VTODO, so we only have to handle
|
||||
// whatever we get first
|
||||
if matches!(comp_filter.name.as_str(), "VEVENT" | "VTODO") {
|
||||
if let Some(time_range) = &comp_filter.time_range {
|
||||
let start = time_range.start.as_ref().map(|start| start.date_naive());
|
||||
let end = time_range.end.as_ref().map(|end| end.date_naive());
|
||||
return CalendarQuery {
|
||||
time_start: start,
|
||||
time_end: end,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(XmlDeserialize, Clone, Debug, PartialEq)]
|
||||
#[allow(dead_code)]
|
||||
// <!ELEMENT calendar-query ((DAV:allprop | DAV:propname | DAV:prop)?, filter, timezone?)>
|
||||
@@ -165,13 +188,25 @@ pub struct CalendarQueryRequest {
|
||||
pub(crate) timezone_id: Option<String>,
|
||||
}
|
||||
|
||||
impl From<&CalendarQueryRequest> for CalendarQuery {
|
||||
fn from(value: &CalendarQueryRequest) -> Self {
|
||||
value
|
||||
.filter
|
||||
.as_ref()
|
||||
.map(CalendarQuery::from)
|
||||
.unwrap_or_default()
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_objects_calendar_query<C: CalendarStore>(
|
||||
cal_query: &CalendarQueryRequest,
|
||||
principal: &str,
|
||||
cal_id: &str,
|
||||
store: &C,
|
||||
) -> Result<Vec<CalendarObject>, Error> {
|
||||
let mut objects = store.get_objects(principal, cal_id).await?;
|
||||
let mut objects = store
|
||||
.calendar_query(principal, cal_id, cal_query.into())
|
||||
.await?;
|
||||
if let Some(filter) = &cal_query.filter {
|
||||
objects.retain(|object| filter.matches(object));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user