mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-13 22:52:22 +00:00
Migrate mkcol/mkcalendar to rustical_xml
This commit is contained in:
@@ -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())
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user