migrate birthday store to sqlite

This commit is contained in:
Lennart
2025-11-02 21:06:43 +01:00
parent 547e477eca
commit 5cdbb3b9d3
9 changed files with 388 additions and 236 deletions

View File

@@ -0,0 +1,353 @@
use crate::addressbook_store::SqliteAddressbookStore;
use async_trait::async_trait;
use chrono::NaiveDateTime;
use rustical_ical::{AddressObject, CalendarObject, CalendarObjectType};
use rustical_store::{
Addressbook, AddressbookStore, Calendar, CalendarMetadata, CalendarStore, CollectionMetadata,
Error, PrefixedCalendarStore,
};
use sha2::{Digest, Sha256};
use sqlx::{Executor, Sqlite};
use std::collections::HashMap;
use tracing::instrument;
pub const BIRTHDAYS_PREFIX: &str = "_birthdays_";
struct BirthdayCalendarJoinRow {
principal: String,
id: String,
displayname: Option<String>,
description: Option<String>,
order: i64,
color: Option<String>,
timezone_id: Option<String>,
deleted_at: Option<NaiveDateTime>,
push_topic: String,
addr_synctoken: i64,
}
impl From<BirthdayCalendarJoinRow> for Calendar {
fn from(value: BirthdayCalendarJoinRow) -> Self {
Self {
principal: value.principal,
id: format!("{}{}", BIRTHDAYS_PREFIX, value.id),
meta: CalendarMetadata {
displayname: value.displayname,
order: value.order,
description: value.description,
color: value.color,
},
deleted_at: value.deleted_at,
components: vec![CalendarObjectType::Event],
timezone_id: value.timezone_id,
synctoken: value.addr_synctoken,
subscription_url: None,
push_topic: value.push_topic,
}
}
}
impl PrefixedCalendarStore for SqliteAddressbookStore {
const PREFIX: &'static str = BIRTHDAYS_PREFIX;
}
impl SqliteAddressbookStore {
#[instrument]
pub async fn _get_birthday_calendar<'e, E: Executor<'e, Database = Sqlite>>(
executor: E,
principal: &str,
id: &str,
show_deleted: bool,
) -> Result<Calendar, Error> {
let cal = sqlx::query_as!(
BirthdayCalendarJoinRow,
r#"SELECT principal, id, displayname, description, "order", color, timezone_id, deleted_at, addr_synctoken, push_topic
FROM birthday_calendars
INNER JOIN (
SELECT principal AS addr_principal,
id AS addr_id,
synctoken AS addr_synctoken
FROM addressbooks
) ON (principal, id) = (addr_principal, addr_id)
WHERE (principal, id) = (?, ?)
AND ((deleted_at IS NULL) OR ?)
"#,
principal,
id,
show_deleted
)
.fetch_one(executor)
.await
.map_err(crate::Error::from)?;
Ok(cal.into())
}
#[instrument]
pub async fn _get_birthday_calendars<'e, E: Executor<'e, Database = Sqlite>>(
executor: E,
principal: &str,
deleted: bool,
) -> Result<Vec<Calendar>, Error> {
Ok(
sqlx::query_as!(
BirthdayCalendarJoinRow,
r#"SELECT principal, id, displayname, description, "order", color, timezone_id, deleted_at, addr_synctoken, push_topic
FROM birthday_calendars
INNER JOIN (
SELECT principal AS addr_principal,
id AS addr_id,
synctoken AS addr_synctoken
FROM addressbooks
) ON (principal, id) = (addr_principal, addr_id)
WHERE principal = ?
AND (
(deleted_at IS NULL AND NOT ?) -- not deleted, want not deleted
OR (deleted_at IS NOT NULL AND ?) -- deleted, want deleted
)
"#,
principal,
deleted,
deleted
)
.fetch_all(executor)
.await
.map_err(crate::Error::from).map(|cals| cals.into_iter().map(BirthdayCalendarJoinRow::into).collect())?)
}
#[instrument]
pub async fn _insert_birthday_calendar<'e, E: Executor<'e, Database = Sqlite>>(
executor: E,
addressbook: Addressbook,
) -> Result<(), rustical_store::Error> {
let birthday_name = addressbook
.displayname
.map(|name| format!("{name} birthdays"));
let birthday_push_topic = {
let mut hasher = Sha256::new();
hasher.update("birthdays");
hasher.update(addressbook.push_topic);
format!("{:x}", hasher.finalize())
};
sqlx::query!(
r#"INSERT INTO birthday_calendars (principal, id, displayname, push_topic)
VALUES (?, ?, ?, ?)"#,
addressbook.principal,
addressbook.id,
birthday_name,
birthday_push_topic,
)
.execute(executor)
.await
.map_err(crate::Error::from)?;
Ok(())
}
#[instrument]
async fn _update_birthday_calendar<'e, E: Executor<'e, Database = Sqlite>>(
executor: E,
principal: &str,
calendar: &Calendar,
) -> Result<(), Error> {
let result = sqlx::query!(
r#"UPDATE birthday_calendars SET principal = ?, id = ?, displayname = ?, description = ?, "order" = ?, color = ?, timezone_id = ?, push_topic = ?
WHERE (principal, id) = (?, ?)"#,
calendar.principal,
calendar.id,
calendar.meta.displayname,
calendar.meta.description,
calendar.meta.order,
calendar.meta.color,
calendar.timezone_id,
calendar.push_topic,
principal,
calendar.id,
).execute(executor).await.map_err(crate::Error::from)?;
if result.rows_affected() == 0 {
return Err(rustical_store::Error::NotFound);
}
Ok(())
}
}
#[async_trait]
impl CalendarStore for SqliteAddressbookStore {
#[instrument]
async fn get_calendar(
&self,
principal: &str,
id: &str,
show_deleted: bool,
) -> Result<Calendar, Error> {
let id = id.strip_prefix(BIRTHDAYS_PREFIX).ok_or(Error::NotFound)?;
Self::_get_birthday_calendar(&self.db, principal, id, show_deleted).await
}
#[instrument]
async fn get_calendars(&self, principal: &str) -> Result<Vec<Calendar>, Error> {
Self::_get_birthday_calendars(&self.db, principal, false).await
}
#[instrument]
async fn get_deleted_calendars(&self, principal: &str) -> Result<Vec<Calendar>, Error> {
Self::_get_birthday_calendars(&self.db, principal, true).await
}
#[instrument]
async fn update_calendar(
&self,
principal: String,
id: String,
mut calendar: Calendar,
) -> Result<(), Error> {
assert_eq!(id, calendar.id);
calendar.id = calendar
.id
.strip_prefix(BIRTHDAYS_PREFIX)
.ok_or(Error::NotFound)?
.to_string();
Self::_update_birthday_calendar(&self.db, &principal, &calendar).await
}
#[instrument]
async fn insert_calendar(&self, _calendar: Calendar) -> Result<(), Error> {
Err(Error::ReadOnly)
}
#[instrument]
async fn delete_calendar(
&self,
_principal: &str,
_name: &str,
_use_trashbin: bool,
) -> Result<(), Error> {
Err(Error::ReadOnly)
}
#[instrument]
async fn restore_calendar(&self, _principal: &str, _name: &str) -> Result<(), Error> {
Err(Error::ReadOnly)
}
#[instrument]
async fn import_calendar(
&self,
_calendar: Calendar,
_objects: Vec<CalendarObject>,
_merge_existing: bool,
) -> Result<(), Error> {
Err(Error::ReadOnly)
}
#[instrument]
async fn sync_changes(
&self,
principal: &str,
cal_id: &str,
synctoken: i64,
) -> Result<(Vec<CalendarObject>, Vec<String>, i64), Error> {
let cal_id = cal_id
.strip_prefix(BIRTHDAYS_PREFIX)
.ok_or(Error::NotFound)?;
let (objects, deleted_objects, new_synctoken) =
AddressbookStore::sync_changes(self, principal, cal_id, synctoken).await?;
let objects: Result<Vec<Option<CalendarObject>>, rustical_ical::Error> = objects
.iter()
.map(AddressObject::get_birthday_object)
.collect();
let objects = objects?.into_iter().flatten().collect();
Ok((objects, deleted_objects, new_synctoken))
}
#[instrument]
async fn calendar_metadata(
&self,
principal: &str,
cal_id: &str,
) -> Result<CollectionMetadata, Error> {
let cal_id = cal_id
.strip_prefix(BIRTHDAYS_PREFIX)
.ok_or(Error::NotFound)?;
self.addressbook_metadata(principal, cal_id).await
}
#[instrument]
async fn get_objects(
&self,
principal: &str,
cal_id: &str,
) -> Result<Vec<CalendarObject>, Error> {
let cal_id = cal_id
.strip_prefix(BIRTHDAYS_PREFIX)
.ok_or(Error::NotFound)?;
let objects: Result<Vec<HashMap<&'static str, CalendarObject>>, rustical_ical::Error> =
AddressbookStore::get_objects(self, principal, cal_id)
.await?
.iter()
.map(AddressObject::get_significant_dates)
.collect();
let objects = objects?
.into_iter()
.flat_map(HashMap::into_values)
.collect();
Ok(objects)
}
#[instrument]
async fn get_object(
&self,
principal: &str,
cal_id: &str,
object_id: &str,
show_deleted: bool,
) -> Result<CalendarObject, Error> {
let cal_id = cal_id
.strip_prefix(BIRTHDAYS_PREFIX)
.ok_or(Error::NotFound)?;
let (addressobject_id, date_type) = object_id.rsplit_once('-').ok_or(Error::NotFound)?;
AddressbookStore::get_object(self, principal, cal_id, addressobject_id, show_deleted)
.await?
.get_significant_dates()?
.remove(date_type)
.ok_or(Error::NotFound)
}
#[instrument]
async fn put_object(
&self,
_principal: String,
_cal_id: String,
_object: CalendarObject,
_overwrite: bool,
) -> Result<(), Error> {
Err(Error::ReadOnly)
}
#[instrument]
async fn delete_object(
&self,
_principal: &str,
_cal_id: &str,
_object_id: &str,
_use_trashbin: bool,
) -> Result<(), Error> {
Err(Error::ReadOnly)
}
#[instrument]
async fn restore_object(
&self,
_principal: &str,
_cal_id: &str,
_object_id: &str,
) -> Result<(), Error> {
Err(Error::ReadOnly)
}
fn is_read_only(&self, _cal_id: &str) -> bool {
true
}
}

View File

@@ -0,0 +1,722 @@
use super::ChangeOperation;
use crate::BEGIN_IMMEDIATE;
use async_trait::async_trait;
use derive_more::derive::Constructor;
use rustical_ical::AddressObject;
use rustical_store::{
Addressbook, AddressbookStore, CollectionMetadata, CollectionOperation,
CollectionOperationInfo, Error, synctoken::format_synctoken,
};
use sqlx::{Acquire, Executor, Sqlite, SqlitePool, Transaction};
use tokio::sync::mpsc::Sender;
use tracing::{error, instrument};
pub mod birthday_calendar;
#[derive(Debug, Clone)]
struct AddressObjectRow {
id: String,
vcf: String,
}
impl TryFrom<AddressObjectRow> for AddressObject {
type Error = rustical_store::Error;
fn try_from(value: AddressObjectRow) -> Result<Self, Self::Error> {
Ok(Self::from_vcf(value.id, value.vcf)?)
}
}
#[derive(Debug, Constructor)]
pub struct SqliteAddressbookStore {
db: SqlitePool,
sender: Sender<CollectionOperation>,
}
impl SqliteAddressbookStore {
async fn _get_addressbook<'e, E: Executor<'e, Database = Sqlite>>(
executor: E,
principal: &str,
id: &str,
show_deleted: bool,
) -> Result<Addressbook, rustical_store::Error> {
let addressbook = sqlx::query_as!(
Addressbook,
r#"SELECT principal, id, synctoken, displayname, description, deleted_at, push_topic
FROM addressbooks
WHERE (principal, id) = (?, ?)
AND ((deleted_at IS NULL) OR ?) "#,
principal,
id,
show_deleted
)
.fetch_one(executor)
.await
.map_err(crate::Error::from)?;
Ok(addressbook)
}
async fn _get_addressbooks<'e, E: Executor<'e, Database = Sqlite>>(
executor: E,
principal: &str,
) -> Result<Vec<Addressbook>, rustical_store::Error> {
let addressbooks = sqlx::query_as!(
Addressbook,
r#"SELECT principal, id, synctoken, displayname, description, deleted_at, push_topic
FROM addressbooks
WHERE principal = ? AND deleted_at IS NULL"#,
principal
)
.fetch_all(executor)
.await
.map_err(crate::Error::from)?;
Ok(addressbooks)
}
async fn _get_deleted_addressbooks<'e, E: Executor<'e, Database = Sqlite>>(
executor: E,
principal: &str,
) -> Result<Vec<Addressbook>, rustical_store::Error> {
let addressbooks = sqlx::query_as!(
Addressbook,
r#"SELECT principal, id, synctoken, displayname, description, deleted_at, push_topic
FROM addressbooks
WHERE principal = ? AND deleted_at IS NOT NULL"#,
principal
)
.fetch_all(executor)
.await
.map_err(crate::Error::from)?;
Ok(addressbooks)
}
async fn _update_addressbook<'e, E: Executor<'e, Database = Sqlite>>(
executor: E,
principal: String,
id: String,
addressbook: Addressbook,
) -> Result<(), rustical_store::Error> {
let result = sqlx::query!(
r#"UPDATE addressbooks SET principal = ?, id = ?, displayname = ?, description = ?, push_topic = ?
WHERE (principal, id) = (?, ?)"#,
addressbook.principal,
addressbook.id,
addressbook.displayname,
addressbook.description,
addressbook.push_topic,
principal,
id
)
.execute(executor)
.await
.map_err(crate::Error::from)?;
if result.rows_affected() == 0 {
return Err(rustical_store::Error::NotFound);
}
Ok(())
}
async fn _insert_addressbook<'e, E: Executor<'e, Database = Sqlite>>(
executor: E,
addressbook: &Addressbook,
) -> Result<(), rustical_store::Error> {
sqlx::query!(
r#"INSERT INTO addressbooks (principal, id, displayname, description, push_topic)
VALUES (?, ?, ?, ?, ?)"#,
addressbook.principal,
addressbook.id,
addressbook.displayname,
addressbook.description,
addressbook.push_topic,
)
.execute(executor)
.await
.map_err(crate::Error::from)?;
Ok(())
}
async fn _delete_addressbook<'e, E: Executor<'e, Database = Sqlite>>(
executor: E,
principal: &str,
addressbook_id: &str,
use_trashbin: bool,
) -> Result<(), rustical_store::Error> {
if use_trashbin {
sqlx::query!(
r#"UPDATE addressbooks SET deleted_at = datetime() WHERE (principal, id) = (?, ?)"#,
principal,
addressbook_id
)
.execute(executor)
.await
.map_err(crate::Error::from)?;
} else {
sqlx::query!(
r#"DELETE FROM addressbooks WHERE (principal, id) = (?, ?)"#,
principal,
addressbook_id
)
.execute(executor)
.await
.map_err(crate::Error::from)?;
}
Ok(())
}
async fn _restore_addressbook<'e, E: Executor<'e, Database = Sqlite>>(
executor: E,
principal: &str,
addressbook_id: &str,
) -> Result<(), rustical_store::Error> {
sqlx::query!(
r"UPDATE addressbooks SET deleted_at = NULL WHERE (principal, id) = (?, ?)",
principal,
addressbook_id
)
.execute(executor)
.await
.map_err(crate::Error::from)?;
Ok(())
}
async fn _sync_changes<'a, A: Acquire<'a, Database = Sqlite>>(
acquire: A,
principal: &str,
addressbook_id: &str,
synctoken: i64,
) -> Result<(Vec<AddressObject>, Vec<String>, i64), rustical_store::Error> {
struct Row {
object_id: String,
synctoken: i64,
}
let mut conn = acquire.acquire().await.map_err(crate::Error::from)?;
let changes = sqlx::query_as!(
Row,
r#"
SELECT DISTINCT object_id, max(0, synctoken) as "synctoken!: i64" from addressobjectchangelog
WHERE synctoken > ?
ORDER BY synctoken ASC
"#,
synctoken
)
.fetch_all(&mut *conn)
.await.map_err(crate::Error::from)?;
let mut objects = vec![];
let mut deleted_objects = vec![];
let new_synctoken = changes.last().map_or(0, |&Row { synctoken, .. }| synctoken);
for Row { object_id, .. } in changes {
match Self::_get_object(&mut *conn, principal, addressbook_id, &object_id, false).await
{
Ok(object) => objects.push(object),
Err(rustical_store::Error::NotFound) => deleted_objects.push(object_id),
Err(err) => return Err(err),
}
}
Ok((objects, deleted_objects, new_synctoken))
}
async fn _list_objects<'e, E: Executor<'e, Database = Sqlite>>(
executor: E,
principal: &str,
addressbook_id: &str,
) -> Result<Vec<(u64, bool)>, rustical_store::Error> {
struct ObjectEntry {
length: u64,
deleted: bool,
}
Ok(sqlx::query_as!(
ObjectEntry,
"SELECT length(vcf) AS 'length!: u64', deleted_at AS 'deleted!: bool' FROM addressobjects WHERE principal = ? AND addressbook_id = ?",
principal,
addressbook_id
)
.fetch_all(executor)
.await.map_err(crate::Error::from)?
.into_iter()
.map(|row| (row.length, row.deleted))
.collect())
}
async fn _get_objects<'e, E: Executor<'e, Database = Sqlite>>(
executor: E,
principal: &str,
addressbook_id: &str,
) -> Result<Vec<AddressObject>, rustical_store::Error> {
sqlx::query_as!(
AddressObjectRow,
"SELECT id, vcf FROM addressobjects WHERE principal = ? AND addressbook_id = ? AND deleted_at IS NULL",
principal,
addressbook_id
)
.fetch_all(executor)
.await.map_err(crate::Error::from)?
.into_iter()
.map(std::convert::TryInto::try_into)
.collect()
}
async fn _get_object<'e, E: Executor<'e, Database = Sqlite>>(
executor: E,
principal: &str,
addressbook_id: &str,
object_id: &str,
show_deleted: bool,
) -> Result<AddressObject, rustical_store::Error> {
sqlx::query_as!(
AddressObjectRow,
"SELECT id, vcf FROM addressobjects WHERE (principal, addressbook_id, id) = (?, ?, ?) AND ((deleted_at IS NULL) OR ?)",
principal,
addressbook_id,
object_id,
show_deleted
)
.fetch_one(executor)
.await
.map_err(crate::Error::from)?
.try_into()
}
async fn _put_object<'e, E: Executor<'e, Database = Sqlite>>(
executor: E,
principal: &str,
addressbook_id: &str,
object: &AddressObject,
overwrite: bool,
) -> Result<(), rustical_store::Error> {
let (object_id, vcf) = (object.get_id(), object.get_vcf());
(if overwrite {
sqlx::query!(
"REPLACE INTO addressobjects (principal, addressbook_id, id, vcf) VALUES (?, ?, ?, ?)",
principal,
addressbook_id,
object_id,
vcf
)
} else {
// If the object already exists a database error is thrown and handled in error.rs
sqlx::query!(
"INSERT INTO addressobjects (principal, addressbook_id, id, vcf) VALUES (?, ?, ?, ?)",
principal,
addressbook_id,
object_id,
vcf
)
})
.execute(executor)
.await
.map_err(crate::Error::from)?;
Ok(())
}
async fn _delete_object<'e, E: Executor<'e, Database = Sqlite>>(
executor: E,
principal: &str,
addressbook_id: &str,
object_id: &str,
use_trashbin: bool,
) -> Result<(), rustical_store::Error> {
if use_trashbin {
sqlx::query!(
"UPDATE addressobjects SET deleted_at = datetime(), updated_at = datetime() WHERE (principal, addressbook_id, id) = (?, ?, ?)",
principal,
addressbook_id,
object_id
)
.execute(executor)
.await.map_err(crate::Error::from)?;
} else {
sqlx::query!(
"DELETE FROM addressobjects WHERE addressbook_id = ? AND id = ?",
addressbook_id,
object_id
)
.execute(executor)
.await
.map_err(crate::Error::from)?;
}
Ok(())
}
async fn _restore_object<'e, E: Executor<'e, Database = Sqlite>>(
executor: E,
principal: &str,
addressbook_id: &str,
object_id: &str,
) -> Result<(), rustical_store::Error> {
sqlx::query!(
r#"UPDATE addressobjects SET deleted_at = NULL, updated_at = datetime() WHERE (principal, addressbook_id, id) = (?, ?, ?)"#,
principal,
addressbook_id,
object_id
)
.execute(executor)
.await.map_err(crate::Error::from)?;
Ok(())
}
}
#[async_trait]
impl AddressbookStore for SqliteAddressbookStore {
#[instrument]
async fn get_addressbook(
&self,
principal: &str,
id: &str,
show_deleted: bool,
) -> Result<Addressbook, rustical_store::Error> {
Self::_get_addressbook(&self.db, principal, id, show_deleted).await
}
#[instrument]
async fn get_addressbooks(
&self,
principal: &str,
) -> Result<Vec<Addressbook>, rustical_store::Error> {
Self::_get_addressbooks(&self.db, principal).await
}
#[instrument]
async fn get_deleted_addressbooks(
&self,
principal: &str,
) -> Result<Vec<Addressbook>, rustical_store::Error> {
Self::_get_deleted_addressbooks(&self.db, principal).await
}
#[instrument]
async fn update_addressbook(
&self,
principal: String,
id: String,
addressbook: Addressbook,
) -> Result<(), rustical_store::Error> {
Self::_update_addressbook(&self.db, principal, id, addressbook).await
}
#[instrument]
async fn insert_addressbook(
&self,
addressbook: Addressbook,
) -> Result<(), rustical_store::Error> {
let mut tx = self
.db
.begin_with(BEGIN_IMMEDIATE)
.await
.map_err(crate::Error::from)?;
Self::_insert_addressbook(&mut *tx, &addressbook).await?;
Self::_insert_birthday_calendar(&mut *tx, addressbook).await?;
tx.commit().await.map_err(crate::Error::from)?;
Ok(())
}
#[instrument]
async fn delete_addressbook(
&self,
principal: &str,
addressbook_id: &str,
use_trashbin: bool,
) -> Result<(), rustical_store::Error> {
let mut tx = self
.db
.begin_with(BEGIN_IMMEDIATE)
.await
.map_err(crate::Error::from)?;
let addressbook =
match Self::_get_addressbook(&mut *tx, principal, addressbook_id, use_trashbin).await {
Ok(addressbook) => Some(addressbook),
Err(Error::NotFound) => None,
Err(err) => return Err(err),
};
Self::_delete_addressbook(&mut *tx, principal, addressbook_id, use_trashbin).await?;
tx.commit().await.map_err(crate::Error::from)?;
if let Some(addressbook) = addressbook
&& let Err(err) = self.sender.try_send(CollectionOperation {
data: CollectionOperationInfo::Delete,
topic: addressbook.push_topic,
})
{
error!("Push notification about deleted addressbook failed: {err}");
}
Ok(())
}
#[instrument]
async fn restore_addressbook(
&self,
principal: &str,
addressbook_id: &str,
) -> Result<(), rustical_store::Error> {
Self::_restore_addressbook(&self.db, principal, addressbook_id).await
}
#[instrument]
async fn sync_changes(
&self,
principal: &str,
addressbook_id: &str,
synctoken: i64,
) -> Result<(Vec<AddressObject>, Vec<String>, i64), rustical_store::Error> {
Self::_sync_changes(&self.db, principal, addressbook_id, synctoken).await
}
#[instrument]
async fn addressbook_metadata(
&self,
principal: &str,
addressbook_id: &str,
) -> Result<CollectionMetadata, rustical_store::Error> {
let mut sizes = vec![];
let mut deleted_sizes = vec![];
for (size, deleted) in Self::_list_objects(&self.db, principal, addressbook_id).await? {
if deleted {
deleted_sizes.push(size);
} else {
sizes.push(size);
}
}
Ok(CollectionMetadata {
len: sizes.len(),
deleted_len: deleted_sizes.len(),
size: sizes.iter().sum(),
deleted_size: deleted_sizes.iter().sum(),
})
}
#[instrument]
async fn get_objects(
&self,
principal: &str,
addressbook_id: &str,
) -> Result<Vec<AddressObject>, rustical_store::Error> {
Self::_get_objects(&self.db, principal, addressbook_id).await
}
#[instrument]
async fn get_object(
&self,
principal: &str,
addressbook_id: &str,
object_id: &str,
show_deleted: bool,
) -> Result<AddressObject, rustical_store::Error> {
Self::_get_object(&self.db, principal, addressbook_id, object_id, show_deleted).await
}
#[instrument]
async fn put_object(
&self,
principal: String,
addressbook_id: String,
object: AddressObject,
overwrite: bool,
) -> Result<(), rustical_store::Error> {
let mut tx = self
.db
.begin_with(BEGIN_IMMEDIATE)
.await
.map_err(crate::Error::from)?;
let object_id = object.get_id().to_owned();
Self::_put_object(&mut *tx, &principal, &addressbook_id, &object, overwrite).await?;
let sync_token = log_object_operation(
&mut tx,
&principal,
&addressbook_id,
&object_id,
ChangeOperation::Add,
)
.await
.map_err(crate::Error::from)?;
tx.commit().await.map_err(crate::Error::from)?;
if let Err(err) = self.sender.try_send(CollectionOperation {
data: CollectionOperationInfo::Content { sync_token },
topic: self
.get_addressbook(&principal, &addressbook_id, false)
.await?
.push_topic,
}) {
error!("Push notification about deleted addressbook failed: {err}");
}
Ok(())
}
#[instrument]
async fn delete_object(
&self,
principal: &str,
addressbook_id: &str,
object_id: &str,
use_trashbin: bool,
) -> Result<(), rustical_store::Error> {
let mut tx = self
.db
.begin_with(BEGIN_IMMEDIATE)
.await
.map_err(crate::Error::from)?;
Self::_delete_object(&mut *tx, principal, addressbook_id, object_id, use_trashbin).await?;
let sync_token = log_object_operation(
&mut tx,
principal,
addressbook_id,
object_id,
ChangeOperation::Delete,
)
.await
.map_err(crate::Error::from)?;
tx.commit().await.map_err(crate::Error::from)?;
if let Err(err) = self.sender.try_send(CollectionOperation {
data: CollectionOperationInfo::Content { sync_token },
topic: self
.get_addressbook(principal, addressbook_id, false)
.await?
.push_topic,
}) {
error!("Push notification about deleted addressbook failed: {err}");
}
Ok(())
}
#[instrument]
async fn restore_object(
&self,
principal: &str,
addressbook_id: &str,
object_id: &str,
) -> Result<(), rustical_store::Error> {
let mut tx = self
.db
.begin_with(BEGIN_IMMEDIATE)
.await
.map_err(crate::Error::from)?;
Self::_restore_object(&mut *tx, principal, addressbook_id, object_id).await?;
let sync_token = log_object_operation(
&mut tx,
principal,
addressbook_id,
object_id,
ChangeOperation::Add,
)
.await
.map_err(crate::Error::from)?;
tx.commit().await.map_err(crate::Error::from)?;
if let Err(err) = self.sender.try_send(CollectionOperation {
data: CollectionOperationInfo::Content { sync_token },
topic: self
.get_addressbook(principal, addressbook_id, false)
.await?
.push_topic,
}) {
error!("Push notification about deleted addressbook failed: {err}");
}
Ok(())
}
#[instrument(skip(objects))]
async fn import_addressbook(
&self,
addressbook: Addressbook,
objects: Vec<AddressObject>,
merge_existing: bool,
) -> Result<(), Error> {
let mut tx = self
.db
.begin_with(BEGIN_IMMEDIATE)
.await
.map_err(crate::Error::from)?;
let existing =
match Self::_get_addressbook(&mut *tx, &addressbook.principal, &addressbook.id, true)
.await
{
Ok(addressbook) => Some(addressbook),
Err(Error::NotFound) => None,
Err(err) => return Err(err),
};
if existing.is_some() && !merge_existing {
return Err(Error::AlreadyExists);
}
if existing.is_none() {
Self::_insert_addressbook(&mut *tx, &addressbook).await?;
}
for object in objects {
Self::_put_object(
&mut *tx,
&addressbook.principal,
&addressbook.id,
&object,
false,
)
.await?;
}
tx.commit().await.map_err(crate::Error::from)?;
Ok(())
}
}
// Logs an operation to an address object
async fn log_object_operation(
tx: &mut Transaction<'_, Sqlite>,
principal: &str,
addressbook_id: &str,
object_id: &str,
operation: ChangeOperation,
) -> Result<String, sqlx::Error> {
struct Synctoken {
synctoken: i64,
}
let Synctoken { synctoken } = sqlx::query_as!(
Synctoken,
r#"
UPDATE addressbooks
SET synctoken = synctoken + 1
WHERE (principal, id) = (?1, ?2)
RETURNING synctoken"#,
principal,
addressbook_id
)
.fetch_one(&mut **tx)
.await?;
sqlx::query!(
r#"
INSERT INTO addressobjectchangelog (principal, addressbook_id, object_id, "operation", synctoken)
VALUES (?1, ?2, ?3, ?4, (
SELECT synctoken FROM addressbooks WHERE (principal, id) = (?1, ?2)
))"#,
principal,
addressbook_id,
object_id,
operation
)
.execute(&mut **tx)
.await?;
Ok(format_synctoken(synctoken))
}