Skip to main content

rustical_store/
calendar_store.rs

1use crate::{Calendar, CollectionMetadata, error::Error};
2use async_trait::async_trait;
3use chrono::NaiveDate;
4use rustical_ical::CalendarObject;
5
6#[derive(Default, Debug, Clone, PartialEq, Eq)]
7pub struct CalendarQuery {
8    pub time_start: Option<NaiveDate>,
9    pub time_end: Option<NaiveDate>,
10}
11
12#[async_trait]
13pub trait CalendarReadStore: Send + Sync + 'static {
14    async fn get_calendar(
15        &self,
16        principal: &str,
17        id: &str,
18        show_deleted: bool,
19    ) -> Result<Calendar, Error>;
20    async fn get_calendars(&self, principal: &str) -> Result<Vec<Calendar>, Error>;
21    async fn get_deleted_calendars(&self, principal: &str) -> Result<Vec<Calendar>, Error>;
22
23    async fn sync_changes(
24        &self,
25        principal: &str,
26        cal_id: &str,
27        synctoken: i64,
28    ) -> Result<(Vec<(String, CalendarObject)>, Vec<String>, i64), Error>;
29
30    /// Since the <calendar-query> rules are rather complex this function
31    /// is only meant to do some prefiltering
32    async fn calendar_query(
33        &self,
34        principal: &str,
35        cal_id: &str,
36        _query: CalendarQuery,
37    ) -> Result<Vec<(String, CalendarObject)>, Error> {
38        self.get_objects(principal, cal_id).await
39    }
40
41    async fn calendar_metadata(
42        &self,
43        principal: &str,
44        cal_id: &str,
45    ) -> Result<CollectionMetadata, Error>;
46
47    async fn get_objects(
48        &self,
49        principal: &str,
50        cal_id: &str,
51    ) -> Result<Vec<(String, CalendarObject)>, Error>;
52    async fn get_object(
53        &self,
54        principal: &str,
55        cal_id: &str,
56        object_id: &str,
57        show_deleted: bool,
58    ) -> Result<CalendarObject, Error>;
59
60    // read_only refers to objects, metadata may still be updated
61    fn is_read_only(&self, cal_id: &str) -> bool;
62}
63
64#[async_trait]
65pub trait CalendarWriteStore: Send + Sync + 'static {
66    async fn update_calendar(
67        &self,
68        principal: &str,
69        id: &str,
70        calendar: Calendar,
71    ) -> Result<(), Error>;
72    async fn insert_calendar(&self, calendar: Calendar) -> Result<(), Error>;
73    async fn delete_calendar(
74        &self,
75        principal: &str,
76        name: &str,
77        use_trashbin: bool,
78    ) -> Result<(), Error>;
79    async fn restore_calendar(&self, principal: &str, name: &str) -> Result<(), Error>;
80    async fn import_calendar(
81        &self,
82        calendar: Calendar,
83        objects: Vec<CalendarObject>,
84        merge_existing: bool,
85    ) -> Result<(), Error>;
86
87    async fn put_objects(
88        &self,
89        principal: &str,
90        cal_id: &str,
91        objects: Vec<(String, CalendarObject)>,
92        overwrite: bool,
93    ) -> Result<(), Error>;
94    async fn put_object(
95        &self,
96        principal: &str,
97        cal_id: &str,
98        object_id: &str,
99        object: CalendarObject,
100        overwrite: bool,
101    ) -> Result<(), Error> {
102        self.put_objects(
103            principal,
104            cal_id,
105            vec![(object_id.to_owned(), object)],
106            overwrite,
107        )
108        .await
109    }
110    async fn delete_object(
111        &self,
112        principal: &str,
113        cal_id: &str,
114        object_id: &str,
115        use_trashbin: bool,
116    ) -> Result<(), Error>;
117    async fn restore_object(
118        &self,
119        principal: &str,
120        cal_id: &str,
121        object_id: &str,
122    ) -> Result<(), Error>;
123}
124
125#[async_trait]
126pub trait CalendarStorePruneDeleted: Send + Sync + 'static {
127    /// Removes all calendars marked for deletion before specified date
128    async fn prune_deleted_calendars(&self, before: NaiveDate) -> Result<(), Error>;
129
130    /// Removes all objects marked for deletion before specified date
131    async fn prune_deleted_objects(&self, before: NaiveDate) -> Result<(), Error>;
132}
133
134pub trait CalendarStore:
135    CalendarReadStore + CalendarWriteStore + CalendarStorePruneDeleted
136{
137}
138
139impl<T: CalendarReadStore + CalendarWriteStore + CalendarStorePruneDeleted> CalendarStore for T {}