mirror of
https://github.com/perstarkse/minne.git
synced 2026-03-20 08:34:31 +01:00
feat: timezone support and setting
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use axum::{
|
||||
extract::DefaultBodyLimit,
|
||||
middleware::from_fn_with_state,
|
||||
routing::{delete, get, post},
|
||||
routing::{delete, get, patch, post},
|
||||
Router,
|
||||
};
|
||||
use axum_session::{SessionConfig, SessionLayer, SessionStore};
|
||||
@@ -27,7 +27,7 @@ use zettle_db::{
|
||||
queue_length::queue_length_handler,
|
||||
},
|
||||
html::{
|
||||
account::{delete_account, set_api_key, show_account_page},
|
||||
account::{delete_account, set_api_key, show_account_page, update_timezone},
|
||||
admin_panel::show_admin_panel,
|
||||
documentation::index::show_documentation_index,
|
||||
gdpr::{accept_gdpr, deny_gdpr},
|
||||
@@ -173,6 +173,7 @@ fn html_routes(
|
||||
.route("/account", get(show_account_page))
|
||||
.route("/admin", get(show_admin_panel))
|
||||
.route("/set-api-key", post(set_api_key))
|
||||
.route("/update-timezone", patch(update_timezone))
|
||||
.route("/delete-account", delete(delete_account))
|
||||
.route(
|
||||
"/signup",
|
||||
|
||||
@@ -2,10 +2,12 @@ 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 chrono_tz::TZ_VARIANTS;
|
||||
use surrealdb::{engine::any::Any, Surreal};
|
||||
|
||||
use crate::{
|
||||
@@ -18,7 +20,8 @@ use crate::{
|
||||
use super::render_block;
|
||||
|
||||
page_data!(AccountData, "auth/account_settings.html", {
|
||||
user: User
|
||||
user: User,
|
||||
timezones: Vec<String>
|
||||
});
|
||||
|
||||
pub async fn show_account_page(
|
||||
@@ -31,9 +34,11 @@ pub async fn show_account_page(
|
||||
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 },
|
||||
AccountData { user, timezones },
|
||||
state.templates.clone(),
|
||||
)?;
|
||||
|
||||
@@ -67,7 +72,10 @@ pub async fn set_api_key(
|
||||
let output = render_block(
|
||||
AccountData::template_name(),
|
||||
"api_key_section",
|
||||
AccountData { user: updated_user },
|
||||
AccountData {
|
||||
user: updated_user,
|
||||
timezones: vec![],
|
||||
},
|
||||
state.templates.clone(),
|
||||
)?;
|
||||
|
||||
@@ -94,3 +102,46 @@ pub async fn delete_account(
|
||||
|
||||
Ok((HxRedirect::from(Uri::from_static("/")), StatusCode::OK).into_response())
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct UpdateTimezoneForm {
|
||||
timezone: String,
|
||||
}
|
||||
|
||||
pub async fn update_timezone(
|
||||
State(state): State<AppState>,
|
||||
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
|
||||
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.surreal_db_client)
|
||||
.await
|
||||
.map_err(|e| HtmlError::new(e, state.templates.clone()))?;
|
||||
|
||||
auth.cache_clear_user(user.id.to_string());
|
||||
|
||||
// Update the user's API key
|
||||
let updated_user = User {
|
||||
timezone: form.timezone,
|
||||
..user.clone()
|
||||
};
|
||||
|
||||
let timezones = TZ_VARIANTS.iter().map(|tz| tz.to_string()).collect();
|
||||
|
||||
// Render the API key section block
|
||||
let output = render_block(
|
||||
AccountData::template_name(),
|
||||
"timezone_section",
|
||||
AccountData {
|
||||
user: updated_user,
|
||||
timezones,
|
||||
},
|
||||
state.templates.clone(),
|
||||
)?;
|
||||
|
||||
Ok(output.into_response())
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ use super::{render_block, render_template};
|
||||
pub struct SignupParams {
|
||||
pub email: String,
|
||||
pub password: String,
|
||||
pub timezone: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
@@ -55,7 +56,14 @@ pub async fn process_signup_and_show_verification(
|
||||
auth: AuthSession<User, String, SessionSurrealPool<Any>, Surreal<Any>>,
|
||||
Form(form): Form<SignupParams>,
|
||||
) -> Result<impl IntoResponse, HtmlError> {
|
||||
let user = match User::create_new(form.email, form.password, &state.surreal_db_client).await {
|
||||
let user = match User::create_new(
|
||||
form.email,
|
||||
form.password,
|
||||
&state.surreal_db_client,
|
||||
form.timezone,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(user) => user,
|
||||
Err(e) => {
|
||||
tracing::error!("{:?}", e);
|
||||
|
||||
@@ -16,7 +16,9 @@ stored_object!(User, "user", {
|
||||
password: String,
|
||||
anonymous: bool,
|
||||
api_key: Option<String>,
|
||||
admin: bool
|
||||
admin: bool,
|
||||
#[serde(default)]
|
||||
timezone: String
|
||||
});
|
||||
|
||||
#[async_trait]
|
||||
@@ -44,6 +46,7 @@ impl User {
|
||||
email: String,
|
||||
password: String,
|
||||
db: &SurrealDbClient,
|
||||
timezone: String,
|
||||
) -> Result<Self, AppError> {
|
||||
// verify that the application allows new creations
|
||||
let systemsettings = SystemSettings::get_current(db).await?;
|
||||
@@ -64,7 +67,8 @@ impl User {
|
||||
admin = $count < 1, // Changed from == 0 to < 1
|
||||
anonymous = false,
|
||||
created_at = $created_at,
|
||||
updated_at = $updated_at",
|
||||
updated_at = $updated_at,
|
||||
timezone = $timezone",
|
||||
)
|
||||
.bind(("table", "user"))
|
||||
.bind(("id", id))
|
||||
@@ -72,6 +76,7 @@ impl User {
|
||||
.bind(("password", password))
|
||||
.bind(("created_at", now))
|
||||
.bind(("updated_at", now))
|
||||
.bind(("timezone", timezone))
|
||||
.await?
|
||||
.take(1)?;
|
||||
|
||||
@@ -214,4 +219,16 @@ impl User {
|
||||
|
||||
Ok(items)
|
||||
}
|
||||
pub async fn update_timezone(
|
||||
user_id: &str,
|
||||
timezone: &str,
|
||||
db: &Surreal<Any>,
|
||||
) -> Result<(), AppError> {
|
||||
db.query("UPDATE type::thing('user', $user_id) SET timezone = $timezone")
|
||||
.bind(("table_name", User::table_name()))
|
||||
.bind(("user_id", user_id.to_string()))
|
||||
.bind(("timezone", timezone.to_string()))
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user