multi mode

This commit is contained in:
Mateusz Gruszczyński
2026-07-31 10:10:19 +02:00
parent e39a1154e6
commit f7afee886b
25 changed files with 910 additions and 212 deletions
+31
View File
@@ -49,6 +49,7 @@ pub struct User {
pub password_hash: String,
pub confirmed_at: Option<String>,
pub is_active: i64,
pub theme: String,
}
#[derive(Deserialize)]
@@ -95,6 +96,8 @@ pub struct ProfileUpdateRequest {
password: String,
#[serde(default)]
editor_color: Option<String>,
#[serde(default)]
theme: Option<String>,
}
#[derive(Deserialize)]
pub struct DeleteAccountRequest {
@@ -190,6 +193,7 @@ impl<'r> sqlx::FromRow<'r, AnyRow> for User {
password_hash: crate::row_decode::text(row, "password_hash")?,
confirmed_at: crate::row_decode::optional_text(row, "confirmed_at")?,
is_active: row.try_get("is_active")?,
theme: crate::row_decode::text(row, "theme")?,
})
}
}
@@ -268,6 +272,7 @@ pub struct SessionResponse {
directory_organization: Option<String>,
suggested_nickname: Option<String>,
editor_color: Option<String>,
theme: String,
}
#[derive(Serialize)]
pub struct IdentityResponse {
@@ -282,6 +287,7 @@ pub struct RegisterResponse {
email: String,
expires_at: Option<String>,
confirmation_required: bool,
theme: String,
message: String,
}
@@ -433,6 +439,7 @@ pub async fn register(
email: user.email,
expires_at: None,
confirmation_required: true,
theme: user.theme,
message:
"Account created. Check your e-mail and confirm the account before logging in."
.into(),
@@ -451,6 +458,7 @@ pub async fn register(
email: session.email,
expires_at: Some(session.expires_at),
confirmation_required: false,
theme: session.theme,
message: "Account created.".into(),
}),
).into_response();
@@ -740,6 +748,7 @@ pub async fn me(
directory_organization,
suggested_nickname,
editor_color,
theme: user.theme,
},
state.user_session_ttl_days,
))
@@ -855,10 +864,23 @@ pub async fn update_profile(
.map_err(AuthError::database)?;
}
let mut theme = user.theme.clone();
if let Some(value) = req.theme.as_deref() {
theme = validate_theme(value)?.to_owned();
sqlx::query(queries::get(state.db.kind(), queries::AUTH_UPDATE_THEME))
.bind(&theme)
.bind(Utc::now().to_rfc3339())
.bind(user.id)
.execute(state.db.pool())
.await
.map_err(AuthError::database)?;
}
Ok(Json(serde_json::json!({
"ok": true,
"nickname": nickname,
"editor_color": req.editor_color.as_deref(),
"theme": theme,
"email_pending": email_pending,
"message": if email_pending {
"Profile updated. Confirm the new e-mail address using the link sent to it."
@@ -2052,6 +2074,7 @@ async fn create_session(state: &SharedState, user: &User) -> Result<SessionRespo
directory_organization,
suggested_nickname,
editor_color,
theme: user.theme.clone(),
})
}
async fn find_user_by_nickname(
@@ -2104,6 +2127,14 @@ async fn find_user_by_email(state: &SharedState, email: &str) -> Result<Option<U
.await
.map_err(AuthError::database)
}
fn validate_theme(value: &str) -> Result<&str, AuthError> {
match value.trim() {
"dark" => Ok("dark"),
"light" => Ok("light"),
_ => Err(AuthError::bad_request("Unknown interface theme.")),
}
}
fn validate_editor_color(value: &str) -> Result<String, AuthError> {
let value = value.trim();
if value.len() == 7