Reorganise resource methods

This commit is contained in:
Lennart
2024-10-04 20:02:14 +02:00
parent 6bc1ac6a7d
commit b710dd9df6
5 changed files with 44 additions and 35 deletions

View File

@@ -1,3 +1,5 @@
use super::methods::mkcalendar::route_mkcalendar;
use super::methods::report::route_report_calendar;
use super::prop::{
Resourcetype, SupportedCalendarComponent, SupportedCalendarComponentSet, SupportedCalendarData,
SupportedReportSet, UserPrivilegeSet,
@@ -6,6 +8,8 @@ use crate::calendar_object::resource::CalendarObjectResource;
use crate::principal::PrincipalResource;
use crate::Error;
use actix_web::dev::ResourceMap;
use actix_web::http::Method;
use actix_web::web;
use actix_web::{web::Data, HttpRequest};
use async_trait::async_trait;
use derive_more::derive::{From, Into};
@@ -14,6 +18,7 @@ use rustical_dav::xml::HrefElement;
use rustical_store::model::Calendar;
use rustical_store::CalendarStore;
use serde::{Deserialize, Serialize};
use std::str::FromStr;
use std::sync::Arc;
use strum::{EnumString, VariantNames};
use tokio::sync::RwLock;
@@ -316,4 +321,13 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarResourceService<C> {
.await?;
Ok(())
}
#[inline]
fn actix_additional_routes(res: actix_web::Resource) -> actix_web::Resource {
let report_method = || web::method(Method::from_str("REPORT").unwrap());
let mkcalendar_method = || web::method(Method::from_str("MKCALENDAR").unwrap());
res.route(report_method().to(route_report_calendar::<C>))
.route(mkcalendar_method().to(route_mkcalendar::<C>))
}
}

View File

@@ -8,11 +8,15 @@ use actix_web::HttpResponse;
use rustical_store::auth::User;
use rustical_store::model::CalendarObject;
use rustical_store::CalendarStore;
use tracing::instrument;
use tracing_actix_web::RootSpan;
#[instrument(parent = root_span.id(), skip(context, path, root_span))]
pub async fn get_event<C: CalendarStore + ?Sized>(
context: Data<CalDavContext<C>>,
path: Path<(String, String, String)>,
user: User,
root_span: RootSpan,
) -> Result<HttpResponse, Error> {
let (principal, cid, mut uid) = path.into_inner();
@@ -46,12 +50,14 @@ pub async fn get_event<C: CalendarStore + ?Sized>(
.body(event.get_ics().to_owned()))
}
#[instrument(parent = root_span.id(), skip(context, path, req, root_span))]
pub async fn put_event<C: CalendarStore + ?Sized>(
context: Data<CalDavContext<C>>,
path: Path<(String, String, String)>,
body: String,
user: User,
req: HttpRequest,
root_span: RootSpan,
) -> Result<HttpResponse, Error> {
let (principal, cid, mut uid) = path.into_inner();
if user.id != principal {

View File

@@ -10,6 +10,8 @@ use std::sync::Arc;
use strum::{EnumString, VariantNames};
use tokio::sync::RwLock;
use super::methods::{get_event, put_event};
pub struct CalendarObjectResourceService<C: CalendarStore + ?Sized> {
pub cal_store: Arc<RwLock<C>>,
pub path: String,
@@ -130,4 +132,9 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarObjectResourceServic
.await?;
Ok(())
}
#[inline]
fn actix_additional_routes(res: actix_web::Resource) -> actix_web::Resource {
res.get(get_event::<C>).put(put_event::<C>)
}
}

View File

@@ -5,11 +5,9 @@ use calendar::resource::CalendarResourceService;
use calendar_object::resource::CalendarObjectResourceService;
use principal::PrincipalResourceService;
use root::RootResourceService;
use rustical_dav::methods::route_delete;
use rustical_dav::resource::ResourceService;
use rustical_store::auth::{AuthenticationMiddleware, AuthenticationProvider};
use rustical_store::CalendarStore;
use std::str::FromStr;
use std::sync::Arc;
use tokio::sync::RwLock;
@@ -34,9 +32,6 @@ pub fn configure_dav<AP: AuthenticationProvider, C: CalendarStore + ?Sized>(
auth_provider: Arc<AP>,
store: Arc<RwLock<C>>,
) {
let report_method = || web::method(Method::from_str("REPORT").unwrap());
let mkcalendar_method = || web::method(Method::from_str("MKCALENDAR").unwrap());
cfg.service(
web::scope("")
.wrap(AuthenticationMiddleware::new(auth_provider))
@@ -57,33 +52,10 @@ pub fn configure_dav<AP: AuthenticationProvider, C: CalendarStore + ?Sized>(
.service(PrincipalResourceService::<C>::actix_resource())
.service(
web::scope("/{calendar}")
.service(
CalendarResourceService::<C>::actix_resource()
.route(report_method().to(
calendar::methods::report::route_report_calendar::<C>,
))
.route(
web::method(Method::DELETE)
.to(route_delete::<CalendarResourceService<C>>),
)
.route(mkcalendar_method().to(
calendar::methods::mkcalendar::route_mkcalendar::<C>,
)),
)
.service(CalendarResourceService::<C>::actix_resource())
.service(
web::scope("/{object}").service(
CalendarObjectResourceService::<C>::actix_resource()
.route(web::method(Method::DELETE).to(route_delete::<
CalendarObjectResourceService<C>,
>))
.route(
web::method(Method::GET)
.to(calendar_object::methods::get_event::<C>),
)
.route(
web::method(Method::PUT)
.to(calendar_object::methods::put_event::<C>),
),
CalendarObjectResourceService::<C>::actix_resource(),
),
),
),

View File

@@ -1,4 +1,4 @@
use crate::methods::{route_propfind, route_proppatch};
use crate::methods::{route_delete, route_propfind, route_proppatch};
use crate::xml::multistatus::{PropTagWrapper, PropstatElement, PropstatWrapper};
use crate::xml::{multistatus::ResponseElement, TagList};
use crate::Error;
@@ -164,17 +164,27 @@ pub trait ResourceService: Sized + 'static {
Err(crate::Error::Unauthorized.into())
}
#[inline]
fn resource_name() -> &'static str {
Self::Resource::resource_name()
}
#[inline]
fn actix_resource() -> actix_web::Resource {
let propfind_method = || web::method(Method::from_str("PROPFIND").unwrap());
let proppatch_method = || web::method(Method::from_str("PROPPATCH").unwrap());
web::resource("")
.name(Self::resource_name())
.route(propfind_method().to(route_propfind::<Self>))
.route(proppatch_method().to(route_proppatch::<Self>))
Self::actix_additional_routes(
web::resource("")
.name(Self::resource_name())
.route(propfind_method().to(route_propfind::<Self>))
.route(proppatch_method().to(route_proppatch::<Self>))
.delete(route_delete::<Self>),
)
}
#[inline]
fn actix_additional_routes(res: actix_web::Resource) -> actix_web::Resource {
res
}
}