Refactoring to remove CalDavContext

This commit is contained in:
Lennart
2024-10-27 01:20:27 +02:00
parent ea4281babd
commit 61aef8d52b
3 changed files with 19 additions and 41 deletions

View File

@@ -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(""))
}