mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-13 22:52:22 +00:00
caldav: Refactoring
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
use crate::resources::event::EventFile;
|
use crate::event::resource::EventFile;
|
||||||
use crate::CalDavContext;
|
use crate::CalDavContext;
|
||||||
use crate::Error;
|
use crate::Error;
|
||||||
use actix_web::http::header::ContentType;
|
use actix_web::http::header::ContentType;
|
||||||
@@ -1,2 +1,2 @@
|
|||||||
|
pub mod methods;
|
||||||
pub mod resource;
|
pub mod resource;
|
||||||
pub mod route;
|
|
||||||
|
|||||||
98
crates/caldav/src/event/methods.rs
Normal file
98
crates/caldav/src/event/methods.rs
Normal file
@@ -0,0 +1,98 @@
|
|||||||
|
use crate::CalDavContext;
|
||||||
|
use actix_web::http::StatusCode;
|
||||||
|
use actix_web::web::{Data, Path};
|
||||||
|
use actix_web::{HttpResponse, ResponseError};
|
||||||
|
use rustical_auth::{AuthInfoExtractor, CheckAuthentication};
|
||||||
|
use rustical_store::calendar::CalendarStore;
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
|
#[derive(Debug, Error)]
|
||||||
|
pub enum Error {
|
||||||
|
#[error(transparent)]
|
||||||
|
Other(#[from] anyhow::Error),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ResponseError for Error {
|
||||||
|
fn status_code(&self) -> actix_web::http::StatusCode {
|
||||||
|
match self {
|
||||||
|
Self::Other(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn error_response(&self) -> HttpResponse<actix_web::body::BoxBody> {
|
||||||
|
HttpResponse::build(self.status_code()).body(self.to_string())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn delete_event<A: CheckAuthentication, C: CalendarStore + ?Sized>(
|
||||||
|
context: Data<CalDavContext<C>>,
|
||||||
|
path: Path<(String, String, String)>,
|
||||||
|
auth: AuthInfoExtractor<A>,
|
||||||
|
) -> Result<HttpResponse, Error> {
|
||||||
|
let _user = auth.inner.user_id;
|
||||||
|
// TODO: verify whether user is authorized
|
||||||
|
let (_principal, mut cid, uid) = path.into_inner();
|
||||||
|
if cid.ends_with(".ics") {
|
||||||
|
cid.truncate(cid.len() - 4);
|
||||||
|
}
|
||||||
|
context.store.write().await.delete_event(&cid, &uid).await?;
|
||||||
|
|
||||||
|
Ok(HttpResponse::Ok().body(""))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get_event<A: CheckAuthentication, C: CalendarStore + ?Sized>(
|
||||||
|
context: Data<CalDavContext<C>>,
|
||||||
|
path: Path<(String, String, String)>,
|
||||||
|
auth: AuthInfoExtractor<A>,
|
||||||
|
) -> Result<HttpResponse, Error> {
|
||||||
|
// TODO: verify whether user is authorized
|
||||||
|
let (principal, cid, mut uid) = path.into_inner();
|
||||||
|
|
||||||
|
if auth.inner.user_id != principal {
|
||||||
|
return Ok(HttpResponse::Unauthorized().body(""));
|
||||||
|
}
|
||||||
|
|
||||||
|
let calendar = context.store.read().await.get_calendar(&cid).await?;
|
||||||
|
if auth.inner.user_id != calendar.owner {
|
||||||
|
return Ok(HttpResponse::Unauthorized().body(""));
|
||||||
|
}
|
||||||
|
|
||||||
|
if uid.ends_with(".ics") {
|
||||||
|
uid.truncate(uid.len() - 4);
|
||||||
|
}
|
||||||
|
let event = context.store.read().await.get_event(&cid, &uid).await?;
|
||||||
|
|
||||||
|
Ok(HttpResponse::Ok()
|
||||||
|
.insert_header(("ETag", event.get_etag()))
|
||||||
|
.body(event.get_ics().to_owned()))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn put_event<A: CheckAuthentication, C: CalendarStore + ?Sized>(
|
||||||
|
context: Data<CalDavContext<C>>,
|
||||||
|
path: Path<(String, String, String)>,
|
||||||
|
body: String,
|
||||||
|
auth: AuthInfoExtractor<A>,
|
||||||
|
) -> Result<HttpResponse, Error> {
|
||||||
|
let (principal, cid, mut uid) = path.into_inner();
|
||||||
|
let auth_info = auth.inner;
|
||||||
|
if auth_info.user_id != principal {
|
||||||
|
return Ok(HttpResponse::Unauthorized().body(""));
|
||||||
|
}
|
||||||
|
|
||||||
|
let calendar = context.store.read().await.get_calendar(&cid).await?;
|
||||||
|
if auth_info.user_id != calendar.owner {
|
||||||
|
return Ok(HttpResponse::Unauthorized().body(""));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Incredibly bodged method of normalising the uid but works for a prototype
|
||||||
|
if uid.ends_with(".ics") {
|
||||||
|
uid.truncate(uid.len() - 4);
|
||||||
|
}
|
||||||
|
context
|
||||||
|
.store
|
||||||
|
.write()
|
||||||
|
.await
|
||||||
|
.upsert_event(cid, uid, body)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
Ok(HttpResponse::Ok().body(""))
|
||||||
|
}
|
||||||
@@ -1,98 +1,2 @@
|
|||||||
use crate::CalDavContext;
|
pub mod methods;
|
||||||
use actix_web::http::StatusCode;
|
pub mod resource;
|
||||||
use actix_web::web::{Data, Path};
|
|
||||||
use actix_web::{HttpResponse, ResponseError};
|
|
||||||
use rustical_auth::{AuthInfoExtractor, CheckAuthentication};
|
|
||||||
use rustical_store::calendar::CalendarStore;
|
|
||||||
use thiserror::Error;
|
|
||||||
|
|
||||||
#[derive(Debug, Error)]
|
|
||||||
pub enum Error {
|
|
||||||
#[error(transparent)]
|
|
||||||
Other(#[from] anyhow::Error),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ResponseError for Error {
|
|
||||||
fn status_code(&self) -> actix_web::http::StatusCode {
|
|
||||||
match self {
|
|
||||||
Self::Other(_) => StatusCode::INTERNAL_SERVER_ERROR,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fn error_response(&self) -> HttpResponse<actix_web::body::BoxBody> {
|
|
||||||
HttpResponse::build(self.status_code()).body(self.to_string())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn delete_event<A: CheckAuthentication, C: CalendarStore + ?Sized>(
|
|
||||||
context: Data<CalDavContext<C>>,
|
|
||||||
path: Path<(String, String, String)>,
|
|
||||||
auth: AuthInfoExtractor<A>,
|
|
||||||
) -> Result<HttpResponse, Error> {
|
|
||||||
let _user = auth.inner.user_id;
|
|
||||||
// TODO: verify whether user is authorized
|
|
||||||
let (_principal, mut cid, uid) = path.into_inner();
|
|
||||||
if cid.ends_with(".ics") {
|
|
||||||
cid.truncate(cid.len() - 4);
|
|
||||||
}
|
|
||||||
context.store.write().await.delete_event(&cid, &uid).await?;
|
|
||||||
|
|
||||||
Ok(HttpResponse::Ok().body(""))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn get_event<A: CheckAuthentication, C: CalendarStore + ?Sized>(
|
|
||||||
context: Data<CalDavContext<C>>,
|
|
||||||
path: Path<(String, String, String)>,
|
|
||||||
auth: AuthInfoExtractor<A>,
|
|
||||||
) -> Result<HttpResponse, Error> {
|
|
||||||
// TODO: verify whether user is authorized
|
|
||||||
let (principal, cid, mut uid) = path.into_inner();
|
|
||||||
|
|
||||||
if auth.inner.user_id != principal {
|
|
||||||
return Ok(HttpResponse::Unauthorized().body(""));
|
|
||||||
}
|
|
||||||
|
|
||||||
let calendar = context.store.read().await.get_calendar(&cid).await?;
|
|
||||||
if auth.inner.user_id != calendar.owner {
|
|
||||||
return Ok(HttpResponse::Unauthorized().body(""));
|
|
||||||
}
|
|
||||||
|
|
||||||
if uid.ends_with(".ics") {
|
|
||||||
uid.truncate(uid.len() - 4);
|
|
||||||
}
|
|
||||||
let event = context.store.read().await.get_event(&cid, &uid).await?;
|
|
||||||
|
|
||||||
Ok(HttpResponse::Ok()
|
|
||||||
.insert_header(("ETag", event.get_etag()))
|
|
||||||
.body(event.get_ics().to_owned()))
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn put_event<A: CheckAuthentication, C: CalendarStore + ?Sized>(
|
|
||||||
context: Data<CalDavContext<C>>,
|
|
||||||
path: Path<(String, String, String)>,
|
|
||||||
body: String,
|
|
||||||
auth: AuthInfoExtractor<A>,
|
|
||||||
) -> Result<HttpResponse, Error> {
|
|
||||||
let (principal, cid, mut uid) = path.into_inner();
|
|
||||||
let auth_info = auth.inner;
|
|
||||||
if auth_info.user_id != principal {
|
|
||||||
return Ok(HttpResponse::Unauthorized().body(""));
|
|
||||||
}
|
|
||||||
|
|
||||||
let calendar = context.store.read().await.get_calendar(&cid).await?;
|
|
||||||
if auth_info.user_id != calendar.owner {
|
|
||||||
return Ok(HttpResponse::Unauthorized().body(""));
|
|
||||||
}
|
|
||||||
|
|
||||||
// Incredibly bodged method of normalising the uid but works for a prototype
|
|
||||||
if uid.ends_with(".ics") {
|
|
||||||
uid.truncate(uid.len() - 4);
|
|
||||||
}
|
|
||||||
context
|
|
||||||
.store
|
|
||||||
.write()
|
|
||||||
.await
|
|
||||||
.upsert_event(cid, uid, body)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(HttpResponse::Ok().body(""))
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -2,8 +2,8 @@ use actix_web::http::Method;
|
|||||||
use actix_web::web::{self, Data};
|
use actix_web::web::{self, Data};
|
||||||
use actix_web::{guard, HttpResponse, Responder};
|
use actix_web::{guard, HttpResponse, Responder};
|
||||||
use calendar::resource::CalendarResource;
|
use calendar::resource::CalendarResource;
|
||||||
|
use event::resource::EventResource;
|
||||||
use principal::PrincipalResource;
|
use principal::PrincipalResource;
|
||||||
use resources::event::EventResource;
|
|
||||||
use root::RootResource;
|
use root::RootResource;
|
||||||
use rustical_auth::CheckAuthentication;
|
use rustical_auth::CheckAuthentication;
|
||||||
use rustical_dav::error::Error;
|
use rustical_dav::error::Error;
|
||||||
@@ -12,11 +12,10 @@ use rustical_store::calendar::CalendarStore;
|
|||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tokio::sync::RwLock;
|
use tokio::sync::RwLock;
|
||||||
pub mod calendar;
|
|
||||||
|
|
||||||
|
pub mod calendar;
|
||||||
pub mod event;
|
pub mod event;
|
||||||
pub mod principal;
|
pub mod principal;
|
||||||
pub mod resources;
|
|
||||||
pub mod root;
|
pub mod root;
|
||||||
|
|
||||||
pub struct CalDavContext<C: CalendarStore + ?Sized> {
|
pub struct CalDavContext<C: CalendarStore + ?Sized> {
|
||||||
@@ -56,17 +55,17 @@ pub fn configure_dav<A: CheckAuthentication, C: CalendarStore + ?Sized>(
|
|||||||
)
|
)
|
||||||
.service(
|
.service(
|
||||||
web::resource("/{principal}/{calendar}")
|
web::resource("/{principal}/{calendar}")
|
||||||
.route(report_method().to(calendar::route::route_report_calendar::<A, C>))
|
.route(report_method().to(calendar::methods::route_report_calendar::<A, C>))
|
||||||
.route(propfind_method().to(handle_propfind::<A, CalendarResource<C>>))
|
.route(propfind_method().to(handle_propfind::<A, CalendarResource<C>>))
|
||||||
.route(mkcol_method().to(calendar::route::route_mkcol_calendar::<A, C>))
|
.route(mkcol_method().to(calendar::methods::route_mkcol_calendar::<A, C>))
|
||||||
.route(web::method(Method::DELETE).to(calendar::route::delete_calendar::<A, C>)),
|
.route(web::method(Method::DELETE).to(calendar::methods::delete_calendar::<A, C>)),
|
||||||
)
|
)
|
||||||
.service(
|
.service(
|
||||||
web::resource("/{principal}/{calendar}/{event}")
|
web::resource("/{principal}/{calendar}/{event}")
|
||||||
.route(propfind_method().to(handle_propfind::<A, EventResource<C>>))
|
.route(propfind_method().to(handle_propfind::<A, EventResource<C>>))
|
||||||
.route(web::method(Method::DELETE).to(event::delete_event::<A, C>))
|
.route(web::method(Method::DELETE).to(event::methods::delete_event::<A, C>))
|
||||||
.route(web::method(Method::GET).to(event::get_event::<A, C>))
|
.route(web::method(Method::GET).to(event::methods::get_event::<A, C>))
|
||||||
.route(web::method(Method::PUT).to(event::put_event::<A, C>)),
|
.route(web::method(Method::PUT).to(event::methods::put_event::<A, C>)),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1 +0,0 @@
|
|||||||
pub mod event;
|
|
||||||
Reference in New Issue
Block a user