Add props to EventResource

This commit is contained in:
Lennart
2023-09-13 19:20:58 +02:00
parent c351fc00ac
commit 6e5b9028e0
2 changed files with 41 additions and 10 deletions

View File

@@ -86,6 +86,7 @@ pub fn configure_dav<A: CheckAuthentication, C: CalendarStore>(
.service( .service(
web::resource("/{principal}/{calendar}/{event}") web::resource("/{principal}/{calendar}/{event}")
.route(web::method(propfind_method()).to(route_new_propfind::<A, EventResource, C>)) .route(web::method(propfind_method()).to(route_new_propfind::<A, EventResource, C>))
.route(web::method(propfind_method()).to(route_new_propfind::<A, EventResource<C>, C>))
.route(web::method(Method::DELETE).to(event::delete_event::<A, C>)) .route(web::method(Method::DELETE).to(event::delete_event::<A, C>))
.route(web::method(Method::GET).to(event::get_event::<A, C>)) .route(web::method(Method::GET).to(event::get_event::<A, C>))
.route(web::method(Method::PUT).to(event::put_event::<A, C>)), .route(web::method(Method::PUT).to(event::put_event::<A, C>)),

View File

@@ -1,15 +1,21 @@
use crate::resource::Resource; use std::sync::Arc;
use actix_web::HttpRequest;
use crate::{proptypes::write_string_prop, resource::Resource};
use actix_web::{web::Data, HttpRequest};
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use async_trait::async_trait; use async_trait::async_trait;
use rustical_auth::AuthInfo; use rustical_auth::AuthInfo;
use rustical_store::calendar::{CalendarStore, Event};
use tokio::sync::RwLock;
pub struct EventResource { pub struct EventResource<C: CalendarStore> {
path: String, pub cal_store: Arc<RwLock<C>>,
pub path: String,
pub event: Event,
} }
#[async_trait(?Send)] #[async_trait(?Send)]
impl Resource for EventResource { impl<C: CalendarStore> Resource for EventResource<C> {
type UriComponents = (String, String, String); // principal, calendar, event type UriComponents = (String, String, String); // principal, calendar, event
type MemberType = Self; type MemberType = Self;
@@ -24,23 +30,47 @@ impl Resource for EventResource {
async fn acquire_from_request( async fn acquire_from_request(
req: HttpRequest, req: HttpRequest,
_auth_info: AuthInfo, _auth_info: AuthInfo,
_uri_components: Self::UriComponents, uri_components: Self::UriComponents,
_prefix: String, _prefix: String,
) -> Result<Self> { ) -> Result<Self> {
let (_principal, cid, uid) = uri_components;
let cal_store = req
.app_data::<Data<RwLock<C>>>()
.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 { Ok(Self {
cal_store,
event,
path: req.path().to_string(), path: req.path().to_string(),
}) })
} }
fn write_prop<W: std::io::Write>( fn write_prop<W: std::io::Write>(
&self, &self,
_writer: &mut quick_xml::Writer<W>, writer: &mut quick_xml::Writer<W>,
_prop: &str, prop: &str,
) -> Result<()> { ) -> 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> { fn list_dead_props() -> Vec<&'static str> {
vec![] vec!["getetag", "calendar-data", "getcontenttype"]
} }
} }