mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 18:52:26 +00:00
Massive refactoring how DAV resources and routes work
This commit is contained in:
@@ -8,7 +8,7 @@ use actix_web::{
|
||||
};
|
||||
use rustical_dav::{
|
||||
methods::propfind::{PropElement, PropfindType},
|
||||
resource::HandlePropfind,
|
||||
resource::Resource,
|
||||
xml::{multistatus::PropstatWrapper, MultistatusElement},
|
||||
};
|
||||
use rustical_store::{model::object::CalendarObject, CalendarStore};
|
||||
@@ -88,7 +88,7 @@ pub async fn handle_calendar_multiget<C: CalendarStore + ?Sized>(
|
||||
let path = format!("{}/{}", req.path(), object.get_uid());
|
||||
responses.push(
|
||||
CalendarObjectResource::from(object)
|
||||
.propfind(prefix, &path, props.clone())
|
||||
.propfind(&path, props.clone(), req.resource_map())
|
||||
.await?,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use actix_web::HttpRequest;
|
||||
use rustical_dav::{
|
||||
methods::propfind::{PropElement, PropfindType},
|
||||
resource::HandlePropfind,
|
||||
resource::Resource,
|
||||
xml::{multistatus::PropstatWrapper, MultistatusElement},
|
||||
};
|
||||
use rustical_store::{model::object::CalendarObject, CalendarStore};
|
||||
@@ -176,7 +176,6 @@ pub async fn get_objects_calendar_query<C: CalendarStore + ?Sized>(
|
||||
pub async fn handle_calendar_query<C: CalendarStore + ?Sized>(
|
||||
cal_query: CalendarQueryRequest,
|
||||
req: HttpRequest,
|
||||
prefix: &str,
|
||||
principal: &str,
|
||||
cid: &str,
|
||||
cal_store: &RwLock<C>,
|
||||
@@ -197,10 +196,14 @@ pub async fn handle_calendar_query<C: CalendarStore + ?Sized>(
|
||||
|
||||
let mut responses = Vec::new();
|
||||
for object in objects {
|
||||
let path = format!("{}/{}", req.path(), object.get_uid());
|
||||
let path = CalendarObjectResource::get_url(
|
||||
req.resource_map(),
|
||||
vec![principal, cid, object.get_uid()],
|
||||
)
|
||||
.unwrap();
|
||||
responses.push(
|
||||
CalendarObjectResource::from(object)
|
||||
.propfind(prefix, &path, props.clone())
|
||||
.propfind(&path, props.clone(), req.resource_map())
|
||||
.await?,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -51,22 +51,14 @@ pub async fn route_report_calendar<C: CalendarStore + ?Sized>(
|
||||
|
||||
Ok(match request.clone() {
|
||||
ReportRequest::CalendarQuery(cal_query) => {
|
||||
handle_calendar_query(cal_query, req, &prefix, &principal, &cid, &cal_store).await?
|
||||
handle_calendar_query(cal_query, req, &principal, &cid, &cal_store).await?
|
||||
}
|
||||
ReportRequest::CalendarMultiget(cal_multiget) => {
|
||||
handle_calendar_multiget(cal_multiget, req, &prefix, &principal, &cid, &cal_store)
|
||||
.await?
|
||||
}
|
||||
ReportRequest::SyncCollection(sync_collection) => {
|
||||
handle_sync_collection(
|
||||
sync_collection,
|
||||
req,
|
||||
&prefix.0,
|
||||
&principal,
|
||||
&cid,
|
||||
&cal_store,
|
||||
)
|
||||
.await?
|
||||
handle_sync_collection(sync_collection, req, &principal, &cid, &cal_store).await?
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use actix_web::{http::StatusCode, HttpRequest};
|
||||
use rustical_dav::{
|
||||
methods::propfind::{PropElement, PropfindType},
|
||||
resource::HandlePropfind,
|
||||
resource::Resource,
|
||||
xml::{
|
||||
multistatus::{PropstatWrapper, ResponseElement},
|
||||
MultistatusElement,
|
||||
@@ -45,7 +45,6 @@ pub struct SyncCollectionRequest {
|
||||
pub async fn handle_sync_collection<C: CalendarStore + ?Sized>(
|
||||
sync_collection: SyncCollectionRequest,
|
||||
req: HttpRequest,
|
||||
prefix: &str,
|
||||
principal: &str,
|
||||
cid: &str,
|
||||
cal_store: &RwLock<C>,
|
||||
@@ -71,16 +70,22 @@ pub async fn handle_sync_collection<C: CalendarStore + ?Sized>(
|
||||
|
||||
let mut responses = Vec::new();
|
||||
for object in new_objects {
|
||||
let path = format!("{}/{}", req.path(), object.get_uid());
|
||||
let path = CalendarObjectResource::get_url(
|
||||
req.resource_map(),
|
||||
vec![principal, cid, &object.get_uid()],
|
||||
)
|
||||
.unwrap();
|
||||
responses.push(
|
||||
CalendarObjectResource::from(object)
|
||||
.propfind(prefix, &path, props.clone())
|
||||
.propfind(&path, props.clone(), req.resource_map())
|
||||
.await?,
|
||||
);
|
||||
}
|
||||
|
||||
for object_uid in deleted_objects {
|
||||
let path = format!("{}/{}", req.path(), object_uid);
|
||||
let path =
|
||||
CalendarObjectResource::get_url(req.resource_map(), vec![principal, cid, &object_uid])
|
||||
.unwrap();
|
||||
responses.push(ResponseElement {
|
||||
href: path,
|
||||
status: Some(format!("HTTP/1.1 {}", StatusCode::NOT_FOUND)),
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
use super::prop::{
|
||||
Resourcetype, SupportedCalendarComponent, SupportedCalendarComponentSet, SupportedCalendarData,
|
||||
SupportedReportSet, UserPrivilegeSet,
|
||||
};
|
||||
use crate::calendar_object::resource::CalendarObjectResource;
|
||||
use crate::principal::PrincipalResource;
|
||||
use crate::Error;
|
||||
use actix_web::dev::ResourceMap;
|
||||
use actix_web::{web::Data, HttpRequest};
|
||||
use async_trait::async_trait;
|
||||
use derive_more::derive::{From, Into};
|
||||
@@ -12,11 +18,6 @@ use std::sync::Arc;
|
||||
use strum::{EnumString, VariantNames};
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
use super::prop::{
|
||||
Resourcetype, SupportedCalendarComponent, SupportedCalendarComponentSet, SupportedCalendarData,
|
||||
SupportedReportSet, UserPrivilegeSet,
|
||||
};
|
||||
|
||||
pub struct CalendarResourceService<C: CalendarStore + ?Sized> {
|
||||
pub cal_store: Arc<RwLock<C>>,
|
||||
pub path: String,
|
||||
@@ -94,16 +95,21 @@ impl Resource for CalendarResource {
|
||||
type Prop = CalendarProp;
|
||||
type Error = Error;
|
||||
|
||||
fn get_prop(&self, prefix: &str, prop: Self::PropName) -> Result<Self::Prop, Self::Error> {
|
||||
fn get_prop(
|
||||
&self,
|
||||
rmap: &ResourceMap,
|
||||
prop: Self::PropName,
|
||||
) -> Result<Self::Prop, Self::Error> {
|
||||
Ok(match prop {
|
||||
CalendarPropName::Resourcetype => CalendarProp::Resourcetype(Resourcetype::default()),
|
||||
CalendarPropName::CurrentUserPrincipal => CalendarProp::CurrentUserPrincipal(
|
||||
HrefElement::new(format!("{}/user/{}/", prefix, self.0.principal)),
|
||||
),
|
||||
CalendarPropName::Owner => CalendarProp::Owner(HrefElement::new(format!(
|
||||
"{}/user/{}/",
|
||||
prefix, self.0.principal
|
||||
))),
|
||||
CalendarPropName::CurrentUserPrincipal => {
|
||||
CalendarProp::CurrentUserPrincipal(HrefElement::new(
|
||||
PrincipalResource::get_url(rmap, vec![&self.0.principal]).unwrap(),
|
||||
))
|
||||
}
|
||||
CalendarPropName::Owner => CalendarProp::Owner(HrefElement::new(
|
||||
PrincipalResource::get_url(rmap, vec![&self.0.principal]).unwrap(),
|
||||
)),
|
||||
CalendarPropName::Displayname => CalendarProp::Displayname(self.0.displayname.clone()),
|
||||
CalendarPropName::CalendarColor => CalendarProp::CalendarColor(self.0.color.clone()),
|
||||
CalendarPropName::CalendarDescription => {
|
||||
@@ -219,6 +225,11 @@ impl Resource for CalendarResource {
|
||||
CalendarPropName::Getctag => Err(rustical_dav::Error::PropReadOnly),
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn resource_name() -> &'static str {
|
||||
"caldav_calendar"
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
@@ -242,8 +253,10 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarResourceService<C> {
|
||||
Ok(calendar.into())
|
||||
}
|
||||
|
||||
async fn get_members(&self) -> Result<Vec<(String, Self::MemberType)>, Self::Error> {
|
||||
// As of now the calendar resource has no members since events are shown with REPORT
|
||||
async fn get_members(
|
||||
&self,
|
||||
rmap: &ResourceMap,
|
||||
) -> Result<Vec<(String, Self::MemberType)>, Self::Error> {
|
||||
Ok(self
|
||||
.cal_store
|
||||
.read()
|
||||
@@ -251,7 +264,16 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarResourceService<C> {
|
||||
.get_objects(&self.principal, &self.calendar_id)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|event| (format!("{}/{}", self.path, &event.get_uid()), event.into()))
|
||||
.map(|object| {
|
||||
(
|
||||
CalendarObjectResource::get_url(
|
||||
rmap,
|
||||
vec![&self.principal, &self.calendar_id, object.get_uid()],
|
||||
)
|
||||
.unwrap(),
|
||||
object.into(),
|
||||
)
|
||||
})
|
||||
.collect())
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user