Skip to content

Commit

Permalink
birthday calendar, lots of refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
lennart-k committed Jan 12, 2025
1 parent f031478 commit b9af6c1
Show file tree
Hide file tree
Showing 19 changed files with 284 additions and 210 deletions.
12 changes: 3 additions & 9 deletions crates/caldav/src/calendar/methods/report/calendar_multiget.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::{
calendar_object::resource::{CalendarObjectProp, CalendarObjectResource},
principal::PrincipalResource,
Error,
};
use actix_web::{
Expand All @@ -27,13 +26,12 @@ pub(crate) struct CalendarMultigetRequest {

pub async fn get_objects_calendar_multiget<C: CalendarStore + ?Sized>(
cal_query: &CalendarMultigetRequest,
principal_url: &str,
path: &str,
principal: &str,
cal_id: &str,
store: &C,
) -> Result<(Vec<CalendarObject>, Vec<String>), Error> {
let resource_def =
ResourceDef::prefix(principal_url).join(&ResourceDef::new("/{cal_id}/{object_id}"));
let resource_def = ResourceDef::prefix(path).join(&ResourceDef::new("/{object_id}"));

let mut result = vec![];
let mut not_found = vec![];
Expand All @@ -43,9 +41,6 @@ pub async fn get_objects_calendar_multiget<C: CalendarStore + ?Sized>(
if !resource_def.capture_match_info(&mut path) {
not_found.push(href.to_owned());
};
if path.get("cal_id").unwrap() != cal_id {
not_found.push(href.to_owned());
}
let object_id = path.get("object_id").unwrap();
match store.get_object(principal, cal_id, object_id).await {
Ok(object) => result.push(object),
Expand All @@ -66,9 +61,8 @@ pub async fn handle_calendar_multiget<C: CalendarStore + ?Sized>(
cal_store: &C,
) -> Result<MultistatusElement<EitherProp<CalendarObjectProp, CommonPropertiesProp>, String>, Error>
{
let principal_url = PrincipalResource::get_url(req.resource_map(), vec![principal]).unwrap();
let (objects, not_found) =
get_objects_calendar_multiget(&cal_multiget, &principal_url, principal, cal_id, cal_store)
get_objects_calendar_multiget(&cal_multiget, req.path(), principal, cal_id, cal_store)
.await?;

let props = match cal_multiget.prop {
Expand Down
9 changes: 2 additions & 7 deletions crates/caldav/src/calendar/methods/report/calendar_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ impl CompFilterElement {
if self
.comp_filter
.iter()
.map(|filter| filter.matches(cal_object))
.all(|x| x)
.all(|filter| filter.matches(cal_object))
{
return true;
}
Expand Down Expand Up @@ -215,11 +214,7 @@ pub async fn handle_calendar_query<C: CalendarStore + ?Sized>(

let mut responses = Vec::new();
for object in objects {
let path = CalendarObjectResource::get_url(
req.resource_map(),
vec![principal, cal_id, object.get_id()],
)
.unwrap();
let path = format!("{}/{}", req.path().trim_end_matches('/'), object.get_id());
responses.push(
CalendarObjectResource {
object,
Expand Down
12 changes: 2 additions & 10 deletions crates/caldav/src/calendar/methods/report/sync_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,7 @@ pub async fn handle_sync_collection<C: CalendarStore + ?Sized>(

let mut responses = Vec::new();
for object in new_objects {
let path = CalendarObjectResource::get_url(
req.resource_map(),
vec![principal, cal_id, &object.get_id()],
)
.unwrap();
let path = format!("{}/{}", req.path().trim_end_matches('/'), object.get_id());
responses.push(
CalendarObjectResource {
object,
Expand All @@ -101,11 +97,7 @@ pub async fn handle_sync_collection<C: CalendarStore + ?Sized>(
}

for object_id in deleted_objects {
let path = CalendarObjectResource::get_url(
req.resource_map(),
vec![principal, cal_id, &object_id],
)
.unwrap();
let path = format!("{}/{}", req.path().trim_end_matches('/'), object_id);
responses.push(ResponseElement {
href: path,
status: Some(StatusCode::NOT_FOUND),
Expand Down
42 changes: 16 additions & 26 deletions crates/caldav/src/calendar/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,6 @@ use std::str::FromStr;
use std::sync::Arc;
use strum::{EnumDiscriminants, EnumString, IntoStaticStr, VariantNames};

pub struct CalendarResourceService<C: CalendarStore + ?Sized> {
cal_store: Arc<C>,
}

impl<C: CalendarStore + ?Sized> CalendarResourceService<C> {
pub fn new(cal_store: Arc<C>) -> Self {
Self { cal_store }
}
}

#[derive(XmlDeserialize, XmlSerialize, PartialEq, EnumDiscriminants, Clone)]
#[strum_discriminants(
name(CalendarPropName),
Expand Down Expand Up @@ -122,7 +112,7 @@ impl Resource for CalendarResource {

fn get_prop(
&self,
rmap: &ResourceMap,
_rmap: &ResourceMap,
_user: &User,
prop: &Self::PropName,
) -> Result<Self::Prop, Self::Error> {
Expand Down Expand Up @@ -153,8 +143,9 @@ impl Resource for CalendarResource {
CalendarPropName::Transports => CalendarProp::Transports(Default::default()),
CalendarPropName::Topic => {
// TODO: Add salt since this could be public
let url =
CalendarResource::get_url(rmap, [&self.cal.principal, &self.cal.id]).unwrap();
// let url =
// CalendarResource::get_url(rmap, [&self.cal.principal, &self.cal.id]).unwrap();
let url = "TODO!".to_owned();
let mut hasher = Sha256::new();
hasher.update(url);
let topic = format!("{:x}", hasher.finalize());
Expand Down Expand Up @@ -263,11 +254,6 @@ impl Resource for CalendarResource {
}
}

#[inline]
fn resource_name() -> &'static str {
"caldav_calendar"
}

fn get_owner(&self) -> Option<&str> {
Some(&self.cal.principal)
}
Expand All @@ -281,6 +267,16 @@ impl Resource for CalendarResource {
}
}

pub struct CalendarResourceService<C: CalendarStore + ?Sized> {
cal_store: Arc<C>,
}

impl<C: CalendarStore + ?Sized> CalendarResourceService<C> {
pub fn new(cal_store: Arc<C>) -> Self {
Self { cal_store }
}
}

#[async_trait(?Send)]
impl<C: CalendarStore + ?Sized> ResourceService for CalendarResourceService<C> {
type MemberType = CalendarObjectResource;
Expand All @@ -292,11 +288,7 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarResourceService<C> {
&self,
(principal, cal_id): &Self::PathComponents,
) -> Result<Self::Resource, Error> {
let calendar = self
.cal_store
.get_calendar(principal, cal_id)
.await
.map_err(|_e| Error::NotFound)?;
let calendar = self.cal_store.get_calendar(principal, cal_id).await?;
Ok(CalendarResource {
cal: calendar,
read_only: self.cal_store.is_read_only(),
Expand All @@ -306,7 +298,6 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarResourceService<C> {
async fn get_members(
&self,
(principal, cal_id): &Self::PathComponents,
rmap: &ResourceMap,
) -> Result<Vec<(String, Self::MemberType)>, Self::Error> {
Ok(self
.cal_store
Expand All @@ -315,8 +306,7 @@ impl<C: CalendarStore + ?Sized> ResourceService for CalendarResourceService<C> {
.into_iter()
.map(|object| {
(
CalendarObjectResource::get_url(rmap, vec![principal, cal_id, object.get_id()])
.unwrap(),
object.get_id().to_string(),
CalendarObjectResource {
object,
principal: principal.to_owned(),
Expand Down
5 changes: 0 additions & 5 deletions crates/caldav/src/calendar_object/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,6 @@ impl Resource for CalendarObjectResource {
})
}

#[inline]
fn resource_name() -> &'static str {
"caldav_calendar_object"
}

fn get_owner(&self) -> Option<&str> {
Some(&self.principal)
}
Expand Down
114 changes: 114 additions & 0 deletions crates/caldav/src/calendar_set/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
use crate::calendar::resource::CalendarResource;
use crate::principal::PrincipalResource;
use crate::Error;
use actix_web::dev::ResourceMap;
use async_trait::async_trait;
use rustical_dav::privileges::UserPrivilegeSet;
use rustical_dav::resource::{NamedRoute, Resource, ResourceService};
use rustical_dav::xml::{HrefElement, Resourcetype, ResourcetypeInner};
use rustical_store::auth::User;
use rustical_store::CalendarStore;
use rustical_xml::{XmlDeserialize, XmlSerialize};
use std::sync::Arc;
use strum::{EnumDiscriminants, EnumString, IntoStaticStr, VariantNames};

#[derive(Clone)]
pub struct CalendarSetResource {
pub(crate) principal: String,
}

#[derive(XmlDeserialize, XmlSerialize, PartialEq, EnumDiscriminants, Clone)]
#[strum_discriminants(
name(PrincipalPropName),
derive(EnumString, VariantNames, IntoStaticStr),
strum(serialize_all = "kebab-case")
)]
pub enum PrincipalProp {
// WebDAV Access Control (RFC 3744)
#[strum_discriminants(strum(serialize = "principal-URL"))]
#[xml(ns = "rustical_dav::namespace::NS_DAV")]
PrincipalUrl(HrefElement),
}

impl Resource for CalendarSetResource {
type PropName = PrincipalPropName;
type Prop = PrincipalProp;
type Error = Error;
type PrincipalResource = PrincipalResource;

fn get_resourcetype(&self) -> Resourcetype {
Resourcetype(&[ResourcetypeInner(
rustical_dav::namespace::NS_DAV,
"collection",
)])
}

fn get_prop(
&self,
rmap: &ResourceMap,
_user: &User,
prop: &Self::PropName,
) -> Result<Self::Prop, Self::Error> {
let principal_href = HrefElement::new(
Self::PrincipalResource::get_url(rmap, vec![&self.principal]).unwrap(),
);

Ok(match prop {
PrincipalPropName::PrincipalUrl => PrincipalProp::PrincipalUrl(principal_href),
})
}

fn get_owner(&self) -> Option<&str> {
Some(&self.principal)
}

fn get_user_privileges(&self, user: &User) -> Result<UserPrivilegeSet, Self::Error> {
Ok(UserPrivilegeSet::owner_only(self.principal == user.id))
}
}

pub struct CalendarSetResourceService<C: CalendarStore + ?Sized> {
cal_store: Arc<C>,
}

impl<C: CalendarStore + ?Sized> CalendarSetResourceService<C> {
pub fn new(cal_store: Arc<C>) -> Self {
Self { cal_store }
}
}

#[async_trait(?Send)]
impl<C: CalendarStore + ?Sized> ResourceService for CalendarSetResourceService<C> {
type PathComponents = (String,);
type MemberType = CalendarResource;
type Resource = CalendarSetResource;
type Error = Error;

async fn get_resource(
&self,
(principal,): &Self::PathComponents,
) -> Result<Self::Resource, Self::Error> {
Ok(CalendarSetResource {
principal: principal.to_owned(),
})
}

async fn get_members(
&self,
(principal,): &Self::PathComponents,
) -> Result<Vec<(String, Self::MemberType)>, Self::Error> {
let calendars = self.cal_store.get_calendars(principal).await?;
Ok(calendars
.into_iter()
.map(|cal| {
(
cal.id.to_owned(),
CalendarResource {
cal,
read_only: self.cal_store.is_read_only(),
},
)
})
.collect())
}
}
Loading

0 comments on commit b9af6c1

Please sign in to comment.