report multiget: Add NotFound handling

This commit is contained in:
Lennart
2024-10-08 12:53:16 +02:00
parent f7d253de85
commit 00c493dea4

View File

@@ -5,12 +5,16 @@ use crate::{
}; };
use actix_web::{ use actix_web::{
dev::{Path, ResourceDef}, dev::{Path, ResourceDef},
http::StatusCode,
HttpRequest, HttpRequest,
}; };
use rustical_dav::{ use rustical_dav::{
methods::propfind::{PropElement, PropfindType}, methods::propfind::{PropElement, PropfindType},
resource::Resource, resource::Resource,
xml::{multistatus::PropstatWrapper, MultistatusElement}, xml::{
multistatus::{PropstatWrapper, ResponseElement},
MultistatusElement,
},
}; };
use rustical_store::{model::object::CalendarObject, CalendarStore}; use rustical_store::{model::object::CalendarObject, CalendarStore};
use serde::Deserialize; use serde::Deserialize;
@@ -32,28 +36,31 @@ pub async fn get_objects_calendar_multiget<C: CalendarStore + ?Sized>(
principal: &str, principal: &str,
cid: &str, cid: &str,
store: &RwLock<C>, store: &RwLock<C>,
) -> Result<Vec<CalendarObject>, Error> { ) -> Result<(Vec<CalendarObject>, Vec<String>), Error> {
// TODO: add proper error results for single events
let resource_def = ResourceDef::prefix(principal_url).join(&ResourceDef::new("/{cid}/{uid}")); let resource_def = ResourceDef::prefix(principal_url).join(&ResourceDef::new("/{cid}/{uid}"));
let mut result = vec![]; let mut result = vec![];
let mut not_found = vec![];
let store = store.read().await; let store = store.read().await;
for href in &cal_query.href { for href in &cal_query.href {
let mut path = Path::new(href.as_str()); let mut path = Path::new(href.as_str());
if !resource_def.capture_match_info(&mut path) { if !resource_def.capture_match_info(&mut path) {
// TODO: Handle error not_found.push(href.to_owned());
continue;
}; };
if path.get("cid").unwrap() != cid { if path.get("cid").unwrap() != cid {
// TODO: Handle error not_found.push(href.to_owned());
continue;
} }
let uid = path.get("uid").unwrap(); let uid = path.get("uid").unwrap();
result.push(store.get_object(principal, cid, uid).await?); match store.get_object(principal, cid, uid).await {
Ok(object) => result.push(object),
Err(rustical_store::Error::NotFound) => not_found.push(href.to_owned()),
// TODO: Maybe add event handling on a per-event basis
Err(err) => return Err(err.into()),
};
} }
Ok(result) Ok((result, not_found))
} }
pub async fn handle_calendar_multiget<C: CalendarStore + ?Sized>( pub async fn handle_calendar_multiget<C: CalendarStore + ?Sized>(
@@ -64,7 +71,7 @@ pub async fn handle_calendar_multiget<C: CalendarStore + ?Sized>(
cal_store: &RwLock<C>, cal_store: &RwLock<C>,
) -> Result<MultistatusElement<PropstatWrapper<CalendarObjectProp>, String>, Error> { ) -> Result<MultistatusElement<PropstatWrapper<CalendarObjectProp>, String>, Error> {
let principal_url = PrincipalResource::get_url(req.resource_map(), vec![principal]).unwrap(); let principal_url = PrincipalResource::get_url(req.resource_map(), vec![principal]).unwrap();
let objects = let (objects, not_found) =
get_objects_calendar_multiget(&cal_multiget, &principal_url, principal, cid, cal_store) get_objects_calendar_multiget(&cal_multiget, &principal_url, principal, cid, cal_store)
.await?; .await?;
@@ -90,8 +97,18 @@ pub async fn handle_calendar_multiget<C: CalendarStore + ?Sized>(
); );
} }
let not_found_responses = not_found
.into_iter()
.map(|path| ResponseElement {
href: path,
status: Some(format!("HTTP/1.1 {}", StatusCode::NOT_FOUND)),
..Default::default()
})
.collect();
Ok(MultistatusElement { Ok(MultistatusElement {
responses, responses,
member_responses: not_found_responses,
..Default::default() ..Default::default()
}) })
} }