From 6e5b9028e0197d135ffa1f87329592d35452c72e Mon Sep 17 00:00:00 2001 From: Lennart <18233294+lennart-k@users.noreply.github.com> Date: Wed, 13 Sep 2023 19:20:58 +0200 Subject: [PATCH] Add props to EventResource --- crates/dav/src/lib.rs | 1 + crates/dav/src/resources/event.rs | 50 ++++++++++++++++++++++++------- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/crates/dav/src/lib.rs b/crates/dav/src/lib.rs index 90cb993..cd94ede 100644 --- a/crates/dav/src/lib.rs +++ b/crates/dav/src/lib.rs @@ -86,6 +86,7 @@ pub fn configure_dav( .service( web::resource("/{principal}/{calendar}/{event}") .route(web::method(propfind_method()).to(route_new_propfind::)) + .route(web::method(propfind_method()).to(route_new_propfind::, C>)) .route(web::method(Method::DELETE).to(event::delete_event::)) .route(web::method(Method::GET).to(event::get_event::)) .route(web::method(Method::PUT).to(event::put_event::)), diff --git a/crates/dav/src/resources/event.rs b/crates/dav/src/resources/event.rs index dd2201e..2e6c3aa 100644 --- a/crates/dav/src/resources/event.rs +++ b/crates/dav/src/resources/event.rs @@ -1,15 +1,21 @@ -use crate::resource::Resource; -use actix_web::HttpRequest; +use std::sync::Arc; + +use crate::{proptypes::write_string_prop, resource::Resource}; +use actix_web::{web::Data, HttpRequest}; use anyhow::{anyhow, Result}; use async_trait::async_trait; use rustical_auth::AuthInfo; +use rustical_store::calendar::{CalendarStore, Event}; +use tokio::sync::RwLock; -pub struct EventResource { - path: String, +pub struct EventResource { + pub cal_store: Arc>, + pub path: String, + pub event: Event, } #[async_trait(?Send)] -impl Resource for EventResource { +impl Resource for EventResource { type UriComponents = (String, String, String); // principal, calendar, event type MemberType = Self; @@ -24,23 +30,47 @@ impl Resource for EventResource { async fn acquire_from_request( req: HttpRequest, _auth_info: AuthInfo, - _uri_components: Self::UriComponents, + uri_components: Self::UriComponents, _prefix: String, ) -> Result { + let (_principal, cid, uid) = uri_components; + + let cal_store = req + .app_data::>>() + .ok_or(anyhow!("no calendar store in app_data!"))? + .clone() + .into_inner(); + + let event = cal_store.read().await.get_event(&cid, &uid).await?; + Ok(Self { + cal_store, + event, path: req.path().to_string(), }) } fn write_prop( &self, - _writer: &mut quick_xml::Writer, - _prop: &str, + writer: &mut quick_xml::Writer, + prop: &str, ) -> Result<()> { - Err(anyhow!("invalid prop!")) + match prop { + "getetag" => { + write_string_prop(writer, "getetag", &self.event.get_etag())?; + } + "calendar-data" => { + write_string_prop(writer, "C:calendar-data", self.event.to_ics())?; + } + "getcontenttype" => { + write_string_prop(writer, "getcontenttype", "text/calendar;charset=utf-8")?; + } + _ => return Err(anyhow!("invalid prop!")), + }; + Ok(()) } fn list_dead_props() -> Vec<&'static str> { - vec![] + vec!["getetag", "calendar-data", "getcontenttype"] } }