From 61aef8d52b13e524faa63fc8d95ea25693e1a71c Mon Sep 17 00:00:00 2001 From: Lennart <18233294+lennart-k@users.noreply.github.com> Date: Sun, 27 Oct 2024 01:20:27 +0200 Subject: [PATCH] Refactoring to remove CalDavContext --- .../caldav/src/calendar/methods/mkcalendar.rs | 14 ++----- crates/caldav/src/calendar_object/methods.rs | 39 +++++++------------ crates/caldav/src/lib.rs | 7 ---- 3 files changed, 19 insertions(+), 41 deletions(-) diff --git a/crates/caldav/src/calendar/methods/mkcalendar.rs b/crates/caldav/src/calendar/methods/mkcalendar.rs index b4e7de0..32f942b 100644 --- a/crates/caldav/src/calendar/methods/mkcalendar.rs +++ b/crates/caldav/src/calendar/methods/mkcalendar.rs @@ -1,4 +1,3 @@ -use crate::CalDavContext; use crate::Error; use actix_web::web::{Data, Path}; use actix_web::HttpResponse; @@ -6,6 +5,7 @@ use rustical_store::auth::User; use rustical_store::model::Calendar; use rustical_store::CalendarStore; use serde::{Deserialize, Serialize}; +use tokio::sync::RwLock; #[derive(Deserialize, Serialize, Clone, Debug)] #[serde(rename_all = "kebab-case")] @@ -57,7 +57,7 @@ pub async fn route_mkcalendar( path: Path<(String, String)>, body: String, user: User, - context: Data>, + store: Data>, ) -> Result { let (principal, cal_id) = path.into_inner(); if principal != user.id { @@ -79,13 +79,7 @@ pub async fn route_mkcalendar( synctoken: 0, }; - match context - .store - .read() - .await - .get_calendar(&principal, &cal_id) - .await - { + match store.read().await.get_calendar(&principal, &cal_id).await { Err(rustical_store::Error::NotFound) => { // No conflict, no worries } @@ -99,7 +93,7 @@ pub async fn route_mkcalendar( } } - match context.store.write().await.insert_calendar(calendar).await { + match store.write().await.insert_calendar(calendar).await { // The spec says we should return a mkcalendar-response but I don't know what goes into it. // However, it works without one but breaks on iPadOS when using an empty one :) Ok(()) => Ok(HttpResponse::Created() diff --git a/crates/caldav/src/calendar_object/methods.rs b/crates/caldav/src/calendar_object/methods.rs index df3be2b..40f4cf7 100644 --- a/crates/caldav/src/calendar_object/methods.rs +++ b/crates/caldav/src/calendar_object/methods.rs @@ -1,4 +1,3 @@ -use crate::CalDavContext; use crate::Error; use actix_web::http::header; use actix_web::http::header::HeaderValue; @@ -8,15 +7,16 @@ use actix_web::HttpResponse; use rustical_store::auth::User; use rustical_store::model::CalendarObject; use rustical_store::CalendarStore; +use tokio::sync::RwLock; use tracing::instrument; use tracing_actix_web::RootSpan; use super::resource::CalendarObjectPathComponents; -#[instrument(parent = root_span.id(), skip(context, root_span))] +#[instrument(parent = root_span.id(), skip(store, root_span))] pub async fn get_event( - context: Data>, path: Path, + store: Data>, user: User, root_span: RootSpan, ) -> Result { @@ -30,18 +30,12 @@ pub async fn get_event( return Ok(HttpResponse::Unauthorized().body("")); } - let calendar = context - .store - .read() - .await - .get_calendar(&principal, &cal_id) - .await?; + let calendar = store.read().await.get_calendar(&principal, &cal_id).await?; if user.id != calendar.principal { return Ok(HttpResponse::Unauthorized().body("")); } - let event = context - .store + let event = store .read() .await .get_object(&principal, &cal_id, &object_id) @@ -53,10 +47,10 @@ pub async fn get_event( .body(event.get_ics().to_owned())) } -#[instrument(parent = root_span.id(), skip(context, req, root_span))] +#[instrument(parent = root_span.id(), skip(store, req, root_span))] pub async fn put_event( - context: Data>, path: Path, + store: Data>, body: String, user: User, req: HttpRequest, @@ -72,24 +66,21 @@ pub async fn put_event( return Ok(HttpResponse::Unauthorized().body("")); } - let calendar = context - .store - .read() - .await - .get_calendar(&principal, &cal_id) - .await?; + let calendar = store.read().await.get_calendar(&principal, &cal_id).await?; if user.id != calendar.principal { return Ok(HttpResponse::Unauthorized().body("")); } // TODO: implement If-Match - - // Lock the store - let mut store = context.store.write().await; + // + let mut store_write = store.write().await; if Some(&HeaderValue::from_static("*")) == req.headers().get(header::IF_NONE_MATCH) { // Only write if not existing - match store.get_object(&principal, &cal_id, &object_id).await { + match store_write + .get_object(&principal, &cal_id, &object_id) + .await + { Ok(_) => { // Conflict return Ok(HttpResponse::Conflict().body("Resource with this URI already exists")); @@ -105,7 +96,7 @@ pub async fn put_event( } let object = CalendarObject::from_ics(object_id, body)?; - store.put_object(principal, cal_id, object).await?; + store_write.put_object(principal, cal_id, object).await?; Ok(HttpResponse::Created().body("")) } diff --git a/crates/caldav/src/lib.rs b/crates/caldav/src/lib.rs index fb8f59e..540443f 100644 --- a/crates/caldav/src/lib.rs +++ b/crates/caldav/src/lib.rs @@ -21,10 +21,6 @@ pub mod root; pub use error::Error; -pub struct CalDavContext { - pub store: Arc>, -} - pub fn configure_well_known(cfg: &mut web::ServiceConfig, caldav_root: String) { cfg.service(web::redirect("/caldav", caldav_root).permanent()); } @@ -59,9 +55,6 @@ pub fn configure_dav( res }) }) - .app_data(Data::new(CalDavContext { - store: store.clone(), - })) .app_data(Data::from(store.clone())) .service(RootResourceService::actix_resource()) .service(