mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 10:32:19 +00:00
Migrate from Event type to CalendarObject
This is preparation to support other calendar components like VTODO and VJOURNAL
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use crate::{
|
||||
event::resource::{EventProp, EventResource},
|
||||
calendar_object::resource::{CalendarObjectProp, CalendarObjectResource},
|
||||
Error,
|
||||
};
|
||||
use actix_web::{
|
||||
@@ -11,7 +11,7 @@ use rustical_dav::{
|
||||
resource::HandlePropfind,
|
||||
xml::{multistatus::PropstatWrapper, MultistatusElement},
|
||||
};
|
||||
use rustical_store::{model::Event, CalendarStore};
|
||||
use rustical_store::{model::object::CalendarObject, CalendarStore};
|
||||
use serde::Deserialize;
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
@@ -31,7 +31,7 @@ pub async fn get_events_calendar_multiget<C: CalendarStore + ?Sized>(
|
||||
principal: &str,
|
||||
cid: &str,
|
||||
store: &RwLock<C>,
|
||||
) -> Result<Vec<Event>, Error> {
|
||||
) -> Result<Vec<CalendarObject>, Error> {
|
||||
// TODO: add proper error results for single events
|
||||
let resource_def =
|
||||
ResourceDef::prefix(prefix).join(&ResourceDef::new("/user/{principal}/{cid}/{uid}"));
|
||||
@@ -54,7 +54,7 @@ pub async fn get_events_calendar_multiget<C: CalendarStore + ?Sized>(
|
||||
continue;
|
||||
}
|
||||
let uid = path.get("uid").unwrap();
|
||||
result.push(store.get_event(principal, cid, uid).await?);
|
||||
result.push(store.get_object(principal, cid, uid).await?);
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
@@ -67,7 +67,7 @@ pub async fn handle_calendar_multiget<C: CalendarStore + ?Sized>(
|
||||
principal: &str,
|
||||
cid: &str,
|
||||
cal_store: &RwLock<C>,
|
||||
) -> Result<MultistatusElement<PropstatWrapper<EventProp>, String>, Error> {
|
||||
) -> Result<MultistatusElement<PropstatWrapper<CalendarObjectProp>, String>, Error> {
|
||||
let events =
|
||||
get_events_calendar_multiget(&cal_multiget, prefix, principal, cid, cal_store).await?;
|
||||
|
||||
@@ -87,7 +87,7 @@ pub async fn handle_calendar_multiget<C: CalendarStore + ?Sized>(
|
||||
for event in events {
|
||||
let path = format!("{}/{}", req.path(), event.get_uid());
|
||||
responses.push(
|
||||
EventResource::from(event)
|
||||
CalendarObjectResource::from(event)
|
||||
.propfind(prefix, &path, props.clone())
|
||||
.await?,
|
||||
);
|
||||
|
||||
@@ -4,12 +4,12 @@ use rustical_dav::{
|
||||
resource::HandlePropfind,
|
||||
xml::{multistatus::PropstatWrapper, MultistatusElement},
|
||||
};
|
||||
use rustical_store::{model::Event, CalendarStore};
|
||||
use rustical_store::{model::object::CalendarObject, CalendarStore};
|
||||
use serde::Deserialize;
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
use crate::{
|
||||
event::resource::{EventProp, EventResource},
|
||||
calendar_object::resource::{CalendarObjectProp, CalendarObjectResource},
|
||||
Error,
|
||||
};
|
||||
|
||||
@@ -95,9 +95,9 @@ pub async fn get_events_calendar_query<C: CalendarStore + ?Sized>(
|
||||
principal: &str,
|
||||
cid: &str,
|
||||
store: &RwLock<C>,
|
||||
) -> Result<Vec<Event>, Error> {
|
||||
) -> Result<Vec<CalendarObject>, Error> {
|
||||
// TODO: Implement filtering
|
||||
Ok(store.read().await.get_events(principal, cid).await?)
|
||||
Ok(store.read().await.get_objects(principal, cid).await?)
|
||||
}
|
||||
|
||||
pub async fn handle_calendar_query<C: CalendarStore + ?Sized>(
|
||||
@@ -107,7 +107,7 @@ pub async fn handle_calendar_query<C: CalendarStore + ?Sized>(
|
||||
principal: &str,
|
||||
cid: &str,
|
||||
cal_store: &RwLock<C>,
|
||||
) -> Result<MultistatusElement<PropstatWrapper<EventProp>, String>, Error> {
|
||||
) -> Result<MultistatusElement<PropstatWrapper<CalendarObjectProp>, String>, Error> {
|
||||
let events = get_events_calendar_query(&cal_query, principal, cid, cal_store).await?;
|
||||
|
||||
let props = match cal_query.prop {
|
||||
@@ -126,7 +126,7 @@ pub async fn handle_calendar_query<C: CalendarStore + ?Sized>(
|
||||
for event in events {
|
||||
let path = format!("{}/{}", req.path(), event.get_uid());
|
||||
responses.push(
|
||||
EventResource::from(event)
|
||||
CalendarObjectResource::from(event)
|
||||
.propfind(prefix, &path, props.clone())
|
||||
.await?,
|
||||
);
|
||||
|
||||
@@ -15,7 +15,7 @@ use serde::Deserialize;
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
use crate::{
|
||||
event::resource::{EventProp, EventResource},
|
||||
calendar_object::resource::{CalendarObjectProp, CalendarObjectResource},
|
||||
Error,
|
||||
};
|
||||
|
||||
@@ -49,7 +49,7 @@ pub async fn handle_sync_collection<C: CalendarStore + ?Sized>(
|
||||
principal: &str,
|
||||
cid: &str,
|
||||
cal_store: &RwLock<C>,
|
||||
) -> Result<MultistatusElement<PropstatWrapper<EventProp>, String>, Error> {
|
||||
) -> Result<MultistatusElement<PropstatWrapper<CalendarObjectProp>, String>, Error> {
|
||||
let props = match sync_collection.prop {
|
||||
PropfindType::Allprop => {
|
||||
vec!["allprop".to_owned()]
|
||||
@@ -73,7 +73,7 @@ pub async fn handle_sync_collection<C: CalendarStore + ?Sized>(
|
||||
for event in new_events {
|
||||
let path = format!("{}/{}", req.path(), event.get_uid());
|
||||
responses.push(
|
||||
EventResource::from(event)
|
||||
CalendarObjectResource::from(event)
|
||||
.propfind(prefix, &path, props.clone())
|
||||
.await?,
|
||||
);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::event::resource::EventResource;
|
||||
use crate::calendar_object::resource::CalendarObjectResource;
|
||||
use crate::Error;
|
||||
use actix_web::{web::Data, HttpRequest};
|
||||
use async_trait::async_trait;
|
||||
@@ -218,7 +218,7 @@ impl Resource for CalendarResource {
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl<C: CalendarStore + ?Sized> ResourceService for CalendarResourceService<C> {
|
||||
type MemberType = EventResource;
|
||||
type MemberType = CalendarObjectResource;
|
||||
type PathComponents = (String, String); // principal, calendar_id
|
||||
type Resource = CalendarResource;
|
||||
type Error = Error;
|
||||
@@ -243,7 +243,7 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarResourceService<C> {
|
||||
.cal_store
|
||||
.read()
|
||||
.await
|
||||
.get_events(&self.principal, &self.calendar_id)
|
||||
.get_objects(&self.principal, &self.calendar_id)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|event| (format!("{}/{}", self.path, &event.get_uid()), event.into()))
|
||||
|
||||
@@ -36,7 +36,7 @@ pub async fn get_event<A: CheckAuthentication, C: CalendarStore + ?Sized>(
|
||||
.store
|
||||
.read()
|
||||
.await
|
||||
.get_event(&principal, &cid, &uid)
|
||||
.get_object(&principal, &cid, &uid)
|
||||
.await?;
|
||||
|
||||
Ok(HttpResponse::Ok()
|
||||
@@ -79,7 +79,7 @@ pub async fn put_event<A: CheckAuthentication, C: CalendarStore + ?Sized>(
|
||||
|
||||
if Some(&HeaderValue::from_static("*")) == req.headers().get(header::IF_NONE_MATCH) {
|
||||
// Only write if not existing
|
||||
match store.get_event(&principal, &cid, &uid).await {
|
||||
match store.get_object(&principal, &cid, &uid).await {
|
||||
Ok(_) => {
|
||||
// Conflict
|
||||
return Ok(HttpResponse::Conflict().body("Resource with this URI already existing"));
|
||||
@@ -94,7 +94,7 @@ pub async fn put_event<A: CheckAuthentication, C: CalendarStore + ?Sized>(
|
||||
}
|
||||
}
|
||||
|
||||
store.put_event(principal, cid, uid, body).await?;
|
||||
store.put_object(principal, cid, uid, body).await?;
|
||||
|
||||
Ok(HttpResponse::Created().body(""))
|
||||
}
|
||||
@@ -3,14 +3,14 @@ use actix_web::{web::Data, HttpRequest};
|
||||
use async_trait::async_trait;
|
||||
use derive_more::derive::{From, Into};
|
||||
use rustical_dav::resource::{InvalidProperty, Resource, ResourceService};
|
||||
use rustical_store::model::Event;
|
||||
use rustical_store::model::object::CalendarObject;
|
||||
use rustical_store::CalendarStore;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::sync::Arc;
|
||||
use strum::{EnumString, VariantNames};
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
pub struct EventResourceService<C: CalendarStore + ?Sized> {
|
||||
pub struct CalendarObjectResourceService<C: CalendarStore + ?Sized> {
|
||||
pub cal_store: Arc<RwLock<C>>,
|
||||
pub path: String,
|
||||
pub principal: String,
|
||||
@@ -20,7 +20,7 @@ pub struct EventResourceService<C: CalendarStore + ?Sized> {
|
||||
|
||||
#[derive(EnumString, Debug, VariantNames, Clone)]
|
||||
#[strum(serialize_all = "kebab-case")]
|
||||
pub enum EventPropName {
|
||||
pub enum CalendarObjectPropName {
|
||||
Getetag,
|
||||
CalendarData,
|
||||
Getcontenttype,
|
||||
@@ -28,7 +28,7 @@ pub enum EventPropName {
|
||||
|
||||
#[derive(Deserialize, Serialize, Debug, Clone)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
pub enum EventProp {
|
||||
pub enum CalendarObjectProp {
|
||||
Getetag(String),
|
||||
#[serde(rename = "C:calendar-data")]
|
||||
CalendarData(String),
|
||||
@@ -37,36 +37,38 @@ pub enum EventProp {
|
||||
Invalid,
|
||||
}
|
||||
|
||||
impl InvalidProperty for EventProp {
|
||||
impl InvalidProperty for CalendarObjectProp {
|
||||
fn invalid_property(&self) -> bool {
|
||||
matches!(self, Self::Invalid)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, From, Into)]
|
||||
pub struct EventResource(Event);
|
||||
pub struct CalendarObjectResource(CalendarObject);
|
||||
|
||||
impl Resource for EventResource {
|
||||
type PropName = EventPropName;
|
||||
type Prop = EventProp;
|
||||
impl Resource for CalendarObjectResource {
|
||||
type PropName = CalendarObjectPropName;
|
||||
type Prop = CalendarObjectProp;
|
||||
type Error = Error;
|
||||
|
||||
fn get_prop(&self, _prefix: &str, prop: Self::PropName) -> Result<Self::Prop, Self::Error> {
|
||||
Ok(match prop {
|
||||
EventPropName::Getetag => EventProp::Getetag(self.0.get_etag()),
|
||||
EventPropName::CalendarData => EventProp::CalendarData(self.0.get_ics().to_owned()),
|
||||
EventPropName::Getcontenttype => {
|
||||
EventProp::Getcontenttype("text/calendar;charset=utf-8".to_owned())
|
||||
CalendarObjectPropName::Getetag => CalendarObjectProp::Getetag(self.0.get_etag()),
|
||||
CalendarObjectPropName::CalendarData => {
|
||||
CalendarObjectProp::CalendarData(self.0.get_ics().to_owned())
|
||||
}
|
||||
CalendarObjectPropName::Getcontenttype => {
|
||||
CalendarObjectProp::Getcontenttype("text/calendar;charset=utf-8".to_owned())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait(?Send)]
|
||||
impl<C: CalendarStore + ?Sized> ResourceService for EventResourceService<C> {
|
||||
impl<C: CalendarStore + ?Sized> ResourceService for CalendarObjectResourceService<C> {
|
||||
type PathComponents = (String, String, String); // principal, calendar, event
|
||||
type Resource = EventResource;
|
||||
type MemberType = EventResource;
|
||||
type Resource = CalendarObjectResource;
|
||||
type MemberType = CalendarObjectResource;
|
||||
type Error = Error;
|
||||
|
||||
async fn new(
|
||||
@@ -102,7 +104,7 @@ impl<C: CalendarStore + ?Sized> ResourceService for EventResourceService<C> {
|
||||
.cal_store
|
||||
.read()
|
||||
.await
|
||||
.get_event(&self.principal, &self.cid, &self.uid)
|
||||
.get_object(&self.principal, &self.cid, &self.uid)
|
||||
.await?;
|
||||
Ok(event.into())
|
||||
}
|
||||
@@ -115,7 +117,7 @@ impl<C: CalendarStore + ?Sized> ResourceService for EventResourceService<C> {
|
||||
self.cal_store
|
||||
.write()
|
||||
.await
|
||||
.delete_event(&self.principal, &self.cid, &self.uid, use_trashbin)
|
||||
.delete_object(&self.principal, &self.cid, &self.uid, use_trashbin)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -2,7 +2,7 @@ use actix_web::http::Method;
|
||||
use actix_web::web::{self, Data};
|
||||
use actix_web::{guard, HttpResponse, Responder};
|
||||
use calendar::resource::CalendarResourceService;
|
||||
use event::resource::EventResourceService;
|
||||
use calendar_object::resource::CalendarObjectResourceService;
|
||||
use principal::PrincipalResourceService;
|
||||
use root::RootResourceService;
|
||||
use rustical_auth::CheckAuthentication;
|
||||
@@ -15,8 +15,8 @@ use std::sync::Arc;
|
||||
use tokio::sync::RwLock;
|
||||
|
||||
pub mod calendar;
|
||||
pub mod calendar_object;
|
||||
pub mod error;
|
||||
pub mod event;
|
||||
pub mod principal;
|
||||
pub mod root;
|
||||
|
||||
@@ -102,21 +102,23 @@ pub fn configure_dav<A: CheckAuthentication, C: CalendarStore + ?Sized>(
|
||||
web::resource("/{event}")
|
||||
.route(
|
||||
propfind_method()
|
||||
.to(route_propfind::<A, EventResourceService<C>>),
|
||||
.to(route_propfind::<A, CalendarObjectResourceService<C>>),
|
||||
)
|
||||
.route(
|
||||
proppatch_method()
|
||||
.to(route_proppatch::<A, EventResourceService<C>>),
|
||||
.to(route_proppatch::<A, CalendarObjectResourceService<C>>),
|
||||
)
|
||||
.route(
|
||||
web::method(Method::DELETE)
|
||||
.to(route_delete::<A, EventResourceService<C>>),
|
||||
.to(route_delete::<A, CalendarObjectResourceService<C>>),
|
||||
)
|
||||
.route(
|
||||
web::method(Method::GET).to(event::methods::get_event::<A, C>),
|
||||
web::method(Method::GET)
|
||||
.to(calendar_object::methods::get_event::<A, C>),
|
||||
)
|
||||
.route(
|
||||
web::method(Method::PUT).to(event::methods::put_event::<A, C>),
|
||||
web::method(Method::PUT)
|
||||
.to(calendar_object::methods::put_event::<A, C>),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user