Refactoring around routing and getting the principal uri (less dependence on actix)

This commit is contained in:
Lennart K
2025-06-02 16:17:13 +02:00
parent 0f294cf2e1
commit ef33868151
23 changed files with 169 additions and 216 deletions

View File

@@ -1,11 +1,10 @@
use crate::{Error, principal::PrincipalResource};
use actix_web::dev::ResourceMap;
use crate::{CardDavPrincipalUri, Error};
use async_trait::async_trait;
use derive_more::derive::{Constructor, From, Into};
use rustical_dav::{
extensions::{CommonPropertiesExtension, CommonPropertiesProp},
privileges::UserPrivilegeSet,
resource::{Resource, ResourceService},
resource::{PrincipalUri, Resource, ResourceService},
xml::Resourcetype,
};
use rustical_store::{AddressObject, AddressbookStore, auth::User};
@@ -47,10 +46,6 @@ pub struct AddressObjectResource {
pub principal: String,
}
impl CommonPropertiesExtension for AddressObjectResource {
type PrincipalResource = PrincipalResource;
}
impl Resource for AddressObjectResource {
type Prop = AddressObjectPropWrapper;
type Error = Error;
@@ -62,7 +57,7 @@ impl Resource for AddressObjectResource {
fn get_prop(
&self,
rmap: &ResourceMap,
puri: &impl PrincipalUri,
user: &User,
prop: &AddressObjectPropWrapperName,
) -> Result<Self::Prop, Self::Error> {
@@ -81,7 +76,7 @@ impl Resource for AddressObjectResource {
})
}
AddressObjectPropWrapperName::Common(prop) => AddressObjectPropWrapper::Common(
CommonPropertiesExtension::get_prop(self, rmap, user, prop)?,
CommonPropertiesExtension::get_prop(self, puri, user, prop)?,
),
})
}
@@ -115,6 +110,7 @@ impl<AS: AddressbookStore> ResourceService for AddressObjectResourceService<AS>
type MemberType = AddressObjectResource;
type Error = Error;
type Principal = User;
type PrincipalUri = CardDavPrincipalUri;
async fn get_resource(
&self,

View File

@@ -3,12 +3,11 @@ use crate::{
address_object::resource::{AddressObjectPropWrapper, AddressObjectResource},
};
use actix_web::{
HttpRequest,
dev::{Path, ResourceDef},
http::StatusCode,
};
use rustical_dav::{
resource::Resource,
resource::{PrincipalUri, Resource},
xml::{MultistatusElement, PropfindType, multistatus::ResponseElement},
};
use rustical_store::{AddressObject, AddressbookStore, auth::User};
@@ -60,25 +59,26 @@ pub async fn get_objects_addressbook_multiget<AS: AddressbookStore>(
pub async fn handle_addressbook_multiget<AS: AddressbookStore>(
addr_multiget: &AddressbookMultigetRequest,
props: &[&str],
req: HttpRequest,
path: &str,
puri: &impl PrincipalUri,
user: &User,
principal: &str,
cal_id: &str,
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, path, principal, cal_id, addr_store)
.await?;
let mut responses = Vec::new();
for object in objects {
let path = format!("{}/{}.vcf", req.path(), object.get_id());
let path = format!("{}/{}.vcf", path, object.get_id());
responses.push(
AddressObjectResource {
object,
principal: principal.to_owned(),
}
.propfind(&path, props, user, req.resource_map())?,
.propfind(&path, props, puri, user)?,
);
}

View File

@@ -1,4 +1,4 @@
use crate::Error;
use crate::{CardDavPrincipalUri, Error};
use actix_web::{
HttpRequest, Responder,
web::{Data, Path},
@@ -49,6 +49,7 @@ pub async fn route_report_addressbook<AS: AddressbookStore>(
body: String,
user: User,
req: HttpRequest,
puri: Data<CardDavPrincipalUri>,
addr_store: Data<AS>,
) -> Result<impl Responder, Error> {
let (principal, addressbook_id) = path.into_inner();
@@ -64,7 +65,8 @@ pub async fn route_report_addressbook<AS: AddressbookStore>(
handle_addressbook_multiget(
addr_multiget,
&props,
req,
req.path(),
puri.as_ref(),
&user,
&principal,
&addressbook_id,
@@ -76,7 +78,8 @@ pub async fn route_report_addressbook<AS: AddressbookStore>(
handle_sync_collection(
sync_collection,
&props,
req,
req.path(),
puri.as_ref(),
&user,
&principal,
&addressbook_id,

View File

@@ -2,9 +2,9 @@ use crate::{
Error,
address_object::resource::{AddressObjectPropWrapper, AddressObjectResource},
};
use actix_web::{HttpRequest, http::StatusCode};
use actix_web::http::StatusCode;
use rustical_dav::{
resource::Resource,
resource::{PrincipalUri, Resource},
xml::{
MultistatusElement, multistatus::ResponseElement, sync_collection::SyncCollectionRequest,
},
@@ -18,7 +18,8 @@ use rustical_store::{
pub async fn handle_sync_collection<AS: AddressbookStore>(
sync_collection: &SyncCollectionRequest,
props: &[&str],
req: HttpRequest,
path: &str,
puri: &impl PrincipalUri,
user: &User,
principal: &str,
addressbook_id: &str,
@@ -31,22 +32,18 @@ pub async fn handle_sync_collection<AS: AddressbookStore>(
let mut responses = Vec::new();
for object in new_objects {
let path = format!(
"{}/{}.vcf",
req.path().trim_end_matches('/'),
object.get_id()
);
let path = format!("{}/{}.vcf", path.trim_end_matches('/'), object.get_id());
responses.push(
AddressObjectResource {
object,
principal: principal.to_owned(),
}
.propfind(&path, props, user, req.resource_map())?,
.propfind(&path, props, puri, user)?,
);
}
for object_id in deleted_objects {
let path = format!("{}/{}.vcf", req.path().trim_end_matches('/'), object_id);
let path = format!("{}/{}.vcf", path.trim_end_matches('/'), object_id);
responses.push(ResponseElement {
href: path,
status: Some(StatusCode::NOT_FOUND),

View File

@@ -2,10 +2,8 @@ use super::methods::mkcol::route_mkcol;
use super::methods::post::route_post;
use super::methods::report::route_report_addressbook;
use super::prop::{SupportedAddressData, SupportedReportSet};
use crate::Error;
use crate::address_object::resource::AddressObjectResource;
use crate::principal::PrincipalResource;
use actix_web::dev::ResourceMap;
use crate::{CardDavPrincipalUri, Error};
use actix_web::http::Method;
use actix_web::web;
use async_trait::async_trait;
@@ -14,7 +12,7 @@ use rustical_dav::extensions::{
CommonPropertiesExtension, CommonPropertiesProp, SyncTokenExtension, SyncTokenExtensionProp,
};
use rustical_dav::privileges::UserPrivilegeSet;
use rustical_dav::resource::{Resource, ResourceService};
use rustical_dav::resource::{PrincipalUri, Resource, ResourceService};
use rustical_dav::xml::{Resourcetype, ResourcetypeInner};
use rustical_dav_push::{DavPushExtension, DavPushExtensionProp};
use rustical_store::auth::User;
@@ -80,10 +78,6 @@ impl DavPushExtension for AddressbookResource {
}
}
impl CommonPropertiesExtension for AddressbookResource {
type PrincipalResource = PrincipalResource;
}
impl Resource for AddressbookResource {
type Prop = AddressbookPropWrapper;
type Error = Error;
@@ -98,7 +92,7 @@ impl Resource for AddressbookResource {
fn get_prop(
&self,
rmap: &ResourceMap,
puri: &impl PrincipalUri,
user: &User,
prop: &AddressbookPropWrapperName,
) -> Result<Self::Prop, Self::Error> {
@@ -130,7 +124,7 @@ impl Resource for AddressbookResource {
AddressbookPropWrapper::DavPush(<Self as DavPushExtension>::get_prop(self, prop)?)
}
AddressbookPropWrapperName::Common(prop) => AddressbookPropWrapper::Common(
CommonPropertiesExtension::get_prop(self, rmap, user, prop)?,
CommonPropertiesExtension::get_prop(self, puri, user, prop)?,
),
})
}
@@ -204,6 +198,7 @@ impl<AS: AddressbookStore, S: SubscriptionStore> ResourceService
type Resource = AddressbookResource;
type Error = Error;
type Principal = User;
type PrincipalUri = CardDavPrincipalUri;
async fn get_resource(
&self,

View File

@@ -11,9 +11,10 @@ use actix_web::{
};
use address_object::resource::AddressObjectResourceService;
use addressbook::resource::AddressbookResourceService;
use derive_more::Constructor;
pub use error::Error;
use principal::{PrincipalResource, PrincipalResourceService};
use rustical_dav::resource::{NamedRoute, ResourceService};
use rustical_dav::resource::{PrincipalUri, ResourceService};
use rustical_dav::resources::RootResourceService;
use rustical_store::{
AddressbookStore, SubscriptionStore,
@@ -26,6 +27,15 @@ pub mod addressbook;
pub mod error;
pub mod principal;
#[derive(Debug, Clone, Constructor)]
pub struct CardDavPrincipalUri(&'static str);
impl PrincipalUri for CardDavPrincipalUri {
fn principal_uri(&self, principal: &str) -> String {
format!("{}/{}", self.0, principal)
}
}
/// Quite a janky implementation but the default METHOD_NOT_ALLOWED response gives us the allowed
/// methods of a resource
fn options_handler() -> ErrorHandlers<BoxBody> {
@@ -53,6 +63,7 @@ fn options_handler() -> ErrorHandlers<BoxBody> {
}
pub fn carddav_service<AP: AuthenticationProvider, A: AddressbookStore, S: SubscriptionStore>(
prefix: &'static str,
auth_provider: Arc<AP>,
store: Arc<A>,
subscription_store: Arc<S>,
@@ -62,14 +73,19 @@ pub fn carddav_service<AP: AuthenticationProvider, A: AddressbookStore, S: Subsc
.wrap(options_handler())
.app_data(Data::from(store.clone()))
.app_data(Data::from(subscription_store))
.service(RootResourceService::<PrincipalResource, User>::default().actix_resource())
.app_data(Data::new(CardDavPrincipalUri::new(
format!("{prefix}/principal").leak(),
)))
.service(
RootResourceService::<PrincipalResource, User, CardDavPrincipalUri>::default()
.actix_resource(),
)
.service(
web::scope("/principal").service(
web::scope("/{principal}")
.service(
PrincipalResourceService::new(store.clone(), auth_provider)
.actix_resource()
.name(PrincipalResource::route_name()),
.actix_resource(),
)
.service(
web::scope("/{addressbook_id}")

View File

@@ -1,10 +1,9 @@
use crate::Error;
use crate::addressbook::resource::AddressbookResource;
use actix_web::dev::ResourceMap;
use crate::{CardDavPrincipalUri, Error};
use async_trait::async_trait;
use rustical_dav::extensions::{CommonPropertiesExtension, CommonPropertiesProp};
use rustical_dav::privileges::UserPrivilegeSet;
use rustical_dav::resource::{NamedRoute, Resource, ResourceService};
use rustical_dav::resource::{PrincipalUri, Resource, ResourceService};
use rustical_dav::xml::{HrefElement, Resourcetype, ResourcetypeInner};
use rustical_store::AddressbookStore;
use rustical_store::auth::{AuthenticationProvider, User};
@@ -58,22 +57,6 @@ pub enum PrincipalPropWrapper {
Common(CommonPropertiesProp),
}
impl PrincipalResource {
pub fn get_principal_url(rmap: &ResourceMap, principal: &str) -> String {
Self::get_url(rmap, vec![principal]).unwrap()
}
}
impl NamedRoute for PrincipalResource {
fn route_name() -> &'static str {
"carddav_principal"
}
}
impl CommonPropertiesExtension for PrincipalResource {
type PrincipalResource = Self;
}
impl Resource for PrincipalResource {
type Prop = PrincipalPropWrapper;
type Error = Error;
@@ -88,16 +71,16 @@ impl Resource for PrincipalResource {
fn get_prop(
&self,
rmap: &ResourceMap,
puri: &impl PrincipalUri,
user: &User,
prop: &PrincipalPropWrapperName,
) -> Result<Self::Prop, Self::Error> {
let principal_href = HrefElement::new(Self::get_principal_url(rmap, &self.principal.id));
let principal_href = HrefElement::new(puri.principal_uri(&user.id));
let home_set = AddressbookHomeSet(
user.memberships()
.into_iter()
.map(|principal| Self::get_url(rmap, vec![principal]).unwrap())
.map(|principal| puri.principal_uri(principal))
.map(HrefElement::new)
.collect(),
);
@@ -120,7 +103,7 @@ impl Resource for PrincipalResource {
}
PrincipalPropWrapperName::Common(prop) => PrincipalPropWrapper::Common(
CommonPropertiesExtension::get_prop(self, rmap, user, prop)?,
CommonPropertiesExtension::get_prop(self, puri, user, prop)?,
),
})
}
@@ -145,6 +128,7 @@ impl<A: AddressbookStore, AP: AuthenticationProvider> ResourceService
type Resource = PrincipalResource;
type Error = Error;
type Principal = User;
type PrincipalUri = CardDavPrincipalUri;
async fn get_resource(
&self,