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

@@ -4,12 +4,11 @@ use crate::{
calendar_object::resource::{CalendarObjectPropWrapper, CalendarObjectResource},
};
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::{CalendarObject, CalendarStore, auth::User};
@@ -58,25 +57,25 @@ pub async fn get_objects_calendar_multiget<C: CalendarStore>(
pub async fn handle_calendar_multiget<C: CalendarStore>(
cal_multiget: &CalendarMultigetRequest,
props: &[&str],
req: HttpRequest,
path: &str,
puri: &impl PrincipalUri,
user: &User,
principal: &str,
cal_id: &str,
cal_store: &C,
) -> Result<MultistatusElement<CalendarObjectPropWrapper, String>, Error> {
let (objects, not_found) =
get_objects_calendar_multiget(cal_multiget, req.path(), principal, cal_id, cal_store)
.await?;
get_objects_calendar_multiget(cal_multiget, path, principal, cal_id, cal_store).await?;
let mut responses = Vec::new();
for object in objects {
let path = format!("{}/{}.ics", req.path(), object.get_id());
let path = format!("{}/{}.ics", path, object.get_id());
responses.push(
CalendarObjectResource {
object,
principal: principal.to_owned(),
}
.propfind(&path, props, user, req.resource_map())?,
.propfind(&path, props, puri, user)?,
);
}

View File

@@ -1,6 +1,5 @@
use actix_web::HttpRequest;
use rustical_dav::{
resource::Resource,
resource::{PrincipalUri, Resource},
xml::{MultistatusElement, PropfindType},
};
use rustical_ical::UtcDateTime;
@@ -217,7 +216,8 @@ pub async fn get_objects_calendar_query<C: CalendarStore>(
pub async fn handle_calendar_query<C: CalendarStore>(
cal_query: &CalendarQueryRequest,
props: &[&str],
req: HttpRequest,
path: &str,
puri: &impl PrincipalUri,
user: &User,
principal: &str,
cal_id: &str,
@@ -227,17 +227,13 @@ pub async fn handle_calendar_query<C: CalendarStore>(
let mut responses = Vec::new();
for object in objects {
let path = format!(
"{}/{}.ics",
req.path().trim_end_matches('/'),
object.get_id()
);
let path = format!("{}/{}.ics", path, object.get_id());
responses.push(
CalendarObjectResource {
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::{CalDavPrincipalUri, Error};
use actix_web::{
HttpRequest, Responder,
web::{Data, Path},
@@ -87,6 +87,7 @@ pub async fn route_report_calendar<C: CalendarStore>(
body: String,
user: User,
req: HttpRequest,
puri: Data<CalDavPrincipalUri>,
cal_store: Data<C>,
) -> Result<impl Responder, Error> {
let (principal, cal_id) = path.into_inner();
@@ -102,7 +103,8 @@ pub async fn route_report_calendar<C: CalendarStore>(
handle_calendar_query(
cal_query,
&props,
req,
req.path(),
puri.as_ref(),
&user,
&principal,
&cal_id,
@@ -114,7 +116,8 @@ pub async fn route_report_calendar<C: CalendarStore>(
handle_calendar_multiget(
cal_multiget,
&props,
req,
req.path(),
puri.as_ref(),
&user,
&principal,
&cal_id,
@@ -126,7 +129,8 @@ pub async fn route_report_calendar<C: CalendarStore>(
handle_sync_collection(
sync_collection,
&props,
req,
req.path(),
puri.as_ref(),
&user,
&principal,
&cal_id,

View File

@@ -3,9 +3,9 @@ use crate::{
Error,
calendar_object::resource::{CalendarObjectPropWrapper, CalendarObjectResource},
};
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,
},
@@ -19,7 +19,8 @@ use rustical_store::{
pub async fn handle_sync_collection<C: CalendarStore>(
sync_collection: &SyncCollectionRequest<ReportPropName>,
props: &[&str],
req: HttpRequest,
path: &str,
puri: &impl PrincipalUri,
user: &User,
principal: &str,
cal_id: &str,
@@ -32,22 +33,18 @@ pub async fn handle_sync_collection<C: CalendarStore>(
let mut responses = Vec::new();
for object in new_objects {
let path = format!(
"{}/{}.ics",
req.path().trim_end_matches('/'),
object.get_id()
);
let path = format!("{}/{}.ics", path, object.get_id());
responses.push(
CalendarObjectResource {
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!("{}/{}.ics", req.path().trim_end_matches('/'), object_id);
let path = format!("{path}/{object_id}.ics");
responses.push(ResponseElement {
href: path,
status: Some(StatusCode::NOT_FOUND),

View File

@@ -2,10 +2,8 @@ use super::methods::mkcalendar::route_mkcalendar;
use super::methods::post::route_post;
use super::methods::report::route_report_calendar;
use super::prop::{SupportedCalendarComponentSet, SupportedCalendarData, SupportedReportSet};
use crate::Error;
use crate::calendar_object::resource::CalendarObjectResource;
use crate::principal::PrincipalResource;
use actix_web::dev::ResourceMap;
use crate::{CalDavPrincipalUri, Error};
use actix_web::http::Method;
use actix_web::web;
use async_trait::async_trait;
@@ -15,7 +13,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::{HrefElement, Resourcetype, ResourcetypeInner};
use rustical_dav_push::{DavPushExtension, DavPushExtensionProp};
use rustical_ical::CalDateTime;
@@ -100,10 +98,6 @@ impl DavPushExtension for CalendarResource {
}
}
impl CommonPropertiesExtension for CalendarResource {
type PrincipalResource = PrincipalResource;
}
impl Resource for CalendarResource {
type Prop = CalendarPropWrapper;
type Error = Error;
@@ -128,7 +122,7 @@ impl Resource for CalendarResource {
fn get_prop(
&self,
rmap: &ResourceMap,
puri: &impl PrincipalUri,
user: &User,
prop: &CalendarPropWrapperName,
) -> Result<Self::Prop, Self::Error> {
@@ -183,7 +177,7 @@ impl Resource for CalendarResource {
CalendarPropWrapper::DavPush(DavPushExtension::get_prop(self, prop)?)
}
CalendarPropWrapperName::Common(prop) => CalendarPropWrapper::Common(
CommonPropertiesExtension::get_prop(self, rmap, user, prop)?,
CommonPropertiesExtension::get_prop(self, puri, user, prop)?,
),
})
}
@@ -336,6 +330,7 @@ impl<C: CalendarStore, S: SubscriptionStore> ResourceService for CalendarResourc
type Resource = CalendarResource;
type Error = Error;
type Principal = User;
type PrincipalUri = CalDavPrincipalUri;
async fn get_resource(
&self,

View File

@@ -1,12 +1,11 @@
use super::methods::{get_event, put_event};
use crate::{Error, principal::PrincipalResource};
use actix_web::dev::ResourceMap;
use crate::{CalDavPrincipalUri, Error};
use async_trait::async_trait;
use derive_more::derive::{From, Into};
use rustical_dav::{
extensions::{CommonPropertiesExtension, CommonPropertiesProp},
privileges::UserPrivilegeSet,
resource::{Resource, ResourceService},
resource::{PrincipalUri, Resource, ResourceService},
xml::Resourcetype,
};
use rustical_store::{CalendarObject, CalendarStore, auth::User};
@@ -51,10 +50,6 @@ pub struct CalendarObjectResource {
pub principal: String,
}
impl CommonPropertiesExtension for CalendarObjectResource {
type PrincipalResource = PrincipalResource;
}
impl Resource for CalendarObjectResource {
type Prop = CalendarObjectPropWrapper;
type Error = Error;
@@ -66,7 +61,7 @@ impl Resource for CalendarObjectResource {
fn get_prop(
&self,
rmap: &ResourceMap,
puri: &impl PrincipalUri,
user: &User,
prop: &CalendarObjectPropWrapperName,
) -> Result<Self::Prop, Self::Error> {
@@ -85,7 +80,7 @@ impl Resource for CalendarObjectResource {
})
}
CalendarObjectPropWrapperName::Common(prop) => CalendarObjectPropWrapper::Common(
CommonPropertiesExtension::get_prop(self, rmap, user, prop)?,
CommonPropertiesExtension::get_prop(self, puri, user, prop)?,
),
})
}
@@ -119,6 +114,7 @@ impl<C: CalendarStore> ResourceService for CalendarObjectResourceService<C> {
type MemberType = CalendarObjectResource;
type Error = Error;
type Principal = User;
type PrincipalUri = CalDavPrincipalUri;
async fn get_resource(
&self,

View File

@@ -1,11 +1,9 @@
use crate::Error;
use crate::calendar::resource::CalendarResource;
use crate::principal::PrincipalResource;
use actix_web::dev::ResourceMap;
use crate::{CalDavPrincipalUri, Error};
use async_trait::async_trait;
use rustical_dav::extensions::{CommonPropertiesExtension, CommonPropertiesProp};
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_store::CalendarStore;
use rustical_store::auth::User;
@@ -24,10 +22,6 @@ pub enum PrincipalPropWrapper {
Common(CommonPropertiesProp),
}
impl CommonPropertiesExtension for CalendarSetResource {
type PrincipalResource = PrincipalResource;
}
impl Resource for CalendarSetResource {
type Prop = PrincipalPropWrapper;
type Error = Error;
@@ -42,13 +36,13 @@ impl Resource for CalendarSetResource {
fn get_prop(
&self,
rmap: &ResourceMap,
puri: &impl PrincipalUri,
user: &User,
prop: &PrincipalPropWrapperName,
) -> Result<Self::Prop, Self::Error> {
Ok(match prop {
PrincipalPropWrapperName::Common(prop) => PrincipalPropWrapper::Common(
<Self as CommonPropertiesExtension>::get_prop(self, rmap, user, prop)?,
<Self as CommonPropertiesExtension>::get_prop(self, puri, user, prop)?,
),
})
}
@@ -83,6 +77,7 @@ impl<C: CalendarStore> ResourceService for CalendarSetResourceService<C> {
type Resource = CalendarSetResource;
type Error = Error;
type Principal = User;
type PrincipalUri = CalDavPrincipalUri;
async fn get_resource(
&self,

View File

@@ -8,8 +8,9 @@ use actix_web::web::{self, Data};
use calendar::resource::CalendarResourceService;
use calendar_object::resource::CalendarObjectResourceService;
use calendar_set::CalendarSetResourceService;
use derive_more::Constructor;
use principal::{PrincipalResource, PrincipalResourceService};
use rustical_dav::resource::{NamedRoute, ResourceService, ResourceServiceRoute};
use rustical_dav::resource::{PrincipalUri, ResourceService, ResourceServiceRoute};
use rustical_dav::resources::RootResourceService;
use rustical_store::auth::{AuthenticationMiddleware, AuthenticationProvider, User};
use rustical_store::{AddressbookStore, CalendarStore, ContactBirthdayStore, SubscriptionStore};
@@ -25,6 +26,15 @@ mod subscription;
pub use error::Error;
#[derive(Debug, Clone, Constructor)]
pub struct CalDavPrincipalUri(&'static str);
impl PrincipalUri for CalDavPrincipalUri {
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> {
@@ -57,6 +67,7 @@ pub fn caldav_service<
C: CalendarStore,
S: SubscriptionStore,
>(
prefix: &'static str,
auth_provider: Arc<AP>,
store: Arc<C>,
addr_store: Arc<AS>,
@@ -70,7 +81,13 @@ pub fn caldav_service<
.app_data(Data::from(store.clone()))
.app_data(Data::from(birthday_store.clone()))
.app_data(Data::from(subscription_store))
.service(RootResourceService::<PrincipalResource, User>::default().actix_resource())
.app_data(Data::new(CalDavPrincipalUri::new(
format!("{prefix}/principal").leak(),
)))
.service(
RootResourceService::<PrincipalResource, User, CalDavPrincipalUri>::default()
.actix_resource(),
)
.service(
web::scope("/principal").service(
web::scope("/{principal}")
@@ -79,8 +96,7 @@ pub fn caldav_service<
auth_provider,
home_set: &[("calendar", false), ("birthdays", true)],
}
.actix_resource()
.name(PrincipalResource::route_name()),
.actix_resource(),
)
.service(
web::scope("/calendar")

View File

@@ -1,16 +1,14 @@
use std::sync::Arc;
use crate::Error;
use crate::calendar_set::CalendarSetResource;
use actix_web::dev::ResourceMap;
use crate::{CalDavPrincipalUri, 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::auth::user::PrincipalType;
use rustical_store::auth::{AuthenticationProvider, User};
use rustical_xml::{EnumUnitVariants, EnumVariants, XmlDeserialize, XmlSerialize};
use std::sync::Arc;
#[derive(Clone)]
pub struct PrincipalResource {
@@ -49,22 +47,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 {
"caldav_principal"
}
}
impl CommonPropertiesExtension for PrincipalResource {
type PrincipalResource = Self;
}
impl Resource for PrincipalResource {
type Prop = PrincipalPropWrapper;
type Error = Error;
@@ -79,16 +61,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_url = Self::get_url(rmap, vec![&self.principal.id]).unwrap();
let principal_url = puri.principal_uri(&self.principal.id);
let home_set = CalendarHomeSet(
user.memberships()
.into_iter()
.map(|principal| Self::get_url(rmap, vec![principal]).unwrap())
.map(|principal| puri.principal_uri(principal))
.flat_map(|principal_url| {
self.home_set.iter().map(move |&(home_name, _read_only)| {
HrefElement::new(format!("{}/{}", &principal_url, home_name))
@@ -119,7 +101,7 @@ impl Resource for PrincipalResource {
})
}
PrincipalPropWrapperName::Common(prop) => PrincipalPropWrapper::Common(
<Self as CommonPropertiesExtension>::get_prop(self, rmap, user, prop)?,
<Self as CommonPropertiesExtension>::get_prop(self, puri, user, prop)?,
),
})
}
@@ -147,6 +129,7 @@ impl<AP: AuthenticationProvider> ResourceService for PrincipalResourceService<AP
type Resource = PrincipalResource;
type Error = Error;
type Principal = User;
type PrincipalUri = CalDavPrincipalUri;
async fn get_resource(
&self,