mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 14:02:29 +00:00
Add initial carddav support
This commit is contained in:
64
crates/store/src/addressbook_store.rs
Normal file
64
crates/store/src/addressbook_store.rs
Normal file
@@ -0,0 +1,64 @@
|
||||
use crate::{
|
||||
error::Error,
|
||||
model::{AddressObject, Addressbook},
|
||||
};
|
||||
use async_trait::async_trait;
|
||||
|
||||
#[async_trait]
|
||||
pub trait AddressbookStore: Send + Sync + 'static {
|
||||
async fn get_addressbook(&self, principal: &str, id: &str) -> Result<Addressbook, Error>;
|
||||
async fn get_addressbooks(&self, principal: &str) -> Result<Vec<Addressbook>, Error>;
|
||||
|
||||
async fn update_addressbook(
|
||||
&mut self,
|
||||
principal: String,
|
||||
id: String,
|
||||
addressbook: Addressbook,
|
||||
) -> Result<(), Error>;
|
||||
async fn insert_addressbook(&mut self, addressbook: Addressbook) -> Result<(), Error>;
|
||||
async fn delete_addressbook(
|
||||
&mut self,
|
||||
principal: &str,
|
||||
name: &str,
|
||||
use_trashbin: bool,
|
||||
) -> Result<(), Error>;
|
||||
async fn restore_addressbook(&mut self, principal: &str, name: &str) -> Result<(), Error>;
|
||||
|
||||
async fn sync_changes(
|
||||
&self,
|
||||
principal: &str,
|
||||
addressbook_id: &str,
|
||||
synctoken: i64,
|
||||
) -> Result<(Vec<AddressObject>, Vec<String>, i64), Error>;
|
||||
|
||||
async fn get_objects(
|
||||
&self,
|
||||
principal: &str,
|
||||
addressbook_id: &str,
|
||||
) -> Result<Vec<AddressObject>, Error>;
|
||||
async fn get_object(
|
||||
&self,
|
||||
principal: &str,
|
||||
addressbook_id: &str,
|
||||
object_id: &str,
|
||||
) -> Result<AddressObject, Error>;
|
||||
async fn put_object(
|
||||
&mut self,
|
||||
principal: String,
|
||||
addressbook_id: String,
|
||||
object: AddressObject,
|
||||
) -> Result<(), Error>;
|
||||
async fn delete_object(
|
||||
&mut self,
|
||||
principal: &str,
|
||||
addressbook_id: &str,
|
||||
object_id: &str,
|
||||
use_trashbin: bool,
|
||||
) -> Result<(), Error>;
|
||||
async fn restore_object(
|
||||
&mut self,
|
||||
principal: &str,
|
||||
addressbook_id: &str,
|
||||
object_id: &str,
|
||||
) -> Result<(), Error>;
|
||||
}
|
||||
@@ -1,9 +1,7 @@
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
|
||||
use crate::error::Error;
|
||||
use crate::model::object::CalendarObject;
|
||||
use crate::model::Calendar;
|
||||
use async_trait::async_trait;
|
||||
|
||||
#[async_trait]
|
||||
pub trait CalendarStore: Send + Sync + 'static {
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
pub mod addressbook_store;
|
||||
pub mod calendar_store;
|
||||
pub mod error;
|
||||
pub mod model;
|
||||
pub mod sqlite_store;
|
||||
pub mod timestamp;
|
||||
pub use calendar_store::CalendarStore;
|
||||
pub use error::Error;
|
||||
pub mod auth;
|
||||
|
||||
pub use addressbook_store::AddressbookStore;
|
||||
pub use calendar_store::CalendarStore;
|
||||
|
||||
28
crates/store/src/model/address_object.rs
Normal file
28
crates/store/src/model/address_object.rs
Normal file
@@ -0,0 +1,28 @@
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
use crate::Error;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AddressObject {
|
||||
id: String,
|
||||
vcf: String,
|
||||
}
|
||||
|
||||
impl AddressObject {
|
||||
pub fn from_vcf(object_id: String, vcf: String) -> Result<Self, Error> {
|
||||
Ok(Self { id: object_id, vcf })
|
||||
}
|
||||
pub fn get_id(&self) -> &str {
|
||||
&self.id
|
||||
}
|
||||
pub fn get_etag(&self) -> String {
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(&self.id);
|
||||
hasher.update(self.get_vcf());
|
||||
format!("{:x}", hasher.finalize())
|
||||
}
|
||||
|
||||
pub fn get_vcf(&self) -> &str {
|
||||
&self.vcf
|
||||
}
|
||||
}
|
||||
32
crates/store/src/model/addressbook.rs
Normal file
32
crates/store/src/model/addressbook.rs
Normal file
@@ -0,0 +1,32 @@
|
||||
use chrono::NaiveDateTime;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Addressbook {
|
||||
pub id: String,
|
||||
pub principal: String,
|
||||
pub displayname: Option<String>,
|
||||
pub description: Option<String>,
|
||||
pub deleted_at: Option<NaiveDateTime>,
|
||||
pub synctoken: i64,
|
||||
}
|
||||
|
||||
impl Addressbook {
|
||||
pub fn format_synctoken(&self) -> String {
|
||||
format_synctoken(self.synctoken)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: make nicer
|
||||
const SYNC_NAMESPACE: &str = "github.com/lennart-k/rustical/ns/";
|
||||
|
||||
pub fn format_synctoken(synctoken: i64) -> String {
|
||||
format!("{}{}", SYNC_NAMESPACE, synctoken)
|
||||
}
|
||||
|
||||
pub fn parse_synctoken(synctoken: &str) -> Option<i64> {
|
||||
if !synctoken.starts_with(SYNC_NAMESPACE) {
|
||||
return None;
|
||||
}
|
||||
let (_, synctoken) = synctoken.split_at(SYNC_NAMESPACE.len());
|
||||
synctoken.parse::<i64>().ok()
|
||||
}
|
||||
@@ -5,3 +5,9 @@ pub mod todo;
|
||||
|
||||
pub use calendar::Calendar;
|
||||
pub use object::CalendarObject;
|
||||
|
||||
pub mod addressbook;
|
||||
pub use addressbook::Addressbook;
|
||||
|
||||
pub mod address_object;
|
||||
pub use address_object::AddressObject;
|
||||
|
||||
371
crates/store/src/sqlite_store/addressbook_store.rs
Normal file
371
crates/store/src/sqlite_store/addressbook_store.rs
Normal file
@@ -0,0 +1,371 @@
|
||||
use super::SqliteStore;
|
||||
use crate::Error;
|
||||
use crate::{
|
||||
model::{AddressObject, Addressbook},
|
||||
AddressbookStore,
|
||||
};
|
||||
use async_trait::async_trait;
|
||||
use serde::Serialize;
|
||||
use sqlx::{Sqlite, Transaction};
|
||||
use tracing::instrument;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct AddressObjectRow {
|
||||
id: String,
|
||||
vcf: String,
|
||||
}
|
||||
|
||||
impl TryFrom<AddressObjectRow> for AddressObject {
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(value: AddressObjectRow) -> Result<Self, Error> {
|
||||
Self::from_vcf(value.id, value.vcf)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, sqlx::Type)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
enum AddressbookChangeOperation {
|
||||
// There's no distinction between Add and Modify
|
||||
Add,
|
||||
Delete,
|
||||
}
|
||||
|
||||
// Logs an operation to the events
|
||||
async fn log_object_operation(
|
||||
tx: &mut Transaction<'_, Sqlite>,
|
||||
principal: &str,
|
||||
addressbook_id: &str,
|
||||
object_id: &str,
|
||||
operation: AddressbookChangeOperation,
|
||||
) -> Result<(), Error> {
|
||||
sqlx::query!(
|
||||
r#"
|
||||
UPDATE addressbooks
|
||||
SET synctoken = synctoken + 1
|
||||
WHERE (principal, id) = (?1, ?2)"#,
|
||||
principal,
|
||||
addressbook_id
|
||||
)
|
||||
.execute(&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(())
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl AddressbookStore for SqliteStore {
|
||||
#[instrument]
|
||||
async fn get_addressbook(&self, principal: &str, id: &str) -> Result<Addressbook, Error> {
|
||||
let addressbook = sqlx::query_as!(
|
||||
Addressbook,
|
||||
r#"SELECT principal, id, synctoken, displayname, description, deleted_at
|
||||
FROM addressbooks
|
||||
WHERE (principal, id) = (?, ?)"#,
|
||||
principal,
|
||||
id
|
||||
)
|
||||
.fetch_one(&self.db)
|
||||
.await?;
|
||||
Ok(addressbook)
|
||||
}
|
||||
|
||||
#[instrument]
|
||||
async fn get_addressbooks(&self, principal: &str) -> Result<Vec<Addressbook>, Error> {
|
||||
let addressbooks = sqlx::query_as!(
|
||||
Addressbook,
|
||||
r#"SELECT principal, id, synctoken, displayname, description, deleted_at
|
||||
FROM addressbooks
|
||||
WHERE principal = ? AND deleted_at IS NULL"#,
|
||||
principal
|
||||
)
|
||||
.fetch_all(&self.db)
|
||||
.await?;
|
||||
Ok(addressbooks)
|
||||
}
|
||||
|
||||
#[instrument]
|
||||
async fn update_addressbook(
|
||||
&mut self,
|
||||
principal: String,
|
||||
id: String,
|
||||
addressbook: Addressbook,
|
||||
) -> Result<(), Error> {
|
||||
let result = sqlx::query!(
|
||||
r#"UPDATE addressbooks SET principal = ?, id = ?, displayname = ?, description = ?
|
||||
WHERE (principal, id) = (?, ?)"#,
|
||||
addressbook.principal,
|
||||
addressbook.id,
|
||||
addressbook.displayname,
|
||||
addressbook.description,
|
||||
principal,
|
||||
id
|
||||
)
|
||||
.execute(&self.db)
|
||||
.await?;
|
||||
if result.rows_affected() == 0 {
|
||||
return Err(Error::NotFound);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[instrument]
|
||||
async fn insert_addressbook(&mut self, addressbook: Addressbook) -> Result<(), Error> {
|
||||
sqlx::query!(
|
||||
r#"INSERT INTO addressbooks (principal, id, displayname, description)
|
||||
VALUES (?, ?, ?, ?)"#,
|
||||
addressbook.principal,
|
||||
addressbook.id,
|
||||
addressbook.displayname,
|
||||
addressbook.description,
|
||||
)
|
||||
.execute(&self.db)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[instrument]
|
||||
async fn delete_addressbook(
|
||||
&mut self,
|
||||
principal: &str,
|
||||
addressbook_id: &str,
|
||||
use_trashbin: bool,
|
||||
) -> Result<(), Error> {
|
||||
match use_trashbin {
|
||||
true => {
|
||||
sqlx::query!(
|
||||
r#"UPDATE addressbooks SET deleted_at = datetime() WHERE (principal, id) = (?, ?)"#,
|
||||
principal, addressbook_id
|
||||
)
|
||||
.execute(&self.db)
|
||||
.await?;
|
||||
}
|
||||
false => {
|
||||
sqlx::query!(
|
||||
r#"DELETE FROM addressbooks WHERE (principal, id) = (?, ?)"#,
|
||||
principal,
|
||||
addressbook_id
|
||||
)
|
||||
.execute(&self.db)
|
||||
.await?;
|
||||
}
|
||||
};
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[instrument]
|
||||
async fn restore_addressbook(
|
||||
&mut self,
|
||||
principal: &str,
|
||||
addressbook_id: &str,
|
||||
) -> Result<(), Error> {
|
||||
sqlx::query!(
|
||||
r"UPDATE addressbooks SET deleted_at = NULL WHERE (principal, id) = (?, ?)",
|
||||
principal,
|
||||
addressbook_id
|
||||
)
|
||||
.execute(&self.db)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[instrument]
|
||||
async fn sync_changes(
|
||||
&self,
|
||||
principal: &str,
|
||||
addressbook_id: &str,
|
||||
synctoken: i64,
|
||||
) -> Result<(Vec<AddressObject>, Vec<String>, i64), Error> {
|
||||
struct Row {
|
||||
object_id: String,
|
||||
synctoken: i64,
|
||||
}
|
||||
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(&self.db)
|
||||
.await?;
|
||||
|
||||
let mut objects = vec![];
|
||||
let mut deleted_objects = vec![];
|
||||
|
||||
let new_synctoken = changes
|
||||
.last()
|
||||
.map(|&Row { synctoken, .. }| synctoken)
|
||||
.unwrap_or(0);
|
||||
|
||||
for Row { object_id, .. } in changes {
|
||||
match self.get_object(principal, addressbook_id, &object_id).await {
|
||||
Ok(object) => objects.push(object),
|
||||
Err(Error::NotFound) => deleted_objects.push(object_id),
|
||||
Err(err) => return Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
Ok((objects, deleted_objects, new_synctoken))
|
||||
}
|
||||
|
||||
#[instrument]
|
||||
async fn get_objects(
|
||||
&self,
|
||||
principal: &str,
|
||||
addressbook_id: &str,
|
||||
) -> Result<Vec<AddressObject>, 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(&self.db)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|row| row.try_into())
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[instrument]
|
||||
async fn get_object(
|
||||
&self,
|
||||
principal: &str,
|
||||
addressbook_id: &str,
|
||||
object_id: &str,
|
||||
) -> Result<AddressObject, Error> {
|
||||
Ok(sqlx::query_as!(
|
||||
AddressObjectRow,
|
||||
"SELECT id, vcf FROM addressobjects WHERE (principal, addressbook_id, id) = (?, ?, ?)",
|
||||
principal,
|
||||
addressbook_id,
|
||||
object_id
|
||||
)
|
||||
.fetch_one(&self.db)
|
||||
.await?
|
||||
.try_into()?)
|
||||
}
|
||||
|
||||
#[instrument]
|
||||
async fn put_object(
|
||||
&mut self,
|
||||
principal: String,
|
||||
addressbook_id: String,
|
||||
object: AddressObject,
|
||||
) -> Result<(), Error> {
|
||||
let mut tx = self.db.begin().await?;
|
||||
|
||||
let (object_id, vcf) = (object.get_id(), object.get_vcf());
|
||||
|
||||
sqlx::query!(
|
||||
"REPLACE INTO addressobjects (principal, addressbook_id, id, vcf) VALUES (?, ?, ?, ?)",
|
||||
principal,
|
||||
addressbook_id,
|
||||
object_id,
|
||||
vcf
|
||||
)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
|
||||
log_object_operation(
|
||||
&mut tx,
|
||||
&principal,
|
||||
&addressbook_id,
|
||||
object_id,
|
||||
AddressbookChangeOperation::Add,
|
||||
)
|
||||
.await?;
|
||||
|
||||
tx.commit().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[instrument]
|
||||
async fn delete_object(
|
||||
&mut self,
|
||||
principal: &str,
|
||||
addressbook_id: &str,
|
||||
object_id: &str,
|
||||
use_trashbin: bool,
|
||||
) -> Result<(), Error> {
|
||||
let mut tx = self.db.begin().await?;
|
||||
|
||||
match use_trashbin {
|
||||
true => {
|
||||
sqlx::query!(
|
||||
"UPDATE addressobjects SET deleted_at = datetime(), updated_at = datetime() WHERE (principal, addressbook_id, id) = (?, ?, ?)",
|
||||
principal,
|
||||
addressbook_id,
|
||||
object_id
|
||||
)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
}
|
||||
false => {
|
||||
sqlx::query!(
|
||||
"DELETE FROM addressobjects WHERE addressbook_id = ? AND id = ?",
|
||||
addressbook_id,
|
||||
object_id
|
||||
)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
}
|
||||
};
|
||||
log_object_operation(
|
||||
&mut tx,
|
||||
principal,
|
||||
addressbook_id,
|
||||
object_id,
|
||||
AddressbookChangeOperation::Delete,
|
||||
)
|
||||
.await?;
|
||||
tx.commit().await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[instrument]
|
||||
async fn restore_object(
|
||||
&mut self,
|
||||
principal: &str,
|
||||
addressbook_id: &str,
|
||||
object_id: &str,
|
||||
) -> Result<(), Error> {
|
||||
let mut tx = self.db.begin().await?;
|
||||
|
||||
sqlx::query!(
|
||||
r#"UPDATE addressobjects SET deleted_at = NULL, updated_at = datetime() WHERE (principal, addressbook_id, id) = (?, ?, ?)"#,
|
||||
principal,
|
||||
addressbook_id,
|
||||
object_id
|
||||
)
|
||||
.execute(&mut *tx)
|
||||
.await?;
|
||||
|
||||
log_object_operation(
|
||||
&mut tx,
|
||||
principal,
|
||||
addressbook_id,
|
||||
object_id,
|
||||
AddressbookChangeOperation::Delete,
|
||||
)
|
||||
.await?;
|
||||
tx.commit().await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -1,24 +1,14 @@
|
||||
use super::SqliteStore;
|
||||
use crate::model::object::CalendarObject;
|
||||
use crate::model::Calendar;
|
||||
use crate::{CalendarStore, Error};
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use serde::Serialize;
|
||||
use sqlx::Sqlite;
|
||||
use sqlx::Transaction;
|
||||
use sqlx::{sqlite::SqliteConnectOptions, Pool, Sqlite, SqlitePool};
|
||||
use tracing::instrument;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct SqliteCalendarStore {
|
||||
db: SqlitePool,
|
||||
}
|
||||
|
||||
impl SqliteCalendarStore {
|
||||
pub fn new(db: SqlitePool) -> Self {
|
||||
Self { db }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct CalendarObjectRow {
|
||||
id: String,
|
||||
@@ -77,7 +67,7 @@ async fn log_object_operation(
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl CalendarStore for SqliteCalendarStore {
|
||||
impl CalendarStore for SqliteStore {
|
||||
#[instrument]
|
||||
async fn get_calendar(&self, principal: &str, id: &str) -> Result<Calendar, Error> {
|
||||
let cal = sqlx::query_as!(
|
||||
@@ -380,23 +370,3 @@ impl CalendarStore for SqliteCalendarStore {
|
||||
Ok((objects, deleted_objects, new_synctoken))
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn create_db_pool(db_url: &str, migrate: bool) -> anyhow::Result<Pool<Sqlite>> {
|
||||
let db = SqlitePool::connect_with(
|
||||
SqliteConnectOptions::new()
|
||||
.filename(db_url)
|
||||
.create_if_missing(true),
|
||||
)
|
||||
.await?;
|
||||
if migrate {
|
||||
println!("Running database migrations");
|
||||
sqlx::migrate!("./migrations").run(&db).await?;
|
||||
}
|
||||
Ok(db)
|
||||
}
|
||||
|
||||
pub async fn create_test_store() -> anyhow::Result<SqliteCalendarStore> {
|
||||
let db = SqlitePool::connect("sqlite::memory:").await?;
|
||||
sqlx::migrate!("./migrations").run(&db).await?;
|
||||
Ok(SqliteCalendarStore::new(db))
|
||||
}
|
||||
35
crates/store/src/sqlite_store/mod.rs
Normal file
35
crates/store/src/sqlite_store/mod.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
use sqlx::{sqlite::SqliteConnectOptions, Pool, Sqlite, SqlitePool};
|
||||
|
||||
pub mod addressbook_store;
|
||||
pub mod calendar_store;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct SqliteStore {
|
||||
db: SqlitePool,
|
||||
}
|
||||
|
||||
impl SqliteStore {
|
||||
pub fn new(db: SqlitePool) -> Self {
|
||||
Self { db }
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn create_db_pool(db_url: &str, migrate: bool) -> anyhow::Result<Pool<Sqlite>> {
|
||||
let db = SqlitePool::connect_with(
|
||||
SqliteConnectOptions::new()
|
||||
.filename(db_url)
|
||||
.create_if_missing(true),
|
||||
)
|
||||
.await?;
|
||||
if migrate {
|
||||
println!("Running database migrations");
|
||||
sqlx::migrate!("./migrations").run(&db).await?;
|
||||
}
|
||||
Ok(db)
|
||||
}
|
||||
|
||||
pub async fn create_test_store() -> anyhow::Result<SqliteStore> {
|
||||
let db = SqlitePool::connect("sqlite::memory:").await?;
|
||||
sqlx::migrate!("./migrations").run(&db).await?;
|
||||
Ok(SqliteStore::new(db))
|
||||
}
|
||||
Reference in New Issue
Block a user