mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-13 18:12:27 +00:00
Refactor the .ics path normalisation for CalendarObject
This commit is contained in:
@@ -11,14 +11,20 @@ use rustical_store::CalendarStore;
|
||||
use tracing::instrument;
|
||||
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>(
|
||||
context: Data<CalDavContext<C>>,
|
||||
path: Path<(String, String, String)>,
|
||||
path: Path<CalendarObjectPathComponents>,
|
||||
user: User,
|
||||
root_span: RootSpan,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let (principal, cid, mut uid) = path.into_inner();
|
||||
let CalendarObjectPathComponents {
|
||||
principal,
|
||||
cid,
|
||||
uid,
|
||||
} = path.into_inner();
|
||||
|
||||
if user.id != principal {
|
||||
return Ok(HttpResponse::Unauthorized().body(""));
|
||||
@@ -34,9 +40,6 @@ pub async fn get_event<C: CalendarStore + ?Sized>(
|
||||
return Ok(HttpResponse::Unauthorized().body(""));
|
||||
}
|
||||
|
||||
if uid.ends_with(".ics") {
|
||||
uid.truncate(uid.len() - 4);
|
||||
}
|
||||
let event = context
|
||||
.store
|
||||
.read()
|
||||
@@ -50,16 +53,21 @@ pub async fn get_event<C: CalendarStore + ?Sized>(
|
||||
.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>(
|
||||
context: Data<CalDavContext<C>>,
|
||||
path: Path<(String, String, String)>,
|
||||
path: Path<CalendarObjectPathComponents>,
|
||||
body: String,
|
||||
user: User,
|
||||
req: HttpRequest,
|
||||
root_span: RootSpan,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let (principal, cid, mut uid) = path.into_inner();
|
||||
let CalendarObjectPathComponents {
|
||||
principal,
|
||||
cid,
|
||||
uid,
|
||||
} = path.into_inner();
|
||||
|
||||
if user.id != principal {
|
||||
return Ok(HttpResponse::Unauthorized().body(""));
|
||||
}
|
||||
@@ -73,10 +81,6 @@ pub async fn put_event<C: CalendarStore + ?Sized>(
|
||||
if user.id != calendar.principal {
|
||||
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
|
||||
|
||||
|
||||
@@ -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)]
|
||||
impl<C: CalendarStore + ?Sized> ResourceService for CalendarObjectResourceService<C> {
|
||||
type PathComponents = (String, String, String); // principal, calendar, event
|
||||
type PathComponents = CalendarObjectPathComponents;
|
||||
type Resource = CalendarObjectResource;
|
||||
type MemberType = CalendarObjectResource;
|
||||
type Error = Error;
|
||||
@@ -86,11 +111,11 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarObjectResourceServic
|
||||
req: &HttpRequest,
|
||||
path_components: Self::PathComponents,
|
||||
) -> Result<Self, Self::Error> {
|
||||
let (principal, cid, mut uid) = path_components;
|
||||
|
||||
if uid.ends_with(".ics") {
|
||||
uid.truncate(uid.len() - 4);
|
||||
}
|
||||
let CalendarObjectPathComponents {
|
||||
principal,
|
||||
cid,
|
||||
uid,
|
||||
} = path_components;
|
||||
|
||||
let cal_store = req
|
||||
.app_data::<Data<RwLock<C>>>()
|
||||
|
||||
Reference in New Issue
Block a user