Refactor the .ics path normalisation for CalendarObject

This commit is contained in:
Lennart
2024-10-08 15:54:19 +02:00
parent 63f16b6081
commit c0fb7d1d11
2 changed files with 48 additions and 19 deletions

View File

@@ -11,14 +11,20 @@ use rustical_store::CalendarStore;
use tracing::instrument; use tracing::instrument;
use tracing_actix_web::RootSpan; use tracing_actix_web::RootSpan;
#[instrument(parent = root_span.id(), skip(context, path, root_span))] use super::resource::CalendarObjectPathComponents;
#[instrument(parent = root_span.id(), skip(context, root_span))]
pub async fn get_event<C: CalendarStore + ?Sized>( pub async fn get_event<C: CalendarStore + ?Sized>(
context: Data<CalDavContext<C>>, context: Data<CalDavContext<C>>,
path: Path<(String, String, String)>, path: Path<CalendarObjectPathComponents>,
user: User, user: User,
root_span: RootSpan, root_span: RootSpan,
) -> Result<HttpResponse, Error> { ) -> Result<HttpResponse, Error> {
let (principal, cid, mut uid) = path.into_inner(); let CalendarObjectPathComponents {
principal,
cid,
uid,
} = path.into_inner();
if user.id != principal { if user.id != principal {
return Ok(HttpResponse::Unauthorized().body("")); return Ok(HttpResponse::Unauthorized().body(""));
@@ -34,9 +40,6 @@ pub async fn get_event<C: CalendarStore + ?Sized>(
return Ok(HttpResponse::Unauthorized().body("")); return Ok(HttpResponse::Unauthorized().body(""));
} }
if uid.ends_with(".ics") {
uid.truncate(uid.len() - 4);
}
let event = context let event = context
.store .store
.read() .read()
@@ -50,16 +53,21 @@ 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, path, req, root_span))] #[instrument(parent = root_span.id(), skip(context, req, root_span))]
pub async fn put_event<C: CalendarStore + ?Sized>( pub async fn put_event<C: CalendarStore + ?Sized>(
context: Data<CalDavContext<C>>, context: Data<CalDavContext<C>>,
path: Path<(String, String, String)>, path: Path<CalendarObjectPathComponents>,
body: String, body: String,
user: User, user: User,
req: HttpRequest, req: HttpRequest,
root_span: RootSpan, root_span: RootSpan,
) -> Result<HttpResponse, Error> { ) -> Result<HttpResponse, Error> {
let (principal, cid, mut uid) = path.into_inner(); let CalendarObjectPathComponents {
principal,
cid,
uid,
} = path.into_inner();
if user.id != principal { if user.id != principal {
return Ok(HttpResponse::Unauthorized().body("")); return Ok(HttpResponse::Unauthorized().body(""));
} }
@@ -73,10 +81,6 @@ pub async fn put_event<C: CalendarStore + ?Sized>(
if user.id != calendar.principal { if user.id != calendar.principal {
return Ok(HttpResponse::Unauthorized().body("")); 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);
}
// TODO: implement If-Match // TODO: implement If-Match

View File

@@ -75,9 +75,34 @@ impl Resource for CalendarObjectResource {
} }
} }
#[derive(Debug, Clone)]
pub struct CalendarObjectPathComponents {
pub principal: String,
pub cid: String,
pub uid: String,
}
impl<'de> Deserialize<'de> for CalendarObjectPathComponents {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
type Inner = (String, String, String);
let (principal, calendar, mut object) = Inner::deserialize(deserializer)?;
if object.ends_with(".ics") {
object.truncate(object.len() - 4);
}
Ok(Self {
principal,
cid: calendar,
uid: object,
})
}
}
#[async_trait(?Send)] #[async_trait(?Send)]
impl<C: CalendarStore + ?Sized> ResourceService for CalendarObjectResourceService<C> { impl<C: CalendarStore + ?Sized> ResourceService for CalendarObjectResourceService<C> {
type PathComponents = (String, String, String); // principal, calendar, event type PathComponents = CalendarObjectPathComponents;
type Resource = CalendarObjectResource; type Resource = CalendarObjectResource;
type MemberType = CalendarObjectResource; type MemberType = CalendarObjectResource;
type Error = Error; type Error = Error;
@@ -86,11 +111,11 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarObjectResourceServic
req: &HttpRequest, req: &HttpRequest,
path_components: Self::PathComponents, path_components: Self::PathComponents,
) -> Result<Self, Self::Error> { ) -> Result<Self, Self::Error> {
let (principal, cid, mut uid) = path_components; let CalendarObjectPathComponents {
principal,
if uid.ends_with(".ics") { cid,
uid.truncate(uid.len() - 4); uid,
} } = path_components;
let cal_store = req let cal_store = req
.app_data::<Data<RwLock<C>>>() .app_data::<Data<RwLock<C>>>()