Make prefix a parameter to decrease chaos

This commit is contained in:
Lennart
2024-05-25 22:19:38 +02:00
parent d0f652a951
commit 38f5338ceb
7 changed files with 24 additions and 37 deletions

View File

@@ -14,7 +14,6 @@ use tokio::sync::RwLock;
pub struct CalendarResource<C: CalendarStore + ?Sized> {
pub cal_store: Arc<RwLock<C>>,
pub path: String,
pub prefix: String,
pub principal: String,
pub calendar_id: String,
}
@@ -160,7 +159,6 @@ pub enum CalendarPropResponse {
pub struct CalendarFile {
pub calendar: Calendar,
pub principal: String,
pub prefix: String,
pub path: String,
}
@@ -168,17 +166,17 @@ impl Resource for CalendarFile {
type PropType = CalendarProp;
type PropResponse = CalendarPropResponse;
fn get_prop(&self, prop: Self::PropType) -> Result<Self::PropResponse> {
fn get_prop(&self, prefix: &str, prop: Self::PropType) -> Result<Self::PropResponse> {
match prop {
CalendarProp::Resourcetype => {
Ok(CalendarPropResponse::Resourcetype(Resourcetype::default()))
}
CalendarProp::CurrentUserPrincipal => Ok(CalendarPropResponse::CurrentUserPrincipal(
HrefElement::new(format!("{}/{}/", self.prefix, self.principal)),
HrefElement::new(format!("{}/{}/", prefix, self.principal)),
)),
CalendarProp::Owner => Ok(CalendarPropResponse::Owner(HrefElement::new(format!(
"{}/{}/",
self.prefix, self.principal
prefix, self.principal
)))),
CalendarProp::Displayname => Ok(CalendarPropResponse::Displayname(TextNode(
self.calendar.name.clone(),
@@ -232,7 +230,6 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarResource<C> {
.map_err(|_e| Error::NotFound)?;
Ok(CalendarFile {
calendar,
prefix: self.prefix.to_owned(),
principal: self.principal.to_owned(),
path: self.path.to_owned(),
})
@@ -247,7 +244,6 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarResource<C> {
req: HttpRequest,
auth_info: AuthInfo,
path_components: Self::PathComponents,
prefix: String,
) -> Result<Self, rustical_dav::error::Error> {
let cal_store = req
.app_data::<Data<RwLock<C>>>()
@@ -256,7 +252,6 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarResource<C> {
.into_inner();
Ok(Self {
prefix,
path: req.path().to_owned(),
principal: auth_info.user_id,
calendar_id: path_components.1,

View File

@@ -47,7 +47,7 @@ impl Resource for EventFile {
"asd"
}
fn get_prop(&self, prop: Self::PropType) -> Result<Self::PropResponse> {
fn get_prop(&self, _prefix: &str, prop: Self::PropType) -> Result<Self::PropResponse> {
match prop {
EventProp::Getetag => Ok(PrincipalPropResponse::Getetag(TextNode(Some(
self.event.get_etag(),
@@ -76,7 +76,6 @@ impl<C: CalendarStore + ?Sized> ResourceService for EventResource<C> {
req: HttpRequest,
_auth_info: AuthInfo,
path_components: Self::PathComponents,
_prefix: String,
) -> Result<Self, Error> {
let (_principal, cid, uid) = path_components;

View File

@@ -15,14 +15,12 @@ use tokio::sync::RwLock;
use super::calendar::CalendarFile;
pub struct PrincipalResource<C: CalendarStore + ?Sized> {
prefix: String,
principal: String,
path: String,
cal_store: Arc<RwLock<C>>,
}
pub struct PrincipalFile {
prefix: String,
principal: String,
path: String,
}
@@ -63,23 +61,23 @@ impl Resource for PrincipalFile {
type PropType = PrincipalProp;
type PropResponse = PrincipalPropResponse;
fn get_prop(&self, prop: Self::PropType) -> Result<Self::PropResponse> {
fn get_prop(&self, prefix: &str, prop: Self::PropType) -> Result<Self::PropResponse> {
match prop {
PrincipalProp::Resourcetype => {
Ok(PrincipalPropResponse::Resourcetype(Resourcetype::default()))
}
PrincipalProp::CurrentUserPrincipal => Ok(PrincipalPropResponse::CurrentUserPrincipal(
HrefElement::new(format!("{}/{}/", self.prefix, self.principal)),
HrefElement::new(format!("{}/{}/", prefix, self.principal)),
)),
PrincipalProp::PrincipalUrl => Ok(PrincipalPropResponse::PrincipalUrl(
HrefElement::new(format!("{}/{}/", self.prefix, self.principal)),
HrefElement::new(format!("{}/{}/", prefix, self.principal)),
)),
PrincipalProp::CalendarHomeSet => Ok(PrincipalPropResponse::CalendarHomeSet(
HrefElement::new(format!("{}/{}/", self.prefix, self.principal)),
HrefElement::new(format!("{}/{}/", prefix, self.principal)),
)),
PrincipalProp::CalendarUserAddressSet => {
Ok(PrincipalPropResponse::CalendarUserAddressSet(
HrefElement::new(format!("{}/{}/", self.prefix, self.principal)),
HrefElement::new(format!("{}/{}/", prefix, self.principal)),
))
}
}
@@ -100,7 +98,6 @@ impl<C: CalendarStore + ?Sized> ResourceService for PrincipalResource<C> {
req: HttpRequest,
auth_info: AuthInfo,
_path_components: Self::PathComponents,
prefix: String,
) -> Result<Self, rustical_dav::error::Error> {
let cal_store = req
.app_data::<Data<RwLock<C>>>()
@@ -112,14 +109,12 @@ impl<C: CalendarStore + ?Sized> ResourceService for PrincipalResource<C> {
cal_store,
path: req.path().to_owned(),
principal: auth_info.user_id,
prefix,
})
}
async fn get_file(&self) -> Result<Self::File> {
Ok(PrincipalFile {
principal: self.principal.to_owned(),
prefix: self.prefix.to_owned(),
path: self.path.to_owned(),
})
}
@@ -136,7 +131,6 @@ impl<C: CalendarStore + ?Sized> ResourceService for PrincipalResource<C> {
.map(|cal| CalendarFile {
calendar: cal,
principal: self.principal.to_owned(),
prefix: self.prefix.to_owned(),
path: self.path.to_owned(),
})
.collect())

View File

@@ -9,7 +9,6 @@ use serde::Serialize;
use strum::{EnumString, IntoStaticStr, VariantNames};
pub struct RootResource {
prefix: String,
principal: String,
path: String,
}
@@ -35,7 +34,6 @@ pub enum RootPropResponse {
}
pub struct RootFile {
pub prefix: String,
pub principal: String,
pub path: String,
}
@@ -44,11 +42,11 @@ impl Resource for RootFile {
type PropType = RootProp;
type PropResponse = RootPropResponse;
fn get_prop(&self, prop: Self::PropType) -> Result<Self::PropResponse> {
fn get_prop(&self, prefix: &str, prop: Self::PropType) -> Result<Self::PropResponse> {
match prop {
RootProp::Resourcetype => Ok(RootPropResponse::Resourcetype(Resourcetype::default())),
RootProp::CurrentUserPrincipal => Ok(RootPropResponse::CurrentUserPrincipal(
HrefElement::new(format!("{}/{}/", self.prefix, self.principal)),
HrefElement::new(format!("{}/{}/", prefix, self.principal)),
)),
}
}
@@ -72,10 +70,8 @@ impl ResourceService for RootResource {
req: HttpRequest,
auth_info: AuthInfo,
_path_components: Self::PathComponents,
prefix: String,
) -> Result<Self, Error> {
Ok(Self {
prefix,
principal: auth_info.user_id,
path: req.path().to_string(),
})
@@ -85,7 +81,6 @@ impl ResourceService for RootResource {
Ok(RootFile {
path: self.path.to_owned(),
principal: self.principal.to_owned(),
prefix: self.prefix.to_owned(),
})
}
}