carddav: Change addressbook PUT to IMPORT

This commit is contained in:
Lennart
2025-08-23 19:01:19 +02:00
parent f22d5ca04b
commit 94ace71745
6 changed files with 100 additions and 62 deletions

View File

@@ -0,0 +1,63 @@
use std::io::BufReader;
use crate::Error;
use crate::addressbook::AddressbookResourceService;
use axum::{
extract::{Path, State},
response::{IntoResponse, Response},
};
use http::StatusCode;
use ical::{
parser::{Component, ComponentMut, vcard},
property::Property,
};
use rustical_store::{Addressbook, AddressbookStore, SubscriptionStore, auth::Principal};
use tracing::instrument;
#[instrument(skip(resource_service))]
pub async fn route_import<AS: AddressbookStore, S: SubscriptionStore>(
Path((principal, addressbook_id)): Path<(String, String)>,
user: Principal,
State(resource_service): State<AddressbookResourceService<AS, S>>,
body: String,
) -> Result<Response, Error> {
if !user.is_principal(&principal) {
return Err(Error::Unauthorized);
}
let parser = vcard::VcardParser::new(BufReader::new(body.as_bytes()));
let mut objects = vec![];
for res in parser {
let mut card = res.unwrap();
let uid = card.get_uid();
if uid.is_none() {
let mut card_mut = card.mutable();
card_mut.set_property(Property {
name: "UID".to_owned(),
value: Some(uuid::Uuid::new_v4().to_string()),
params: None,
});
card = card_mut.verify().unwrap();
}
objects.push(card.try_into().unwrap());
}
let addressbook = Addressbook {
principal,
id: addressbook_id,
displayname: None,
description: None,
deleted_at: None,
synctoken: 0,
push_topic: uuid::Uuid::new_v4().to_string(),
};
let addr_store = resource_service.addr_store;
addr_store
.import_addressbook(addressbook, objects, false)
.await?;
Ok(StatusCode::OK.into_response())
}

View File

@@ -1,5 +1,5 @@
pub mod get; pub mod get;
pub mod import;
pub mod mkcol; pub mod mkcol;
pub mod post; pub mod post;
pub mod put;
pub mod report; pub mod report;

View File

@@ -1,47 +0,0 @@
use crate::Error;
use crate::addressbook::AddressbookResourceService;
use axum::response::IntoResponse;
use axum::{
extract::{Path, State},
response::Response,
};
use http::StatusCode;
use ical::VcardParser;
use rustical_ical::AddressObject;
use rustical_store::Addressbook;
use rustical_store::{AddressbookStore, SubscriptionStore, auth::Principal};
use tracing::instrument;
#[instrument(skip(addr_store))]
pub async fn route_put<AS: AddressbookStore, S: SubscriptionStore>(
Path((principal, addressbook_id)): Path<(String, String)>,
State(AddressbookResourceService { addr_store, .. }): State<AddressbookResourceService<AS, S>>,
user: Principal,
body: String,
) -> Result<Response, Error> {
if !user.is_principal(&principal) {
return Err(Error::Unauthorized);
}
let mut objects = vec![];
for object in VcardParser::new(body.as_bytes()) {
let object = object.map_err(rustical_ical::Error::from)?;
objects.push(AddressObject::try_from(object)?);
}
let addressbook = Addressbook {
id: addressbook_id.clone(),
principal: principal.clone(),
displayname: None,
description: None,
deleted_at: None,
synctoken: Default::default(),
push_topic: uuid::Uuid::new_v4().to_string(),
};
addr_store
.import_addressbook(principal.clone(), addressbook, objects)
.await?;
Ok(StatusCode::CREATED.into_response())
}

View File

@@ -3,8 +3,8 @@ use super::methods::report::route_report_addressbook;
use crate::address_object::AddressObjectResourceService; use crate::address_object::AddressObjectResourceService;
use crate::address_object::resource::AddressObjectResource; use crate::address_object::resource::AddressObjectResource;
use crate::addressbook::methods::get::route_get; use crate::addressbook::methods::get::route_get;
use crate::addressbook::methods::import::route_import;
use crate::addressbook::methods::post::route_post; use crate::addressbook::methods::post::route_post;
use crate::addressbook::methods::put::route_put;
use crate::addressbook::resource::AddressbookResource; use crate::addressbook::resource::AddressbookResource;
use crate::{CardDavPrincipalUri, Error}; use crate::{CardDavPrincipalUri, Error};
use async_trait::async_trait; use async_trait::async_trait;
@@ -139,9 +139,9 @@ impl<AS: AddressbookStore, S: SubscriptionStore> AxumMethods for AddressbookReso
}) })
} }
fn put() -> Option<fn(Self, Request) -> BoxFuture<'static, Result<Response, Infallible>>> { fn import() -> Option<fn(Self, Request) -> BoxFuture<'static, Result<Response, Infallible>>> {
Some(|state, req| { Some(|state, req| {
let mut service = Handler::with_state(route_put::<AS, S>, state); let mut service = Handler::with_state(route_import::<AS, S>, state);
Box::pin(Service::call(&mut service, req)) Box::pin(Service::call(&mut service, req))
}) })
} }

View File

@@ -76,8 +76,8 @@ pub trait AddressbookStore: Send + Sync + 'static {
async fn import_addressbook( async fn import_addressbook(
&self, &self,
principal: String,
addressbook: Addressbook, addressbook: Addressbook,
objects: Vec<AddressObject>, objects: Vec<AddressObject>,
merge_existing: bool,
) -> Result<(), Error>; ) -> Result<(), Error>;
} }

View File

@@ -17,10 +17,20 @@ struct AddressObjectRow {
} }
impl TryFrom<AddressObjectRow> for AddressObject { impl TryFrom<AddressObjectRow> for AddressObject {
type Error = crate::Error; type Error = rustical_store::Error;
fn try_from(value: AddressObjectRow) -> Result<Self, Self::Error> { fn try_from(value: AddressObjectRow) -> Result<Self, Self::Error> {
Ok(Self::from_vcf(value.id, value.vcf)?) let object = Self::from_vcf(value.vcf)?;
if object.get_id() != value.id {
return Err(rustical_store::Error::IcalError(
rustical_ical::Error::InvalidData(format!(
"object_id={} and UID={} don't match",
object.get_id(),
value.id
)),
));
}
Ok(object)
} }
} }
@@ -259,7 +269,7 @@ impl SqliteAddressbookStore {
.fetch_all(executor) .fetch_all(executor)
.await.map_err(crate::Error::from)? .await.map_err(crate::Error::from)?
.into_iter() .into_iter()
.map(|row| row.try_into().map_err(rustical_store::Error::from)) .map(|row| row.try_into())
.collect() .collect()
} }
@@ -270,7 +280,7 @@ impl SqliteAddressbookStore {
object_id: &str, object_id: &str,
show_deleted: bool, show_deleted: bool,
) -> Result<AddressObject, rustical_store::Error> { ) -> Result<AddressObject, rustical_store::Error> {
Ok(sqlx::query_as!( sqlx::query_as!(
AddressObjectRow, AddressObjectRow,
"SELECT id, vcf FROM addressobjects WHERE (principal, addressbook_id, id) = (?, ?, ?) AND ((deleted_at IS NULL) OR ?)", "SELECT id, vcf FROM addressobjects WHERE (principal, addressbook_id, id) = (?, ?, ?) AND ((deleted_at IS NULL) OR ?)",
principal, principal,
@@ -281,7 +291,7 @@ impl SqliteAddressbookStore {
.fetch_one(executor) .fetch_one(executor)
.await .await
.map_err(crate::Error::from)? .map_err(crate::Error::from)?
.try_into()?) .try_into()
} }
async fn _put_object<'e, E: Executor<'e, Database = Sqlite>>( async fn _put_object<'e, E: Executor<'e, Database = Sqlite>>(
@@ -627,20 +637,32 @@ impl AddressbookStore for SqliteAddressbookStore {
#[instrument(skip(objects))] #[instrument(skip(objects))]
async fn import_addressbook( async fn import_addressbook(
&self, &self,
principal: String,
addressbook: Addressbook, addressbook: Addressbook,
objects: Vec<AddressObject>, objects: Vec<AddressObject>,
merge_existing: bool,
) -> Result<(), Error> { ) -> Result<(), Error> {
let mut tx = self.db.begin().await.map_err(crate::Error::from)?; let mut tx = self.db.begin().await.map_err(crate::Error::from)?;
let addressbook_id = addressbook.id.clone(); let existing =
Self::_insert_addressbook(&mut *tx, addressbook).await?; 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.clone()).await?;
}
for object in objects { for object in objects {
Self::_put_object( Self::_put_object(
&mut *tx, &mut *tx,
principal.clone(), addressbook.principal.clone(),
addressbook_id.clone(), addressbook.id.clone(),
object, object,
false, false,
) )