Add import_calendar method to CalendarStore

This commit is contained in:
Lennart
2025-08-23 12:23:05 +02:00
parent 9c114dc204
commit b7cfd3301b
4 changed files with 70 additions and 0 deletions

View File

@@ -34,6 +34,12 @@ pub trait CalendarStore: Send + Sync + 'static {
use_trashbin: bool, use_trashbin: bool,
) -> Result<(), Error>; ) -> Result<(), Error>;
async fn restore_calendar(&self, principal: &str, name: &str) -> Result<(), Error>; async fn restore_calendar(&self, principal: &str, name: &str) -> Result<(), Error>;
async fn import_calendar(
&self,
calendar: Calendar,
objects: Vec<CalendarObject>,
merge_existing: bool,
) -> Result<(), Error>;
async fn sync_changes( async fn sync_changes(
&self, &self,

View File

@@ -189,6 +189,24 @@ impl<CS: CalendarStore, BS: CalendarStore> CalendarStore for CombinedCalendarSto
} }
} }
#[inline]
async fn import_calendar(
&self,
calendar: Calendar,
objects: Vec<CalendarObject>,
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] #[inline]
async fn delete_calendar( async fn delete_calendar(
&self, &self,

View File

@@ -83,6 +83,15 @@ impl<AS: AddressbookStore> CalendarStore for ContactBirthdayStore<AS> {
Err(Error::ReadOnly) Err(Error::ReadOnly)
} }
async fn import_calendar(
&self,
_calendar: Calendar,
_objects: Vec<CalendarObject>,
_merge_existing: bool,
) -> Result<(), Error> {
Err(Error::ReadOnly)
}
async fn sync_changes( async fn sync_changes(
&self, &self,
principal: &str, principal: &str,

View File

@@ -570,6 +570,43 @@ impl CalendarStore for SqliteCalendarStore {
Self::_restore_calendar(&self.db, principal, id).await Self::_restore_calendar(&self.db, principal, id).await
} }
#[instrument]
async fn import_calendar(
&self,
calendar: Calendar,
objects: Vec<CalendarObject>,
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] #[instrument]
async fn calendar_query( async fn calendar_query(
&self, &self,