Some refactoring work

This commit is contained in:
Lennart
2025-05-02 19:53:02 +02:00
parent 32e1ce85f1
commit c9683580eb
8 changed files with 94 additions and 109 deletions

View File

@@ -9,7 +9,7 @@ use actix_web::{
};
use rustical_dav::{
resource::Resource,
xml::{MultistatusElement, PropElement, PropfindType, multistatus::ResponseElement},
xml::{MultistatusElement, PropfindType, multistatus::ResponseElement},
};
use rustical_store::{AddressObject, AddressbookStore, auth::User};
use rustical_xml::XmlDeserialize;
@@ -58,7 +58,8 @@ pub async fn get_objects_addressbook_multiget<AS: AddressbookStore>(
}
pub async fn handle_addressbook_multiget<AS: AddressbookStore>(
addr_multiget: AddressbookMultigetRequest,
addr_multiget: &AddressbookMultigetRequest,
props: &[&str],
req: HttpRequest,
user: &User,
principal: &str,
@@ -66,22 +67,9 @@ pub async fn handle_addressbook_multiget<AS: AddressbookStore>(
addr_store: &AS,
) -> Result<MultistatusElement<AddressObjectPropWrapper, String>, Error> {
let (objects, not_found) =
get_objects_addressbook_multiget(&addr_multiget, req.path(), principal, cal_id, addr_store)
get_objects_addressbook_multiget(addr_multiget, req.path(), principal, cal_id, addr_store)
.await?;
let props = match addr_multiget.prop {
PropfindType::Allprop => {
vec!["allprop".to_owned()]
}
PropfindType::Propname => {
vec!["propname".to_owned()]
}
PropfindType::Prop(PropElement(prop_tags)) => {
prop_tags.into_iter().map(|propname| propname.0).collect()
}
};
let props: Vec<&str> = props.iter().map(String::as_str).collect();
let mut responses = Vec::new();
for object in objects {
let path = format!("{}/{}.vcf", req.path(), object.get_id());
@@ -90,7 +78,7 @@ pub async fn handle_addressbook_multiget<AS: AddressbookStore>(
object,
principal: principal.to_owned(),
}
.propfind(&path, &props, user, req.resource_map())?,
.propfind(&path, props, user, req.resource_map())?,
);
}

View File

@@ -1,11 +1,11 @@
use crate::Error;
use actix_web::{
web::{Data, Path},
HttpRequest, Responder,
web::{Data, Path},
};
use addressbook_multiget::{handle_addressbook_multiget, AddressbookMultigetRequest};
use rustical_dav::xml::sync_collection::SyncCollectionRequest;
use rustical_store::{auth::User, AddressbookStore};
use addressbook_multiget::{AddressbookMultigetRequest, handle_addressbook_multiget};
use rustical_dav::xml::{PropElement, PropfindType, sync_collection::SyncCollectionRequest};
use rustical_store::{AddressbookStore, auth::User};
use rustical_xml::{XmlDeserialize, XmlDocument};
use sync_collection::handle_sync_collection;
use tracing::instrument;
@@ -21,6 +21,28 @@ pub(crate) enum ReportRequest {
SyncCollection(SyncCollectionRequest),
}
impl ReportRequest {
fn props(&self) -> Vec<&str> {
let prop_element = match self {
ReportRequest::AddressbookMultiget(AddressbookMultigetRequest { prop, .. }) => prop,
ReportRequest::SyncCollection(SyncCollectionRequest { prop, .. }) => prop,
};
match prop_element {
PropfindType::Allprop => {
vec!["allprop"]
}
PropfindType::Propname => {
vec!["propname"]
}
PropfindType::Prop(PropElement(prop_tags)) => prop_tags
.iter()
.map(|propname| propname.0.as_str())
.collect(),
}
}
}
#[instrument(skip(req, addr_store))]
pub async fn route_report_addressbook<AS: AddressbookStore>(
path: Path<(String, String)>,
@@ -35,11 +57,13 @@ pub async fn route_report_addressbook<AS: AddressbookStore>(
}
let request = ReportRequest::parse_str(&body)?;
let props = request.props();
Ok(match request.clone() {
Ok(match &request {
ReportRequest::AddressbookMultiget(addr_multiget) => {
handle_addressbook_multiget(
addr_multiget,
&props,
req,
&user,
&principal,
@@ -51,6 +75,7 @@ pub async fn route_report_addressbook<AS: AddressbookStore>(
ReportRequest::SyncCollection(sync_collection) => {
handle_sync_collection(
sync_collection,
&props,
req,
&user,
&principal,
@@ -64,7 +89,7 @@ pub async fn route_report_addressbook<AS: AddressbookStore>(
#[cfg(test)]
mod tests {
use rustical_dav::xml::{sync_collection::SyncLevel, PropElement, Propname};
use rustical_dav::xml::{PropElement, Propname, sync_collection::SyncLevel};
use super::*;

View File

@@ -6,8 +6,7 @@ use actix_web::{HttpRequest, http::StatusCode};
use rustical_dav::{
resource::Resource,
xml::{
MultistatusElement, PropElement, PropfindType, multistatus::ResponseElement,
sync_collection::SyncCollectionRequest,
MultistatusElement, multistatus::ResponseElement, sync_collection::SyncCollectionRequest,
},
};
use rustical_store::{
@@ -17,26 +16,14 @@ use rustical_store::{
};
pub async fn handle_sync_collection<AS: AddressbookStore>(
sync_collection: SyncCollectionRequest,
sync_collection: &SyncCollectionRequest,
props: &[&str],
req: HttpRequest,
user: &User,
principal: &str,
addressbook_id: &str,
addr_store: &AS,
) -> Result<MultistatusElement<AddressObjectPropWrapper, String>, Error> {
let props = match sync_collection.prop {
PropfindType::Allprop => {
vec!["allprop".to_owned()]
}
PropfindType::Propname => {
vec!["propname".to_owned()]
}
PropfindType::Prop(PropElement(prop_tags)) => {
prop_tags.into_iter().map(|propname| propname.0).collect()
}
};
let props: Vec<&str> = props.iter().map(String::as_str).collect();
let old_synctoken = parse_synctoken(&sync_collection.sync_token).unwrap_or(0);
let (new_objects, deleted_objects, new_synctoken) = addr_store
.sync_changes(principal, addressbook_id, old_synctoken)
@@ -54,7 +41,7 @@ pub async fn handle_sync_collection<AS: AddressbookStore>(
object,
principal: principal.to_owned(),
}
.propfind(&path, &props, user, req.resource_map())?,
.propfind(&path, props, user, req.resource_map())?,
);
}