mirror of
https://github.com/lennart-k/rustical.git
synced 2025-12-14 08:12:24 +00:00
frontend: Add form to create addressbook
This commit is contained in:
@@ -3,7 +3,7 @@ use std::sync::Arc;
|
||||
use askama::Template;
|
||||
use askama_web::WebTemplate;
|
||||
use axum::{
|
||||
Extension,
|
||||
Extension, Form,
|
||||
extract::Path,
|
||||
response::{IntoResponse, Redirect, Response},
|
||||
};
|
||||
@@ -11,6 +11,7 @@ use axum_extra::TypedHeader;
|
||||
use headers::Referer;
|
||||
use http::StatusCode;
|
||||
use rustical_store::{Addressbook, AddressbookStore, auth::User};
|
||||
use serde::{Deserialize, Deserializer};
|
||||
|
||||
#[derive(Template, WebTemplate)]
|
||||
#[template(path = "pages/addressbook.html")]
|
||||
@@ -32,6 +33,53 @@ pub async fn route_addressbook<AS: AddressbookStore>(
|
||||
.into_response())
|
||||
}
|
||||
|
||||
fn empty_to_none<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let val: Option<String> = Deserialize::deserialize(deserializer)?;
|
||||
Ok(val.filter(|val| !val.is_empty()))
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Clone)]
|
||||
pub struct PutAddressbookForm {
|
||||
id: String,
|
||||
#[serde(deserialize_with = "empty_to_none")]
|
||||
displayname: Option<String>,
|
||||
#[serde(deserialize_with = "empty_to_none")]
|
||||
description: Option<String>,
|
||||
}
|
||||
|
||||
pub async fn route_create_addressbook<AS: AddressbookStore>(
|
||||
Path(owner): Path<String>,
|
||||
Extension(store): Extension<Arc<AS>>,
|
||||
user: User,
|
||||
Form(PutAddressbookForm {
|
||||
id,
|
||||
displayname,
|
||||
description,
|
||||
}): Form<PutAddressbookForm>,
|
||||
) -> Result<Response, rustical_store::Error> {
|
||||
if !user.is_principal(&owner) {
|
||||
return Ok(StatusCode::UNAUTHORIZED.into_response());
|
||||
}
|
||||
|
||||
assert!(!id.is_empty());
|
||||
|
||||
let addressbook = Addressbook {
|
||||
id: id.to_owned(),
|
||||
displayname,
|
||||
description,
|
||||
principal: user.id.to_owned(),
|
||||
synctoken: 0,
|
||||
deleted_at: None,
|
||||
push_topic: uuid::Uuid::new_v4().to_string(),
|
||||
};
|
||||
|
||||
store.insert_addressbook(addressbook).await?;
|
||||
Ok(Redirect::to(&format!("/frontend/user/{}/addressbook/{}", user.id, id)).into_response())
|
||||
}
|
||||
|
||||
pub async fn route_addressbook_restore<AS: AddressbookStore>(
|
||||
Path((owner, addressbook_id)): Path<(String, String)>,
|
||||
Extension(store): Extension<Arc<AS>>,
|
||||
|
||||
Reference in New Issue
Block a user