mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-13 20:32:48 +00:00
Refactoring to remove CalDavContext
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
use crate::CalDavContext;
|
|
||||||
use crate::Error;
|
use crate::Error;
|
||||||
use actix_web::web::{Data, Path};
|
use actix_web::web::{Data, Path};
|
||||||
use actix_web::HttpResponse;
|
use actix_web::HttpResponse;
|
||||||
@@ -6,6 +5,7 @@ use rustical_store::auth::User;
|
|||||||
use rustical_store::model::Calendar;
|
use rustical_store::model::Calendar;
|
||||||
use rustical_store::CalendarStore;
|
use rustical_store::CalendarStore;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use tokio::sync::RwLock;
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize, Clone, Debug)]
|
#[derive(Deserialize, Serialize, Clone, Debug)]
|
||||||
#[serde(rename_all = "kebab-case")]
|
#[serde(rename_all = "kebab-case")]
|
||||||
@@ -57,7 +57,7 @@ pub async fn route_mkcalendar<C: CalendarStore + ?Sized>(
|
|||||||
path: Path<(String, String)>,
|
path: Path<(String, String)>,
|
||||||
body: String,
|
body: String,
|
||||||
user: User,
|
user: User,
|
||||||
context: Data<CalDavContext<C>>,
|
store: Data<RwLock<C>>,
|
||||||
) -> Result<HttpResponse, Error> {
|
) -> Result<HttpResponse, Error> {
|
||||||
let (principal, cal_id) = path.into_inner();
|
let (principal, cal_id) = path.into_inner();
|
||||||
if principal != user.id {
|
if principal != user.id {
|
||||||
@@ -79,13 +79,7 @@ pub async fn route_mkcalendar<C: CalendarStore + ?Sized>(
|
|||||||
synctoken: 0,
|
synctoken: 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
match context
|
match store.read().await.get_calendar(&principal, &cal_id).await {
|
||||||
.store
|
|
||||||
.read()
|
|
||||||
.await
|
|
||||||
.get_calendar(&principal, &cal_id)
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
Err(rustical_store::Error::NotFound) => {
|
Err(rustical_store::Error::NotFound) => {
|
||||||
// No conflict, no worries
|
// 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.
|
// 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 :)
|
// However, it works without one but breaks on iPadOS when using an empty one :)
|
||||||
Ok(()) => Ok(HttpResponse::Created()
|
Ok(()) => Ok(HttpResponse::Created()
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
use crate::CalDavContext;
|
|
||||||
use crate::Error;
|
use crate::Error;
|
||||||
use actix_web::http::header;
|
use actix_web::http::header;
|
||||||
use actix_web::http::header::HeaderValue;
|
use actix_web::http::header::HeaderValue;
|
||||||
@@ -8,15 +7,16 @@ use actix_web::HttpResponse;
|
|||||||
use rustical_store::auth::User;
|
use rustical_store::auth::User;
|
||||||
use rustical_store::model::CalendarObject;
|
use rustical_store::model::CalendarObject;
|
||||||
use rustical_store::CalendarStore;
|
use rustical_store::CalendarStore;
|
||||||
|
use tokio::sync::RwLock;
|
||||||
use tracing::instrument;
|
use tracing::instrument;
|
||||||
use tracing_actix_web::RootSpan;
|
use tracing_actix_web::RootSpan;
|
||||||
|
|
||||||
use super::resource::CalendarObjectPathComponents;
|
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>(
|
pub async fn get_event<C: CalendarStore + ?Sized>(
|
||||||
context: Data<CalDavContext<C>>,
|
|
||||||
path: Path<CalendarObjectPathComponents>,
|
path: Path<CalendarObjectPathComponents>,
|
||||||
|
store: Data<RwLock<C>>,
|
||||||
user: User,
|
user: User,
|
||||||
root_span: RootSpan,
|
root_span: RootSpan,
|
||||||
) -> Result<HttpResponse, Error> {
|
) -> Result<HttpResponse, Error> {
|
||||||
@@ -30,18 +30,12 @@ pub async fn get_event<C: CalendarStore + ?Sized>(
|
|||||||
return Ok(HttpResponse::Unauthorized().body(""));
|
return Ok(HttpResponse::Unauthorized().body(""));
|
||||||
}
|
}
|
||||||
|
|
||||||
let calendar = context
|
let calendar = store.read().await.get_calendar(&principal, &cal_id).await?;
|
||||||
.store
|
|
||||||
.read()
|
|
||||||
.await
|
|
||||||
.get_calendar(&principal, &cal_id)
|
|
||||||
.await?;
|
|
||||||
if user.id != calendar.principal {
|
if user.id != calendar.principal {
|
||||||
return Ok(HttpResponse::Unauthorized().body(""));
|
return Ok(HttpResponse::Unauthorized().body(""));
|
||||||
}
|
}
|
||||||
|
|
||||||
let event = context
|
let event = store
|
||||||
.store
|
|
||||||
.read()
|
.read()
|
||||||
.await
|
.await
|
||||||
.get_object(&principal, &cal_id, &object_id)
|
.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()))
|
.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>(
|
pub async fn put_event<C: CalendarStore + ?Sized>(
|
||||||
context: Data<CalDavContext<C>>,
|
|
||||||
path: Path<CalendarObjectPathComponents>,
|
path: Path<CalendarObjectPathComponents>,
|
||||||
|
store: Data<RwLock<C>>,
|
||||||
body: String,
|
body: String,
|
||||||
user: User,
|
user: User,
|
||||||
req: HttpRequest,
|
req: HttpRequest,
|
||||||
@@ -72,24 +66,21 @@ pub async fn put_event<C: CalendarStore + ?Sized>(
|
|||||||
return Ok(HttpResponse::Unauthorized().body(""));
|
return Ok(HttpResponse::Unauthorized().body(""));
|
||||||
}
|
}
|
||||||
|
|
||||||
let calendar = context
|
let calendar = store.read().await.get_calendar(&principal, &cal_id).await?;
|
||||||
.store
|
|
||||||
.read()
|
|
||||||
.await
|
|
||||||
.get_calendar(&principal, &cal_id)
|
|
||||||
.await?;
|
|
||||||
if user.id != calendar.principal {
|
if user.id != calendar.principal {
|
||||||
return Ok(HttpResponse::Unauthorized().body(""));
|
return Ok(HttpResponse::Unauthorized().body(""));
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: implement If-Match
|
// TODO: implement If-Match
|
||||||
|
//
|
||||||
// Lock the store
|
let mut store_write = store.write().await;
|
||||||
let mut store = context.store.write().await;
|
|
||||||
|
|
||||||
if Some(&HeaderValue::from_static("*")) == req.headers().get(header::IF_NONE_MATCH) {
|
if Some(&HeaderValue::from_static("*")) == req.headers().get(header::IF_NONE_MATCH) {
|
||||||
// Only write if not existing
|
// 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(_) => {
|
Ok(_) => {
|
||||||
// Conflict
|
// Conflict
|
||||||
return Ok(HttpResponse::Conflict().body("Resource with this URI already exists"));
|
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)?;
|
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(""))
|
Ok(HttpResponse::Created().body(""))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,10 +21,6 @@ pub mod root;
|
|||||||
|
|
||||||
pub use error::Error;
|
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) {
|
pub fn configure_well_known(cfg: &mut web::ServiceConfig, caldav_root: String) {
|
||||||
cfg.service(web::redirect("/caldav", caldav_root).permanent());
|
cfg.service(web::redirect("/caldav", caldav_root).permanent());
|
||||||
}
|
}
|
||||||
@@ -59,9 +55,6 @@ pub fn configure_dav<AP: AuthenticationProvider, C: CalendarStore + ?Sized>(
|
|||||||
res
|
res
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.app_data(Data::new(CalDavContext {
|
|
||||||
store: store.clone(),
|
|
||||||
}))
|
|
||||||
.app_data(Data::from(store.clone()))
|
.app_data(Data::from(store.clone()))
|
||||||
.service(RootResourceService::actix_resource())
|
.service(RootResourceService::actix_resource())
|
||||||
.service(
|
.service(
|
||||||
|
|||||||
Reference in New Issue
Block a user