mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-13 18:12:27 +00:00
Refactoring to remove CalDavContext
This commit is contained in:
@@ -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<C: CalendarStore + ?Sized>(
|
||||
path: Path<(String, String)>,
|
||||
body: String,
|
||||
user: User,
|
||||
context: Data<CalDavContext<C>>,
|
||||
store: Data<RwLock<C>>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let (principal, cal_id) = path.into_inner();
|
||||
if principal != user.id {
|
||||
@@ -79,13 +79,7 @@ pub async fn route_mkcalendar<C: CalendarStore + ?Sized>(
|
||||
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<C: CalendarStore + ?Sized>(
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
|
||||
@@ -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<C: CalendarStore + ?Sized>(
|
||||
context: Data<CalDavContext<C>>,
|
||||
path: Path<CalendarObjectPathComponents>,
|
||||
store: Data<RwLock<C>>,
|
||||
user: User,
|
||||
root_span: RootSpan,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
@@ -30,18 +30,12 @@ pub async fn get_event<C: CalendarStore + ?Sized>(
|
||||
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<C: CalendarStore + ?Sized>(
|
||||
.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<C: CalendarStore + ?Sized>(
|
||||
context: Data<CalDavContext<C>>,
|
||||
path: Path<CalendarObjectPathComponents>,
|
||||
store: Data<RwLock<C>>,
|
||||
body: String,
|
||||
user: User,
|
||||
req: HttpRequest,
|
||||
@@ -72,24 +66,21 @@ pub async fn put_event<C: CalendarStore + ?Sized>(
|
||||
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<C: CalendarStore + ?Sized>(
|
||||
}
|
||||
|
||||
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(""))
|
||||
}
|
||||
|
||||
@@ -21,10 +21,6 @@ pub mod root;
|
||||
|
||||
pub use error::Error;
|
||||
|
||||
pub struct CalDavContext<C: CalendarStore + ?Sized> {
|
||||
pub store: Arc<RwLock<C>>,
|
||||
}
|
||||
|
||||
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<AP: AuthenticationProvider, C: CalendarStore + ?Sized>(
|
||||
res
|
||||
})
|
||||
})
|
||||
.app_data(Data::new(CalDavContext {
|
||||
store: store.clone(),
|
||||
}))
|
||||
.app_data(Data::from(store.clone()))
|
||||
.service(RootResourceService::actix_resource())
|
||||
.service(
|
||||
|
||||
Reference in New Issue
Block a user