Some refactoring

This commit is contained in:
Lennart
2024-06-28 21:55:15 +02:00
parent 6130b1ac6a
commit 04ad124799
11 changed files with 215 additions and 209 deletions

View File

@@ -13,6 +13,11 @@ use std::sync::Arc;
use strum::{EnumString, VariantNames};
use tokio::sync::RwLock;
use super::prop::{
Resourcetype, SupportedCalendarComponent, SupportedCalendarComponentSet, SupportedCalendarData,
UserPrivilegeSet,
};
pub struct CalendarResource<C: CalendarStore + ?Sized> {
pub cal_store: Arc<RwLock<C>>,
pub path: String,
@@ -20,102 +25,6 @@ pub struct CalendarResource<C: CalendarStore + ?Sized> {
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)]
#[strum(serialize_all = "kebab-case")]
pub enum CalendarPropName {
@@ -176,7 +85,6 @@ impl InvalidProperty for CalendarProp {
pub struct CalendarFile {
pub calendar: Calendar,
pub principal: String,
pub path: String,
}
impl Resource for CalendarFile {
@@ -190,7 +98,7 @@ impl Resource for CalendarFile {
Ok(CalendarProp::Resourcetype(Resourcetype::default()))
}
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!(
"{}/{}/",
@@ -306,10 +214,6 @@ impl Resource for CalendarFile {
CalendarPropName::CurrentUserPrivilegeSet => Err(rustical_dav::Error::PropReadOnly),
}
}
fn get_path(&self) -> &str {
&self.path
}
}
#[async_trait(?Send)]
@@ -330,14 +234,13 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarResource<C> {
Ok(CalendarFile {
calendar,
principal: self.principal.to_owned(),
path: self.path.to_owned(),
})
}
async fn get_members(
&self,
_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
Ok(self
.cal_store
@@ -346,16 +249,18 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarResource<C> {
.get_events(&self.principal, &self.calendar_id)
.await?
.into_iter()
.map(|event| EventFile {
path: format!("{}/{}", self.path, &event.get_uid()),
event,
.map(|event| {
(
format!("{}/{}", self.path, &event.get_uid()),
EventFile { event },
)
})
.collect())
}
async fn new(
req: HttpRequest,
auth_info: AuthInfo,
req: &HttpRequest,
auth_info: &AuthInfo,
path_components: Self::PathComponents,
) -> Result<Self, Self::Error> {
let cal_store = req
@@ -366,7 +271,7 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarResource<C> {
Ok(Self {
path: req.path().to_owned(),
principal: auth_info.user_id,
principal: auth_info.user_id.to_owned(),
calendar_id: path_components.1,
cal_store,
})