Migrate mkcol/mkcalendar to rustical_xml

This commit is contained in:
Lennart
2024-12-23 23:13:18 +01:00
parent 72844aa94e
commit 92b7be5591
6 changed files with 100 additions and 46 deletions

View File

@@ -3,34 +3,29 @@ use actix_web::web::{Data, Path};
use actix_web::HttpResponse;
use rustical_store::auth::User;
use rustical_store::{Calendar, CalendarStore};
use serde::{Deserialize, Serialize};
use rustical_xml::{Unit, XmlDeserialize, XmlDocument, XmlRootTag};
use tracing::instrument;
use tracing_actix_web::RootSpan;
#[derive(Deserialize, Serialize, Clone, Debug)]
#[serde(rename_all = "kebab-case")]
#[derive(XmlDeserialize, Clone, Debug)]
pub struct Resourcetype {
#[serde(rename = "C:calendar", alias = "calendar")]
calendar: Option<()>,
collection: Option<()>,
calendar: Option<Unit>,
collection: Option<Unit>,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "kebab-case")]
#[derive(XmlDeserialize, Clone, Debug)]
pub struct CalendarComponentElement {
#[serde(rename = "@name")]
#[xml(ty = "attr")]
name: String,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "kebab-case")]
#[derive(XmlDeserialize, Clone, Debug)]
pub struct SupportedCalendarComponentSetElement {
#[serde(rename = "$value")]
#[xml(flatten)]
comp: Vec<CalendarComponentElement>,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "kebab-case")]
#[derive(XmlDeserialize, Clone, Debug)]
pub struct MkcolCalendarProp {
resourcetype: Option<Resourcetype>,
displayname: Option<String>,
@@ -38,17 +33,17 @@ pub struct MkcolCalendarProp {
calendar_color: Option<String>,
order: Option<i64>,
calendar_timezone: Option<String>,
calendar_timezone_id: Option<String>,
supported_calendar_component_set: Option<SupportedCalendarComponentSetElement>,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct PropElement<T: Serialize> {
#[derive(XmlDeserialize, Clone, Debug)]
pub struct PropElement<T: XmlDeserialize> {
prop: T,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "kebab-case")]
#[serde(rename = "mkcalendar")]
#[derive(XmlDeserialize, XmlRootTag, Clone, Debug)]
#[xml(root = b"mkcalendar")]
struct MkcalendarRequest {
set: PropElement<MkcolCalendarProp>,
}
@@ -66,7 +61,7 @@ pub async fn route_mkcalendar<C: CalendarStore + ?Sized>(
return Err(Error::Unauthorized);
}
let request: MkcalendarRequest = quick_xml::de::from_str(&body)?;
let request = MkcalendarRequest::parse_str(&body)?;
let request = request.set.prop;
let calendar = Calendar {
@@ -108,3 +103,30 @@ pub async fn route_mkcalendar<C: CalendarStore + ?Sized>(
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_xml_mkcalendar() {
let mkcalendar_request = MkcalendarRequest::parse_str(r#"
<?xml version='1.0' encoding='UTF-8' ?>
<CAL:mkcalendar xmlns="DAV:" xmlns:CAL="urn:ietf:params:xml:ns:caldav" xmlns:CARD="urn:ietf:params:xml:ns:carddav">
<set>
<prop>
<resourcetype>
<collection />
<CAL:calendar />
</resourcetype>
<displayname>jfs</displayname>
<CAL:calendar-description>rggg</CAL:calendar-description>
<n0:calendar-color xmlns:n0="http://apple.com/ns/ical/">#FFF8DCFF</n0:calendar-color>
<CAL:calendar-timezone-id>Europe/Berlin</CAL:calendar-timezone-id>
<CAL:calendar-timezone>BEGIN:VCALENDAR\r\nBEGIN:VTIMEZONE\r\nTZID:Europe/Berlin\r\nLAST-MODIFIED:20240422T053450Z\r\nTZURL:https://www.tzurl.org/zoneinfo/Europe/Berlin\r\nX-LIC-LOCATION:Europe/Berlin\r\nX-PROLEPTIC-TZNAME:LMT\r\nBEGIN:STANDARD\r\nTZNAME:CET\r\nTZOFFSETFROM:+005328\r\nTZOFFSETTO:+0100\r\nDTSTART:18930401T000632\r\nEND:STANDARD\r\nBEGIN:DAYLIGHT\r\nTZNAME:CEST\r\nTZOFFSETFROM:+0100\r\nTZOFFSETTO:+0200\r\nDTSTART:19160430T230000\r\nRDATE:19400401T020000\r\nRDATE:19430329T020000\r\nRDATE:19460414T020000\r\nRDATE:19470406T030000\r\nRDATE:19480418T020000\r\nRDATE:19490410T020000\r\nRDATE:19800406T020000\r\nEND:DAYLIGHT\r\nBEGIN:STANDARD\r\nTZNAME:CET\r\nTZOFFSETFROM:+0200\r\nTZOFFSETTO:+0100\r\nDTSTART:19161001T010000\r\nRDATE:19421102T030000\r\nRDATE:19431004T030000\r\nRDATE:19441002T030000\r\nRDATE:19451118T030000\r\nRDATE:19461007T030000\r\nEND:STANDARD\r\nBEGIN:DAYLIGHT\r\nTZNAME:CEST\r\nTZOFFSETFROM:+0100\r\nTZOFFSETTO:+0200\r\nDTSTART:19170416T020000\r\nRRULE:FREQ=YEARLY;UNTIL=19180415T010000Z;BYMONTH=4;BYDAY=3MO\r\nEND:DAYLIGHT\r\nBEGIN:STANDARD\r\nTZNAME:CET\r\nTZOFFSETFROM:+0200\r\nTZOFFSETTO:+0100\r\nDTSTART:19170917T030000\r\nRRULE:FREQ=YEARLY;UNTIL=19180916T010000Z;BYMONTH=9;BYDAY=3MO\r\nEND:STANDARD\r\nBEGIN:DAYLIGHT\r\nTZNAME:CEST\r\nTZOFFSETFROM:+0100\r\nTZOFFSETTO:+0200\r\nDTSTART:19440403T020000\r\nRRULE:FREQ=YEARLY;UNTIL=19450402T010000Z;BYMONTH=4;BYDAY=1MO\r\nEND:DAYLIGHT\r\nBEGIN:DAYLIGHT\r\nTZNAME:CEMT\r\nTZOFFSETFROM:+0200\r\nTZOFFSETTO:+0300\r\nDTSTART:19450524T000000\r\nRDATE:19470511T010000\r\nEND:DAYLIGHT\r\nBEGIN:DAYLIGHT\r\nTZNAME:CEST\r\nTZOFFSETFROM:+0300\r\nTZOFFSETTO:+0200\r\nDTSTART:19450924T030000\r\nRDATE:19470629T030000\r\nEND:DAYLIGHT\r\nBEGIN:STANDARD\r\nTZNAME:CET\r\nTZOFFSETFROM:+0100\r\nTZOFFSETTO:+0100\r\nDTSTART:19460101T000000\r\nRDATE:19800101T000000\r\nEND:STANDARD\r\nBEGIN:STANDARD\r\nTZNAME:CET\r\nTZOFFSETFROM:+0200\r\nTZOFFSETTO:+0100\r\nDTSTART:19471005T030000\r\nRRULE:FREQ=YEARLY;UNTIL=19491002T010000Z;BYMONTH=10;BYDAY=1SU\r\nEND:STANDARD\r\nBEGIN:STANDARD\r\nTZNAME:CET\r\nTZOFFSETFROM:+0200\r\nTZOFFSETTO:+0100\r\nDTSTART:19800928T030000\r\nRRULE:FREQ=YEARLY;UNTIL=19950924T010000Z;BYMONTH=9;BYDAY=-1SU\r\nEND:STANDARD\r\nBEGIN:DAYLIGHT\r\nTZNAME:CEST\r\nTZOFFSETFROM:+0100\r\nTZOFFSETTO:+0200\r\nDTSTART:19810329T020000\r\nRRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU\r\nEND:DAYLIGHT\r\nBEGIN:STANDARD\r\nTZNAME:CET\r\nTZOFFSETFROM:+0200\r\nTZOFFSETTO:+0100\r\nDTSTART:19961027T030000\r\nRRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=-1SU\r\nEND:STANDARD\r\nEND:VTIMEZONE\r\nEND:VCALENDAR\r\n</CAL:calendar-timezone>
</prop>
</set>
</CAL:mkcalendar>
"#).unwrap();
}
}

View File

@@ -83,7 +83,7 @@ mod tests {
use super::*;
#[test]
fn test_xml_sync_collection() {
fn test_xml_calendar_query() {
let report_request = ReportRequest::parse_str(
r#"
<?xml version='1.0' encoding='UTF-8' ?>

View File

@@ -19,10 +19,7 @@ pub enum Error {
DavError(#[from] rustical_dav::Error),
#[error(transparent)]
NewXmlDecodeError(#[from] rustical_xml::XmlDeError),
#[error(transparent)]
XmlDecodeError(#[from] quick_xml::DeError),
XmlDecodeError(#[from] rustical_xml::XmlDeError),
#[error(transparent)]
Other(#[from] anyhow::Error),
@@ -38,7 +35,6 @@ impl actix_web::ResponseError for Error {
},
Error::DavError(err) => err.status_code(),
Error::Unauthorized => StatusCode::UNAUTHORIZED,
Error::NewXmlDecodeError(_) => StatusCode::BAD_REQUEST,
Error::XmlDecodeError(_) => StatusCode::BAD_REQUEST,
Error::NotImplemented => StatusCode::INTERNAL_SERVER_ERROR,
Error::Other(_) => StatusCode::INTERNAL_SERVER_ERROR,

View File

@@ -2,48 +2,49 @@ use crate::Error;
use actix_web::web::Path;
use actix_web::{web::Data, HttpResponse};
use rustical_store::{auth::User, Addressbook, AddressbookStore};
use serde::{Deserialize, Serialize};
use rustical_xml::{Unit, XmlDeserialize, XmlDocument, XmlRootTag};
use tracing::instrument;
use tracing_actix_web::RootSpan;
#[derive(Deserialize, Serialize, Clone, Debug)]
#[serde(rename_all = "kebab-case")]
#[derive(XmlDeserialize, Clone, Debug, PartialEq)]
pub struct Resourcetype {
#[serde(rename = "CARD:addressbook", alias = "addressbook")]
addressbook: Option<()>,
collection: Option<()>,
addressbook: Option<Unit>,
collection: Option<Unit>,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "kebab-case")]
#[derive(XmlDeserialize, Clone, Debug, PartialEq)]
pub struct MkcolAddressbookProp {
resourcetype: Option<Resourcetype>,
displayname: Option<String>,
#[xml(rename = b"addressbook-description")]
description: Option<String>,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct PropElement<T: Serialize> {
#[derive(XmlDeserialize, Clone, Debug, PartialEq)]
pub struct PropElement<T: XmlDeserialize> {
prop: T,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "kebab-case")]
#[serde(rename = "mkcol")]
#[derive(XmlDeserialize, XmlRootTag, Clone, Debug, PartialEq)]
#[xml(root = b"mkcol")]
struct MkcolRequest {
set: PropElement<MkcolAddressbookProp>,
}
#[instrument(parent = root_span.id(), skip(store, root_span))]
pub async fn route_mkcol<AS: AddressbookStore + ?Sized>(
path: Path<(String, String)>,
body: String,
user: User,
store: Data<AS>,
root_span: RootSpan,
) -> Result<HttpResponse, Error> {
let (principal, addressbook_id) = path.into_inner();
if principal != user.id {
return Err(Error::Unauthorized);
}
let request: MkcolRequest = quick_xml::de::from_str(&body)?;
let request = MkcolRequest::parse_str(&body)?;
let request = request.set.prop;
let addressbook = Addressbook {
@@ -81,3 +82,42 @@ pub async fn route_mkcol<AS: AddressbookStore + ?Sized>(
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_xml_mkcol() {
let mkcol_request = MkcolRequest::parse_str(r#"
<?xml version='1.0' encoding='UTF-8' ?>
<mkcol xmlns="DAV:" xmlns:CAL="urn:ietf:params:xml:ns:caldav" xmlns:CARD="urn:ietf:params:xml:ns:carddav">
<set>
<prop>
<resourcetype>
<collection />
<CARD:addressbook />
</resourcetype>
<displayname>whoops</displayname>
<CARD:addressbook-description>okay</CARD:addressbook-description>
</prop>
</set>
</mkcol>
"#).unwrap();
assert_eq!(
mkcol_request,
MkcolRequest {
set: PropElement {
prop: MkcolAddressbookProp {
resourcetype: Some(Resourcetype {
addressbook: Some(Unit),
collection: Some(Unit)
}),
displayname: Some("whoops".to_owned()),
description: Some("okay".to_owned())
}
}
}
)
}
}

View File

@@ -31,7 +31,7 @@ pub async fn route_report_addressbook<AS: AddressbookStore + ?Sized>(
return Err(Error::Unauthorized);
}
let request = ReportRequest::parse_str(&body).map_err(crate::Error::NewXmlDecodeError)?;
let request = ReportRequest::parse_str(&body)?;
Ok(match request.clone() {
ReportRequest::AddressbookMultiget(addr_multiget) => {

View File

@@ -19,10 +19,7 @@ pub enum Error {
DavError(#[from] rustical_dav::Error),
#[error(transparent)]
NewXmlDecodeError(#[from] rustical_xml::XmlDeError),
#[error(transparent)]
XmlDecodeError(#[from] quick_xml::DeError),
XmlDecodeError(#[from] rustical_xml::XmlDeError),
#[error(transparent)]
Other(#[from] anyhow::Error),
@@ -38,7 +35,6 @@ impl actix_web::ResponseError for Error {
},
Error::DavError(err) => err.status_code(),
Error::Unauthorized => StatusCode::UNAUTHORIZED,
Error::NewXmlDecodeError(_) => StatusCode::BAD_REQUEST,
Error::XmlDecodeError(_) => StatusCode::BAD_REQUEST,
Error::NotImplemented => StatusCode::INTERNAL_SERVER_ERROR,
Error::Other(_) => StatusCode::INTERNAL_SERVER_ERROR,