mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 07:02:24 +00:00
Some refactoring
This commit is contained in:
@@ -180,13 +180,11 @@ pub async fn route_report_calendar<A: CheckAuthentication, C: CalendarStore + ?S
|
|||||||
|
|
||||||
let mut responses = Vec::new();
|
let mut responses = Vec::new();
|
||||||
for event in events {
|
for event in events {
|
||||||
|
let path = format!("{}/{}", req.path(), event.get_uid());
|
||||||
responses.push(
|
responses.push(
|
||||||
EventFile {
|
EventFile { event }
|
||||||
path: format!("{}/{}", req.path(), event.get_uid()),
|
.propfind(&prefix.0, path, props.clone())
|
||||||
event,
|
.await?,
|
||||||
}
|
|
||||||
.propfind(&prefix.0, props.clone())
|
|
||||||
.await?,
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,2 +1,3 @@
|
|||||||
pub mod methods;
|
pub mod methods;
|
||||||
|
pub mod prop;
|
||||||
pub mod resource;
|
pub mod resource;
|
||||||
|
|||||||
97
crates/caldav/src/calendar/prop.rs
Normal file
97
crates/caldav/src/calendar/prop.rs
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
|
#[serde(rename_all = "kebab-case")]
|
||||||
|
pub struct SupportedCalendarComponent {
|
||||||
|
#[serde(rename = "@name")]
|
||||||
|
pub name: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
|
#[serde(rename_all = "kebab-case")]
|
||||||
|
pub struct SupportedCalendarComponentSet {
|
||||||
|
#[serde(rename = "C:comp")]
|
||||||
|
pub comp: Vec<SupportedCalendarComponent>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
|
#[serde(rename_all = "kebab-case")]
|
||||||
|
pub struct CalendarData {
|
||||||
|
#[serde(rename = "@content-type")]
|
||||||
|
content_type: String,
|
||||||
|
#[serde(rename = "@version")]
|
||||||
|
version: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for CalendarData {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
content_type: "text/calendar".to_owned(),
|
||||||
|
version: "2.0".to_owned(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize, Serialize, Default)]
|
||||||
|
#[serde(rename_all = "kebab-case")]
|
||||||
|
pub struct SupportedCalendarData {
|
||||||
|
#[serde(rename = "C:calendar-data", alias = "calendar-data")]
|
||||||
|
calendar_data: CalendarData,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize, Serialize, Default)]
|
||||||
|
#[serde(rename_all = "kebab-case")]
|
||||||
|
pub struct Resourcetype {
|
||||||
|
#[serde(rename = "C:calendar", alias = "calendar")]
|
||||||
|
calendar: (),
|
||||||
|
collection: (),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
|
#[serde(rename_all = "kebab-case")]
|
||||||
|
pub enum UserPrivilege {
|
||||||
|
Read,
|
||||||
|
ReadAcl,
|
||||||
|
Write,
|
||||||
|
WriteAcl,
|
||||||
|
WriteContent,
|
||||||
|
ReadCurrentUserPrivilegeSet,
|
||||||
|
Bind,
|
||||||
|
Unbind,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
|
#[serde(rename_all = "kebab-case")]
|
||||||
|
pub struct UserPrivilegeWrapper {
|
||||||
|
#[serde(rename = "$value")]
|
||||||
|
privilege: UserPrivilege,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<UserPrivilege> for UserPrivilegeWrapper {
|
||||||
|
fn from(value: UserPrivilege) -> Self {
|
||||||
|
Self { privilege: value }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||||
|
#[serde(rename_all = "kebab-case")]
|
||||||
|
pub struct UserPrivilegeSet {
|
||||||
|
privilege: Vec<UserPrivilegeWrapper>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for UserPrivilegeSet {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
privilege: vec![
|
||||||
|
UserPrivilege::Read.into(),
|
||||||
|
UserPrivilege::ReadAcl.into(),
|
||||||
|
UserPrivilege::Write.into(),
|
||||||
|
UserPrivilege::WriteAcl.into(),
|
||||||
|
UserPrivilege::WriteContent.into(),
|
||||||
|
UserPrivilege::ReadCurrentUserPrivilegeSet.into(),
|
||||||
|
UserPrivilege::Bind.into(),
|
||||||
|
UserPrivilege::Unbind.into(),
|
||||||
|
],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,6 +13,11 @@ use std::sync::Arc;
|
|||||||
use strum::{EnumString, VariantNames};
|
use strum::{EnumString, VariantNames};
|
||||||
use tokio::sync::RwLock;
|
use tokio::sync::RwLock;
|
||||||
|
|
||||||
|
use super::prop::{
|
||||||
|
Resourcetype, SupportedCalendarComponent, SupportedCalendarComponentSet, SupportedCalendarData,
|
||||||
|
UserPrivilegeSet,
|
||||||
|
};
|
||||||
|
|
||||||
pub struct CalendarResource<C: CalendarStore + ?Sized> {
|
pub struct CalendarResource<C: CalendarStore + ?Sized> {
|
||||||
pub cal_store: Arc<RwLock<C>>,
|
pub cal_store: Arc<RwLock<C>>,
|
||||||
pub path: String,
|
pub path: String,
|
||||||
@@ -20,102 +25,6 @@ pub struct CalendarResource<C: CalendarStore + ?Sized> {
|
|||||||
pub calendar_id: String,
|
pub calendar_id: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
|
||||||
#[serde(rename_all = "kebab-case")]
|
|
||||||
pub struct SupportedCalendarComponent {
|
|
||||||
#[serde(rename = "@name")]
|
|
||||||
pub name: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
|
||||||
#[serde(rename_all = "kebab-case")]
|
|
||||||
pub struct SupportedCalendarComponentSet {
|
|
||||||
#[serde(rename = "C:comp")]
|
|
||||||
pub comp: Vec<SupportedCalendarComponent>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
|
||||||
#[serde(rename_all = "kebab-case")]
|
|
||||||
pub struct CalendarData {
|
|
||||||
#[serde(rename = "@content-type")]
|
|
||||||
content_type: String,
|
|
||||||
#[serde(rename = "@version")]
|
|
||||||
version: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for CalendarData {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
content_type: "text/calendar".to_owned(),
|
|
||||||
version: "2.0".to_owned(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, Default)]
|
|
||||||
#[serde(rename_all = "kebab-case")]
|
|
||||||
pub struct SupportedCalendarData {
|
|
||||||
#[serde(rename = "C:calendar-data", alias = "calendar-data")]
|
|
||||||
calendar_data: CalendarData,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize, Default)]
|
|
||||||
#[serde(rename_all = "kebab-case")]
|
|
||||||
pub struct Resourcetype {
|
|
||||||
#[serde(rename = "C:calendar", alias = "calendar")]
|
|
||||||
calendar: (),
|
|
||||||
collection: (),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
|
||||||
#[serde(rename_all = "kebab-case")]
|
|
||||||
pub enum UserPrivilege {
|
|
||||||
Read,
|
|
||||||
ReadAcl,
|
|
||||||
Write,
|
|
||||||
WriteAcl,
|
|
||||||
WriteContent,
|
|
||||||
ReadCurrentUserPrivilegeSet,
|
|
||||||
Bind,
|
|
||||||
Unbind,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
|
||||||
#[serde(rename_all = "kebab-case")]
|
|
||||||
pub struct UserPrivilegeWrapper {
|
|
||||||
#[serde(rename = "$value")]
|
|
||||||
privilege: UserPrivilege,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<UserPrivilege> for UserPrivilegeWrapper {
|
|
||||||
fn from(value: UserPrivilege) -> Self {
|
|
||||||
Self { privilege: value }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
|
||||||
#[serde(rename_all = "kebab-case")]
|
|
||||||
pub struct UserPrivilegeSet {
|
|
||||||
privilege: Vec<UserPrivilegeWrapper>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Default for UserPrivilegeSet {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
privilege: vec![
|
|
||||||
UserPrivilege::Read.into(),
|
|
||||||
UserPrivilege::ReadAcl.into(),
|
|
||||||
UserPrivilege::Write.into(),
|
|
||||||
UserPrivilege::WriteAcl.into(),
|
|
||||||
UserPrivilege::WriteContent.into(),
|
|
||||||
UserPrivilege::ReadCurrentUserPrivilegeSet.into(),
|
|
||||||
UserPrivilege::Bind.into(),
|
|
||||||
UserPrivilege::Unbind.into(),
|
|
||||||
],
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(EnumString, Debug, VariantNames, Clone)]
|
#[derive(EnumString, Debug, VariantNames, Clone)]
|
||||||
#[strum(serialize_all = "kebab-case")]
|
#[strum(serialize_all = "kebab-case")]
|
||||||
pub enum CalendarPropName {
|
pub enum CalendarPropName {
|
||||||
@@ -176,7 +85,6 @@ impl InvalidProperty for CalendarProp {
|
|||||||
pub struct CalendarFile {
|
pub struct CalendarFile {
|
||||||
pub calendar: Calendar,
|
pub calendar: Calendar,
|
||||||
pub principal: String,
|
pub principal: String,
|
||||||
pub path: String,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Resource for CalendarFile {
|
impl Resource for CalendarFile {
|
||||||
@@ -190,7 +98,7 @@ impl Resource for CalendarFile {
|
|||||||
Ok(CalendarProp::Resourcetype(Resourcetype::default()))
|
Ok(CalendarProp::Resourcetype(Resourcetype::default()))
|
||||||
}
|
}
|
||||||
CalendarPropName::CurrentUserPrincipal => Ok(CalendarProp::CurrentUserPrincipal(
|
CalendarPropName::CurrentUserPrincipal => Ok(CalendarProp::CurrentUserPrincipal(
|
||||||
HrefElement::new(format!("{}/{}/", prefix, self.principal)),
|
HrefElement::new(format!("{}/user/{}/", prefix, self.principal)),
|
||||||
)),
|
)),
|
||||||
CalendarPropName::Owner => Ok(CalendarProp::Owner(HrefElement::new(format!(
|
CalendarPropName::Owner => Ok(CalendarProp::Owner(HrefElement::new(format!(
|
||||||
"{}/{}/",
|
"{}/{}/",
|
||||||
@@ -306,10 +214,6 @@ impl Resource for CalendarFile {
|
|||||||
CalendarPropName::CurrentUserPrivilegeSet => Err(rustical_dav::Error::PropReadOnly),
|
CalendarPropName::CurrentUserPrivilegeSet => Err(rustical_dav::Error::PropReadOnly),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_path(&self) -> &str {
|
|
||||||
&self.path
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait(?Send)]
|
#[async_trait(?Send)]
|
||||||
@@ -330,14 +234,13 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarResource<C> {
|
|||||||
Ok(CalendarFile {
|
Ok(CalendarFile {
|
||||||
calendar,
|
calendar,
|
||||||
principal: self.principal.to_owned(),
|
principal: self.principal.to_owned(),
|
||||||
path: self.path.to_owned(),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_members(
|
async fn get_members(
|
||||||
&self,
|
&self,
|
||||||
_auth_info: AuthInfo,
|
_auth_info: AuthInfo,
|
||||||
) -> Result<Vec<Self::MemberType>, Self::Error> {
|
) -> Result<Vec<(String, Self::MemberType)>, Self::Error> {
|
||||||
// As of now the calendar resource has no members since events are shown with REPORT
|
// As of now the calendar resource has no members since events are shown with REPORT
|
||||||
Ok(self
|
Ok(self
|
||||||
.cal_store
|
.cal_store
|
||||||
@@ -346,16 +249,18 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarResource<C> {
|
|||||||
.get_events(&self.principal, &self.calendar_id)
|
.get_events(&self.principal, &self.calendar_id)
|
||||||
.await?
|
.await?
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|event| EventFile {
|
.map(|event| {
|
||||||
path: format!("{}/{}", self.path, &event.get_uid()),
|
(
|
||||||
event,
|
format!("{}/{}", self.path, &event.get_uid()),
|
||||||
|
EventFile { event },
|
||||||
|
)
|
||||||
})
|
})
|
||||||
.collect())
|
.collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn new(
|
async fn new(
|
||||||
req: HttpRequest,
|
req: &HttpRequest,
|
||||||
auth_info: AuthInfo,
|
auth_info: &AuthInfo,
|
||||||
path_components: Self::PathComponents,
|
path_components: Self::PathComponents,
|
||||||
) -> Result<Self, Self::Error> {
|
) -> Result<Self, Self::Error> {
|
||||||
let cal_store = req
|
let cal_store = req
|
||||||
@@ -366,7 +271,7 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarResource<C> {
|
|||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
path: req.path().to_owned(),
|
path: req.path().to_owned(),
|
||||||
principal: auth_info.user_id,
|
principal: auth_info.user_id.to_owned(),
|
||||||
calendar_id: path_components.1,
|
calendar_id: path_components.1,
|
||||||
cal_store,
|
cal_store,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -47,7 +47,6 @@ impl InvalidProperty for EventProp {
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct EventFile {
|
pub struct EventFile {
|
||||||
pub event: Event,
|
pub event: Event,
|
||||||
pub path: String,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Resource for EventFile {
|
impl Resource for EventFile {
|
||||||
@@ -55,10 +54,6 @@ impl Resource for EventFile {
|
|||||||
type Prop = EventProp;
|
type Prop = EventProp;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
fn get_path(&self) -> &str {
|
|
||||||
&self.path
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_prop(&self, _prefix: &str, prop: Self::PropName) -> Result<Self::Prop, Self::Error> {
|
fn get_prop(&self, _prefix: &str, prop: Self::PropName) -> Result<Self::Prop, Self::Error> {
|
||||||
match prop {
|
match prop {
|
||||||
EventPropName::Getetag => Ok(EventProp::Getetag(self.event.get_etag())),
|
EventPropName::Getetag => Ok(EventProp::Getetag(self.event.get_etag())),
|
||||||
@@ -79,16 +74,9 @@ impl<C: CalendarStore + ?Sized> ResourceService for EventResource<C> {
|
|||||||
type MemberType = EventFile;
|
type MemberType = EventFile;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
async fn get_members(
|
|
||||||
&self,
|
|
||||||
_auth_info: AuthInfo,
|
|
||||||
) -> Result<Vec<Self::MemberType>, Self::Error> {
|
|
||||||
Ok(vec![])
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn new(
|
async fn new(
|
||||||
req: HttpRequest,
|
req: &HttpRequest,
|
||||||
_auth_info: AuthInfo,
|
_auth_info: &AuthInfo,
|
||||||
path_components: Self::PathComponents,
|
path_components: Self::PathComponents,
|
||||||
) -> Result<Self, Self::Error> {
|
) -> Result<Self, Self::Error> {
|
||||||
let (principal, cid, uid) = path_components;
|
let (principal, cid, uid) = path_components;
|
||||||
@@ -115,10 +103,7 @@ impl<C: CalendarStore + ?Sized> ResourceService for EventResource<C> {
|
|||||||
.await
|
.await
|
||||||
.get_event(&self.principal, &self.cid, &self.uid)
|
.get_event(&self.principal, &self.cid, &self.uid)
|
||||||
.await?;
|
.await?;
|
||||||
Ok(EventFile {
|
Ok(EventFile { event })
|
||||||
event,
|
|
||||||
path: self.path.to_owned(),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn save_file(&self, _file: Self::File) -> Result<(), Self::Error> {
|
async fn save_file(&self, _file: Self::File) -> Result<(), Self::Error> {
|
||||||
|
|||||||
@@ -58,30 +58,57 @@ pub fn configure_dav<A: CheckAuthentication, C: CalendarStore + ?Sized>(
|
|||||||
.route(proppatch_method().to(route_proppatch::<A, RootResource>)),
|
.route(proppatch_method().to(route_proppatch::<A, RootResource>)),
|
||||||
)
|
)
|
||||||
.service(
|
.service(
|
||||||
web::resource("/{principal}")
|
web::scope("/user").service(
|
||||||
.route(propfind_method().to(route_propfind::<A, PrincipalResource<C>>))
|
web::scope("/{principal}")
|
||||||
.route(proppatch_method().to(route_proppatch::<A, PrincipalResource<C>>)),
|
.service(
|
||||||
)
|
web::resource("")
|
||||||
.service(
|
.route(propfind_method().to(route_propfind::<A, PrincipalResource<C>>))
|
||||||
web::resource("/{principal}/{calendar}")
|
.route(proppatch_method().to(route_proppatch::<A, PrincipalResource<C>>)),
|
||||||
.route(report_method().to(calendar::methods::report::route_report_calendar::<A, C>))
|
)
|
||||||
.route(propfind_method().to(route_propfind::<A, CalendarResource<C>>))
|
.service(
|
||||||
.route(proppatch_method().to(route_proppatch::<A, CalendarResource<C>>))
|
web::scope("/{calendar}")
|
||||||
.route(
|
.service(
|
||||||
mkcalendar_method().to(calendar::methods::mkcalendar::route_mkcol_calendar::<A, C>),
|
web::resource("")
|
||||||
)
|
.route(
|
||||||
.route(
|
report_method().to(
|
||||||
web::method(Method::DELETE)
|
calendar::methods::report::route_report_calendar::<A, C>,
|
||||||
.to(calendar::methods::delete::route_delete_calendar::<A, C>),
|
),
|
||||||
),
|
)
|
||||||
)
|
.route(
|
||||||
.service(
|
propfind_method().to(route_propfind::<A, CalendarResource<C>>),
|
||||||
web::resource("/{principal}/{calendar}/{event}")
|
)
|
||||||
.route(propfind_method().to(route_propfind::<A, EventResource<C>>))
|
.route(
|
||||||
.route(proppatch_method().to(route_proppatch::<A, EventResource<C>>))
|
proppatch_method()
|
||||||
.route(web::method(Method::DELETE).to(event::methods::delete_event::<A, C>))
|
.to(route_proppatch::<A, CalendarResource<C>>),
|
||||||
.route(web::method(Method::GET).to(event::methods::get_event::<A, C>))
|
)
|
||||||
.route(web::method(Method::PUT).to(event::methods::put_event::<A, C>)),
|
.route(mkcalendar_method().to(
|
||||||
|
calendar::methods::mkcalendar::route_mkcol_calendar::<A, C>,
|
||||||
|
))
|
||||||
|
.route(
|
||||||
|
web::method(Method::DELETE).to(
|
||||||
|
calendar::methods::delete::route_delete_calendar::<A, C>,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.service(
|
||||||
|
web::resource("/{event}")
|
||||||
|
.route(propfind_method().to(route_propfind::<A, EventResource<C>>))
|
||||||
|
.route(
|
||||||
|
proppatch_method().to(route_proppatch::<A, EventResource<C>>),
|
||||||
|
)
|
||||||
|
.route(
|
||||||
|
web::method(Method::DELETE)
|
||||||
|
.to(event::methods::delete_event::<A, C>),
|
||||||
|
)
|
||||||
|
.route(
|
||||||
|
web::method(Method::GET).to(event::methods::get_event::<A, C>),
|
||||||
|
)
|
||||||
|
.route(
|
||||||
|
web::method(Method::PUT).to(event::methods::put_event::<A, C>),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,6 @@ pub struct PrincipalResource<C: CalendarStore + ?Sized> {
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct PrincipalFile {
|
pub struct PrincipalFile {
|
||||||
principal: String,
|
principal: String,
|
||||||
path: String,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize, Default, Debug)]
|
#[derive(Deserialize, Serialize, Default, Debug)]
|
||||||
@@ -80,20 +79,16 @@ impl Resource for PrincipalFile {
|
|||||||
HrefElement::new(format!("{}/{}/", prefix, self.principal)),
|
HrefElement::new(format!("{}/{}/", prefix, self.principal)),
|
||||||
)),
|
)),
|
||||||
PrincipalPropName::PrincipalUrl => Ok(PrincipalProp::PrincipalUrl(HrefElement::new(
|
PrincipalPropName::PrincipalUrl => Ok(PrincipalProp::PrincipalUrl(HrefElement::new(
|
||||||
format!("{}/{}/", prefix, self.principal),
|
format!("{}/user/{}/", prefix, self.principal),
|
||||||
))),
|
))),
|
||||||
PrincipalPropName::CalendarHomeSet => Ok(PrincipalProp::CalendarHomeSet(
|
PrincipalPropName::CalendarHomeSet => Ok(PrincipalProp::CalendarHomeSet(
|
||||||
HrefElement::new(format!("{}/{}/", prefix, self.principal)),
|
HrefElement::new(format!("{}/user/{}/", prefix, self.principal)),
|
||||||
)),
|
)),
|
||||||
PrincipalPropName::CalendarUserAddressSet => Ok(PrincipalProp::CalendarUserAddressSet(
|
PrincipalPropName::CalendarUserAddressSet => Ok(PrincipalProp::CalendarUserAddressSet(
|
||||||
HrefElement::new(format!("{}/{}/", prefix, self.principal)),
|
HrefElement::new(format!("{}/user/{}/", prefix, self.principal)),
|
||||||
)),
|
)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_path(&self) -> &str {
|
|
||||||
&self.path
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait(?Send)]
|
#[async_trait(?Send)]
|
||||||
@@ -104,8 +99,8 @@ impl<C: CalendarStore + ?Sized> ResourceService for PrincipalResource<C> {
|
|||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
async fn new(
|
async fn new(
|
||||||
req: HttpRequest,
|
req: &HttpRequest,
|
||||||
auth_info: AuthInfo,
|
auth_info: &AuthInfo,
|
||||||
(principal,): Self::PathComponents,
|
(principal,): Self::PathComponents,
|
||||||
) -> Result<Self, Self::Error> {
|
) -> Result<Self, Self::Error> {
|
||||||
if auth_info.user_id != principal {
|
if auth_info.user_id != principal {
|
||||||
@@ -127,14 +122,13 @@ impl<C: CalendarStore + ?Sized> ResourceService for PrincipalResource<C> {
|
|||||||
async fn get_file(&self) -> Result<Self::File, Self::Error> {
|
async fn get_file(&self) -> Result<Self::File, Self::Error> {
|
||||||
Ok(PrincipalFile {
|
Ok(PrincipalFile {
|
||||||
principal: self.principal.to_owned(),
|
principal: self.principal.to_owned(),
|
||||||
path: self.path.to_owned(),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_members(
|
async fn get_members(
|
||||||
&self,
|
&self,
|
||||||
_auth_info: AuthInfo,
|
_auth_info: AuthInfo,
|
||||||
) -> Result<Vec<Self::MemberType>, Self::Error> {
|
) -> Result<Vec<(String, Self::MemberType)>, Self::Error> {
|
||||||
let calendars = self
|
let calendars = self
|
||||||
.cal_store
|
.cal_store
|
||||||
.read()
|
.read()
|
||||||
@@ -143,10 +137,14 @@ impl<C: CalendarStore + ?Sized> ResourceService for PrincipalResource<C> {
|
|||||||
.await?;
|
.await?;
|
||||||
Ok(calendars
|
Ok(calendars
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|cal| CalendarFile {
|
.map(|cal| {
|
||||||
path: format!("{}/{}", &self.path, &cal.id),
|
(
|
||||||
calendar: cal,
|
format!("{}/{}", &self.path, &cal.id),
|
||||||
principal: self.principal.to_owned(),
|
CalendarFile {
|
||||||
|
calendar: cal,
|
||||||
|
principal: self.principal.to_owned(),
|
||||||
|
},
|
||||||
|
)
|
||||||
})
|
})
|
||||||
.collect())
|
.collect())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,13 +9,13 @@ use strum::{EnumString, VariantNames};
|
|||||||
|
|
||||||
pub struct RootResource {
|
pub struct RootResource {
|
||||||
principal: String,
|
principal: String,
|
||||||
path: String,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(EnumString, Debug, VariantNames, Clone)]
|
#[derive(EnumString, Debug, VariantNames, Clone)]
|
||||||
#[strum(serialize_all = "kebab-case")]
|
#[strum(serialize_all = "kebab-case")]
|
||||||
pub enum RootPropName {
|
pub enum RootPropName {
|
||||||
Resourcetype,
|
Resourcetype,
|
||||||
|
// Defined by RFC 5397
|
||||||
CurrentUserPrincipal,
|
CurrentUserPrincipal,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,7 +43,6 @@ impl InvalidProperty for RootProp {
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct RootFile {
|
pub struct RootFile {
|
||||||
pub principal: String,
|
pub principal: String,
|
||||||
pub path: String,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Resource for RootFile {
|
impl Resource for RootFile {
|
||||||
@@ -55,14 +54,10 @@ impl Resource for RootFile {
|
|||||||
match prop {
|
match prop {
|
||||||
RootPropName::Resourcetype => Ok(RootProp::Resourcetype(Resourcetype::default())),
|
RootPropName::Resourcetype => Ok(RootProp::Resourcetype(Resourcetype::default())),
|
||||||
RootPropName::CurrentUserPrincipal => Ok(RootProp::CurrentUserPrincipal(
|
RootPropName::CurrentUserPrincipal => Ok(RootProp::CurrentUserPrincipal(
|
||||||
HrefElement::new(format!("{}/{}/", prefix, self.principal)),
|
HrefElement::new(format!("{}/user/{}/", prefix, self.principal)),
|
||||||
)),
|
)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_path(&self) -> &str {
|
|
||||||
&self.path
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait(?Send)]
|
#[async_trait(?Send)]
|
||||||
@@ -72,27 +67,18 @@ impl ResourceService for RootResource {
|
|||||||
type File = RootFile;
|
type File = RootFile;
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
async fn get_members(
|
|
||||||
&self,
|
|
||||||
_auth_info: AuthInfo,
|
|
||||||
) -> Result<Vec<Self::MemberType>, Self::Error> {
|
|
||||||
Ok(vec![])
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn new(
|
async fn new(
|
||||||
req: HttpRequest,
|
_req: &HttpRequest,
|
||||||
auth_info: AuthInfo,
|
auth_info: &AuthInfo,
|
||||||
_path_components: Self::PathComponents,
|
_path_components: Self::PathComponents,
|
||||||
) -> Result<Self, Self::Error> {
|
) -> Result<Self, Self::Error> {
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
principal: auth_info.user_id,
|
principal: auth_info.user_id.to_owned(),
|
||||||
path: req.path().to_string(),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_file(&self) -> Result<Self::File, Self::Error> {
|
async fn get_file(&self) -> Result<Self::File, Self::Error> {
|
||||||
Ok(RootFile {
|
Ok(RootFile {
|
||||||
path: self.path.to_owned(),
|
|
||||||
principal: self.principal.to_owned(),
|
principal: self.principal.to_owned(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ struct PropfindElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn route_propfind<A: CheckAuthentication, R: ResourceService + ?Sized>(
|
pub async fn route_propfind<A: CheckAuthentication, R: ResourceService + ?Sized>(
|
||||||
path: Path<R::PathComponents>,
|
path_components: Path<R::PathComponents>,
|
||||||
body: String,
|
body: String,
|
||||||
req: HttpRequest,
|
req: HttpRequest,
|
||||||
prefix: Data<ServicePrefix>,
|
prefix: Data<ServicePrefix>,
|
||||||
@@ -48,9 +48,10 @@ pub async fn route_propfind<A: CheckAuthentication, R: ResourceService + ?Sized>
|
|||||||
debug!("{body}");
|
debug!("{body}");
|
||||||
let auth_info = auth.inner;
|
let auth_info = auth.inner;
|
||||||
let prefix = prefix.0.to_owned();
|
let prefix = prefix.0.to_owned();
|
||||||
let path_components = path.into_inner();
|
let path_components = path_components.into_inner();
|
||||||
|
let path = req.path().to_owned();
|
||||||
|
|
||||||
let resource_service = R::new(req, auth_info.clone(), path_components.clone()).await?;
|
let resource_service = R::new(&req, &auth_info, path_components.clone()).await?;
|
||||||
|
|
||||||
// A request body is optional. If empty we MUST return all props
|
// A request body is optional. If empty we MUST return all props
|
||||||
let propfind: PropfindElement = if !body.is_empty() {
|
let propfind: PropfindElement = if !body.is_empty() {
|
||||||
@@ -75,13 +76,13 @@ pub async fn route_propfind<A: CheckAuthentication, R: ResourceService + ?Sized>
|
|||||||
|
|
||||||
let mut member_responses = Vec::new();
|
let mut member_responses = Vec::new();
|
||||||
if depth != Depth::Zero {
|
if depth != Depth::Zero {
|
||||||
for member in resource_service.get_members(auth_info).await? {
|
for (path, member) in resource_service.get_members(auth_info).await? {
|
||||||
member_responses.push(member.propfind(&prefix, props.clone()).await?);
|
member_responses.push(member.propfind(&prefix, path, props.clone()).await?);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let resource = resource_service.get_file().await?;
|
let resource = resource_service.get_file().await?;
|
||||||
let response = resource.propfind(&prefix, props).await?;
|
let response = resource.propfind(&prefix, path, props).await?;
|
||||||
|
|
||||||
Ok(MultistatusElement {
|
Ok(MultistatusElement {
|
||||||
responses: vec![response],
|
responses: vec![response],
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ pub async fn route_proppatch<A: CheckAuthentication, R: ResourceService + ?Sized
|
|||||||
let auth_info = auth.inner;
|
let auth_info = auth.inner;
|
||||||
let path_components = path.into_inner();
|
let path_components = path.into_inner();
|
||||||
let href = req.path().to_owned();
|
let href = req.path().to_owned();
|
||||||
let resource_service = R::new(req, auth_info.clone(), path_components.clone()).await?;
|
let resource_service = R::new(&req, &auth_info, path_components.clone()).await?;
|
||||||
|
|
||||||
debug!("{body}");
|
debug!("{body}");
|
||||||
|
|
||||||
|
|||||||
@@ -21,8 +21,6 @@ pub trait Resource: Clone {
|
|||||||
|
|
||||||
fn get_prop(&self, prefix: &str, prop: Self::PropName) -> Result<Self::Prop, Self::Error>;
|
fn get_prop(&self, prefix: &str, prop: Self::PropName) -> Result<Self::Prop, Self::Error>;
|
||||||
|
|
||||||
fn get_path(&self) -> &str;
|
|
||||||
|
|
||||||
fn set_prop(&mut self, _prop: Self::Prop) -> Result<(), crate::Error> {
|
fn set_prop(&mut self, _prop: Self::Prop) -> Result<(), crate::Error> {
|
||||||
Err(crate::Error::PropReadOnly)
|
Err(crate::Error::PropReadOnly)
|
||||||
}
|
}
|
||||||
@@ -48,14 +46,19 @@ pub trait ResourceService: Sized {
|
|||||||
type Error: ResponseError + From<crate::Error> + From<anyhow::Error>;
|
type Error: ResponseError + From<crate::Error> + From<anyhow::Error>;
|
||||||
|
|
||||||
async fn new(
|
async fn new(
|
||||||
req: HttpRequest,
|
req: &HttpRequest,
|
||||||
auth_info: AuthInfo,
|
auth_info: &AuthInfo,
|
||||||
path_components: Self::PathComponents,
|
path_components: Self::PathComponents,
|
||||||
) -> Result<Self, Self::Error>;
|
) -> Result<Self, Self::Error>;
|
||||||
|
|
||||||
async fn get_file(&self) -> Result<Self::File, Self::Error>;
|
async fn get_file(&self) -> Result<Self::File, Self::Error>;
|
||||||
|
|
||||||
async fn get_members(&self, auth_info: AuthInfo) -> Result<Vec<Self::MemberType>, Self::Error>;
|
async fn get_members(
|
||||||
|
&self,
|
||||||
|
_auth_info: AuthInfo,
|
||||||
|
) -> Result<Vec<(String, Self::MemberType)>, Self::Error> {
|
||||||
|
Ok(vec![])
|
||||||
|
}
|
||||||
|
|
||||||
async fn save_file(&self, file: Self::File) -> Result<(), Self::Error>;
|
async fn save_file(&self, file: Self::File) -> Result<(), Self::Error>;
|
||||||
}
|
}
|
||||||
@@ -99,8 +102,12 @@ pub enum PropstatType<T1: Serialize, T2: Serialize> {
|
|||||||
pub trait HandlePropfind {
|
pub trait HandlePropfind {
|
||||||
type Error: ResponseError + From<crate::Error> + From<anyhow::Error>;
|
type Error: ResponseError + From<crate::Error> + From<anyhow::Error>;
|
||||||
|
|
||||||
async fn propfind(&self, prefix: &str, props: Vec<&str>)
|
async fn propfind(
|
||||||
-> Result<impl Serialize, Self::Error>;
|
&self,
|
||||||
|
prefix: &str,
|
||||||
|
path: String,
|
||||||
|
props: Vec<&str>,
|
||||||
|
) -> Result<impl Serialize, Self::Error>;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait(?Send)]
|
#[async_trait(?Send)]
|
||||||
@@ -110,6 +117,7 @@ impl<R: Resource> HandlePropfind for R {
|
|||||||
async fn propfind(
|
async fn propfind(
|
||||||
&self,
|
&self,
|
||||||
prefix: &str,
|
prefix: &str,
|
||||||
|
path: String,
|
||||||
props: Vec<&str>,
|
props: Vec<&str>,
|
||||||
) -> Result<PropstatResponseElement<PropWrapper<Vec<R::Prop>>, TagList>, R::Error> {
|
) -> Result<PropstatResponseElement<PropWrapper<Vec<R::Prop>>, TagList>, R::Error> {
|
||||||
let mut props = props;
|
let mut props = props;
|
||||||
@@ -125,7 +133,7 @@ impl<R: Resource> HandlePropfind for R {
|
|||||||
.map(|&prop| prop.to_string())
|
.map(|&prop| prop.to_string())
|
||||||
.collect();
|
.collect();
|
||||||
return Ok(PropstatResponseElement {
|
return Ok(PropstatResponseElement {
|
||||||
href: self.get_path().to_owned(),
|
href: path,
|
||||||
propstat: vec![PropstatType::Normal(PropstatElement {
|
propstat: vec![PropstatType::Normal(PropstatElement {
|
||||||
prop: PropWrapper::TagList(TagList::from(props)),
|
prop: PropWrapper::TagList(TagList::from(props)),
|
||||||
status: format!("HTTP/1.1 {}", StatusCode::OK),
|
status: format!("HTTP/1.1 {}", StatusCode::OK),
|
||||||
@@ -177,7 +185,7 @@ impl<R: Resource> HandlePropfind for R {
|
|||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
Ok(PropstatResponseElement {
|
Ok(PropstatResponseElement {
|
||||||
href: self.get_path().to_owned(),
|
href: path,
|
||||||
propstat: propstats,
|
propstat: propstats,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user