From b7cfd3301b7ea64f0703d0bc646f62a8d8c13148 Mon Sep 17 00:00:00 2001 From: Lennart <18233294+lennart-k@users.noreply.github.com> Date: Sat, 23 Aug 2025 12:23:05 +0200 Subject: [PATCH] Add import_calendar method to CalendarStore --- crates/store/src/calendar_store.rs | 6 ++++ crates/store/src/combined_calendar_store.rs | 18 ++++++++++ crates/store/src/contact_birthday_store.rs | 9 +++++ crates/store_sqlite/src/calendar_store.rs | 37 +++++++++++++++++++++ 4 files changed, 70 insertions(+) diff --git a/crates/store/src/calendar_store.rs b/crates/store/src/calendar_store.rs index c01c217..dd50de6 100644 --- a/crates/store/src/calendar_store.rs +++ b/crates/store/src/calendar_store.rs @@ -34,6 +34,12 @@ pub trait CalendarStore: Send + Sync + 'static { use_trashbin: bool, ) -> Result<(), Error>; async fn restore_calendar(&self, principal: &str, name: &str) -> Result<(), Error>; + async fn import_calendar( + &self, + calendar: Calendar, + objects: Vec, + merge_existing: bool, + ) -> Result<(), Error>; async fn sync_changes( &self, diff --git a/crates/store/src/combined_calendar_store.rs b/crates/store/src/combined_calendar_store.rs index 841bfb8..4b4df4a 100644 --- a/crates/store/src/combined_calendar_store.rs +++ b/crates/store/src/combined_calendar_store.rs @@ -189,6 +189,24 @@ impl CalendarStore for CombinedCalendarSto } } + #[inline] + async fn import_calendar( + &self, + calendar: Calendar, + objects: Vec, + merge_existing: bool, + ) -> Result<(), Error> { + if calendar.id.starts_with(BIRTHDAYS_PREFIX) { + self.birthday_store + .import_calendar(calendar, objects, merge_existing) + .await + } else { + self.cal_store + .import_calendar(calendar, objects, merge_existing) + .await + } + } + #[inline] async fn delete_calendar( &self, diff --git a/crates/store/src/contact_birthday_store.rs b/crates/store/src/contact_birthday_store.rs index 32e4c2d..7f79b85 100644 --- a/crates/store/src/contact_birthday_store.rs +++ b/crates/store/src/contact_birthday_store.rs @@ -83,6 +83,15 @@ impl CalendarStore for ContactBirthdayStore { Err(Error::ReadOnly) } + async fn import_calendar( + &self, + _calendar: Calendar, + _objects: Vec, + _merge_existing: bool, + ) -> Result<(), Error> { + Err(Error::ReadOnly) + } + async fn sync_changes( &self, principal: &str, diff --git a/crates/store_sqlite/src/calendar_store.rs b/crates/store_sqlite/src/calendar_store.rs index cfe82bf..82edfc2 100644 --- a/crates/store_sqlite/src/calendar_store.rs +++ b/crates/store_sqlite/src/calendar_store.rs @@ -570,6 +570,43 @@ impl CalendarStore for SqliteCalendarStore { Self::_restore_calendar(&self.db, principal, id).await } + #[instrument] + async fn import_calendar( + &self, + calendar: Calendar, + objects: Vec, + merge_existing: bool, + ) -> Result<(), Error> { + let mut tx = self.db.begin().await.map_err(crate::Error::from)?; + + let existing_cal = + match Self::_get_calendar(&mut *tx, &calendar.principal, &calendar.id, true).await { + Ok(cal) => Some(cal), + Err(Error::NotFound) => None, + Err(err) => return Err(err), + }; + if existing_cal.is_some() && !merge_existing { + return Err(Error::AlreadyExists); + } + if existing_cal.is_none() { + Self::_insert_calendar(&mut *tx, calendar.clone()).await?; + } + + for object in objects { + Self::_put_object( + &mut *tx, + calendar.principal.clone(), + calendar.id.clone(), + object, + false, + ) + .await?; + } + + tx.commit().await.map_err(crate::Error::from)?; + Ok(()) + } + #[instrument] async fn calendar_query( &self,