mirror of
https://github.com/perstarkse/minne.git
synced 2026-05-30 11:20:46 +02:00
wip: heavy refactoring html routers
This commit is contained in:
@@ -1,65 +1,42 @@
|
||||
use axum::{
|
||||
extract::State,
|
||||
http::{StatusCode, Uri},
|
||||
response::{IntoResponse, Redirect},
|
||||
Form,
|
||||
};
|
||||
use axum_htmx::HxRedirect;
|
||||
use axum_session_auth::AuthSession;
|
||||
use axum_session_surreal::SessionSurrealPool;
|
||||
use axum::{extract::State, response::IntoResponse, Form};
|
||||
use chrono_tz::TZ_VARIANTS;
|
||||
use surrealdb::{engine::any::Any, Surreal};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use common::{
|
||||
error::{AppError, HtmlError},
|
||||
storage::types::user::User,
|
||||
use crate::{
|
||||
middleware_auth::RequireUser,
|
||||
template_response::{HtmlError, TemplateResponse},
|
||||
AuthSessionType,
|
||||
};
|
||||
use common::storage::types::user::User;
|
||||
|
||||
use crate::{html_state::HtmlState, page_data};
|
||||
use crate::html_state::HtmlState;
|
||||
|
||||
use super::{render_block, render_template};
|
||||
|
||||
page_data!(AccountData, "auth/account_settings.html", {
|
||||
#[derive(Serialize)]
|
||||
pub struct AccountPageData {
|
||||
user: User,
|
||||
timezones: Vec<String>
|
||||
});
|
||||
timezones: Vec<String>,
|
||||
}
|
||||
|
||||
pub async fn show_account_page(
|
||||
State(state): State<HtmlState>,
|
||||
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
|
||||
RequireUser(user): RequireUser,
|
||||
) -> Result<impl IntoResponse, HtmlError> {
|
||||
// Early return if the user is not authenticated
|
||||
let user = match auth.current_user {
|
||||
Some(user) => user,
|
||||
None => return Ok(Redirect::to("/").into_response()),
|
||||
};
|
||||
|
||||
let timezones = TZ_VARIANTS.iter().map(|tz| tz.to_string()).collect();
|
||||
|
||||
let output = render_template(
|
||||
AccountData::template_name(),
|
||||
AccountData { user, timezones },
|
||||
state.templates.clone(),
|
||||
)?;
|
||||
|
||||
Ok(output.into_response())
|
||||
Ok(TemplateResponse::new_template(
|
||||
"auth/account_settings.html",
|
||||
AccountPageData { user, timezones },
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn set_api_key(
|
||||
State(state): State<HtmlState>,
|
||||
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
|
||||
RequireUser(user): RequireUser,
|
||||
auth: AuthSessionType,
|
||||
) -> Result<impl IntoResponse, HtmlError> {
|
||||
// Early return if the user is not authenticated
|
||||
let user = match &auth.current_user {
|
||||
Some(user) => user,
|
||||
None => return Ok(Redirect::to("/").into_response()),
|
||||
};
|
||||
|
||||
// Generate and set the API key
|
||||
let api_key = User::set_api_key(&user.id, &state.db)
|
||||
.await
|
||||
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
|
||||
let api_key = User::set_api_key(&user.id, &state.db).await?;
|
||||
|
||||
// Clear the cache so new requests have access to the user with api key
|
||||
auth.cache_clear_user(user.id.to_string());
|
||||
|
||||
// Update the user's API key
|
||||
@@ -69,40 +46,28 @@ pub async fn set_api_key(
|
||||
};
|
||||
|
||||
// Render the API key section block
|
||||
let output = render_block(
|
||||
AccountData::template_name(),
|
||||
Ok(TemplateResponse::new_partial(
|
||||
"auth/account_settings.html",
|
||||
"api_key_section",
|
||||
AccountData {
|
||||
AccountPageData {
|
||||
user: updated_user,
|
||||
timezones: vec![],
|
||||
},
|
||||
state.templates.clone(),
|
||||
)?;
|
||||
|
||||
Ok(output.into_response())
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn delete_account(
|
||||
State(state): State<HtmlState>,
|
||||
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
|
||||
RequireUser(user): RequireUser,
|
||||
auth: AuthSessionType,
|
||||
) -> Result<impl IntoResponse, HtmlError> {
|
||||
// Early return if the user is not authenticated
|
||||
let user = match &auth.current_user {
|
||||
Some(user) => user,
|
||||
None => return Ok(Redirect::to("/").into_response()),
|
||||
};
|
||||
|
||||
state
|
||||
.db
|
||||
.delete_item::<User>(&user.id)
|
||||
.await
|
||||
.map_err(|e| HtmlError::new(AppError::from(e), state.templates.clone()))?;
|
||||
state.db.delete_item::<User>(&user.id).await?;
|
||||
|
||||
auth.logout_user();
|
||||
|
||||
auth.session.destroy();
|
||||
|
||||
Ok((HxRedirect::from(Uri::from_static("/")), StatusCode::OK).into_response())
|
||||
Ok(TemplateResponse::redirect("/"))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
@@ -112,18 +77,13 @@ pub struct UpdateTimezoneForm {
|
||||
|
||||
pub async fn update_timezone(
|
||||
State(state): State<HtmlState>,
|
||||
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
|
||||
RequireUser(user): RequireUser,
|
||||
auth: AuthSessionType,
|
||||
Form(form): Form<UpdateTimezoneForm>,
|
||||
) -> Result<impl IntoResponse, HtmlError> {
|
||||
let user = match &auth.current_user {
|
||||
Some(user) => user,
|
||||
None => return Ok(Redirect::to("/").into_response()),
|
||||
};
|
||||
|
||||
User::update_timezone(&user.id, &form.timezone, &state.db)
|
||||
.await
|
||||
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
|
||||
User::update_timezone(&user.id, &form.timezone, &state.db).await?;
|
||||
|
||||
// Clear the cache
|
||||
auth.cache_clear_user(user.id.to_string());
|
||||
|
||||
// Update the user's API key
|
||||
@@ -135,15 +95,12 @@ pub async fn update_timezone(
|
||||
let timezones = TZ_VARIANTS.iter().map(|tz| tz.to_string()).collect();
|
||||
|
||||
// Render the API key section block
|
||||
let output = render_block(
|
||||
AccountData::template_name(),
|
||||
Ok(TemplateResponse::new_partial(
|
||||
"auth/account_settings.html",
|
||||
"timezone_section",
|
||||
AccountData {
|
||||
AccountPageData {
|
||||
user: updated_user,
|
||||
timezones,
|
||||
},
|
||||
state.templates.clone(),
|
||||
)?;
|
||||
|
||||
Ok(output.into_response())
|
||||
))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user