added user config
This commit is contained in:
+1
-1
@@ -18,7 +18,7 @@ mime_guess = "2.0.5"
|
||||
password-hash = "0.5.0"
|
||||
serde = { version = "1.0.219", features = ["derive"] }
|
||||
serde_json = "1.0.143"
|
||||
sqlx = { version = "0.8.6", features = ["postgres", "runtime-tokio-native-tls", "macros", "uuid", "chrono"] }
|
||||
sqlx = { version = "0.8.6", features = ["postgres", "runtime-tokio-native-tls", "macros", "uuid", "chrono", "json"] }
|
||||
tokio = { version = "1.47.1", features = ["rt-multi-thread", "macros"] }
|
||||
tower = "0.5.3"
|
||||
tower-http = { version = "0.6.6", features = ["cors", "limit", "trace", "fs"] }
|
||||
|
||||
@@ -7,6 +7,11 @@ CREATE TABLE IF NOT EXISTS user_ (
|
||||
password_hash TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS user_config_ (
|
||||
user_id INT PRIMARY KEY REFERENCES user_(id),
|
||||
config JSON DEFAULT '{}'
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS friendship_ (
|
||||
user_first INT NOT NULL REFERENCES user_(id) ON DELETE CASCADE,
|
||||
user_second INT NOT NULL REFERENCES user_(id) ON DELETE CASCADE,
|
||||
|
||||
@@ -3,6 +3,11 @@ INSERT INTO user_ (username, email, uuid, password_hash) VALUES
|
||||
('bob', 'bob@example.com', '019b1e36-3b8c-7f82-b845-6bfeb72466ce', '$argon2id$v=19$m=19456,t=2,p=1$mzO6Qx8ZH4/wrj14ZgKiuA$7bxNWCgsIVEfPgtueFbjbi8mDjbAHMYAHOGpxTJnEpQ'),
|
||||
('carol', 'carol@example.com', '019b1e36-7706-76e2-b9ce-b37916ddfc99', '$argon2id$v=19$m=19456,t=2,p=1$5rw/7uIJIKMnyqNrYQt92Q$DJVEfgbaZtkflsmDEkSoR3uDQmujI4T73cWq9hOBgVI');
|
||||
|
||||
INSERT INTO user_config_ (user_id, config) VALUES
|
||||
(1, '{"language": "French", "theme": "Catppuccin Mocha"}'),
|
||||
(2, '{"language": "English", "theme": "Nordic", "compact_layout": true}'),
|
||||
(3, '{"language": "English", "theme": "Tokyo Night", "compact_layout": false}');
|
||||
|
||||
INSERT INTO room_ (owner, name, global, uuid) VALUES
|
||||
(1, 'General Discussion', true, '5dc599ee-1f5c-40c2-a22a-e40780d2d960'),
|
||||
(2, 'Tech Talk', false, '6b14fe7b-2171-4464-95af-4888062b1b6d'),
|
||||
|
||||
@@ -97,6 +97,8 @@ pub struct Cli {
|
||||
routes::users::update_user,
|
||||
routes::users::upload_avatar,
|
||||
routes::users::get_avatar,
|
||||
routes::users::get_config,
|
||||
routes::users::save_config,
|
||||
routes::voice::voice_ws_handler,
|
||||
routes::ws::issue_ws_token,
|
||||
),
|
||||
@@ -125,6 +127,7 @@ pub struct Cli {
|
||||
routes::users::NewUserPayload,
|
||||
routes::users::UpdateUserPayoad,
|
||||
routes::users::UpdateUserResponse,
|
||||
routes::users::UserConfig,
|
||||
routes::ws::WsAuthQuery,
|
||||
)
|
||||
),
|
||||
|
||||
+79
-1
@@ -3,9 +3,10 @@ use axum::{
|
||||
extract::{Path, Request},
|
||||
http::{HeaderMap, StatusCode, header},
|
||||
middleware::Next,
|
||||
response::Response,
|
||||
response::{IntoResponse, Response},
|
||||
routing::{get, post, put},
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::PgPool;
|
||||
use std::sync::Arc;
|
||||
use uuid::Uuid;
|
||||
@@ -66,6 +67,13 @@ pub struct UpdateUserResponse {
|
||||
pub username: String,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug, utoipa::ToSchema)]
|
||||
pub struct UserConfig {
|
||||
language: Option<String>,
|
||||
theme: Option<String>,
|
||||
compact_layout: Option<bool>,
|
||||
}
|
||||
|
||||
pub fn routes() -> Router {
|
||||
Router::new()
|
||||
.route("/login", post(login))
|
||||
@@ -74,6 +82,8 @@ pub fn routes() -> Router {
|
||||
.route("/account/settings", put(update_user))
|
||||
.route("/account/upload-avatar", post(upload_avatar))
|
||||
.route("/account/get-avatar/{uuid}", get(get_avatar))
|
||||
.route("/account/get-config", get(get_config))
|
||||
.route("/account/save-config", post(save_config))
|
||||
.layer(axum::middleware::from_fn(registration_guard))
|
||||
}
|
||||
|
||||
@@ -410,3 +420,71 @@ async fn get_avatar(
|
||||
.body(axum::body::Body::from(file_contents))
|
||||
.unwrap())
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
get,
|
||||
path = "/account/get-config",
|
||||
responses(
|
||||
(status = 200, description = "User config found", body = UserConfig),
|
||||
(status = 401, description = "Missing authentication header or Invalid or expired token", body = ErrorResponse),
|
||||
(status = 404, description = "User not found", body = ErrorResponse),
|
||||
),
|
||||
security( ("bearer_auth" = [])
|
||||
)
|
||||
)]
|
||||
async fn get_config(
|
||||
headers: HeaderMap,
|
||||
Extension(db): Extension<PgPool>,
|
||||
) -> Result<Response, APIError> {
|
||||
let claims = verify_jwt(headers)?;
|
||||
let user_id = user_id_from_uuid(&db, claims.sub).await?;
|
||||
|
||||
let config: sqlx::types::Json<UserConfig> =
|
||||
sqlx::query_scalar("SELECT config FROM user_config_ WHERE user_id = $1")
|
||||
.bind(user_id)
|
||||
.fetch_one(&db)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
tracing::debug!("Failed to get user config: {e}");
|
||||
APIError::UserNotFound
|
||||
})?;
|
||||
|
||||
let config = config.0;
|
||||
|
||||
Ok(Json(config).into_response())
|
||||
}
|
||||
|
||||
#[utoipa::path(
|
||||
post,
|
||||
path = "/account/save-config",
|
||||
request_body(content = UserConfig, content_type = "application/json", description = "User config in json format"),
|
||||
responses(
|
||||
(status = 200, description = "User config saved", body = UserConfig),
|
||||
(status = 401, description = "Missing authentication header or Invalid or expired token", body = ErrorResponse),
|
||||
(status = 404, description = "User not found", body = ErrorResponse),
|
||||
),
|
||||
security(
|
||||
("bearer_auth" = [])
|
||||
)
|
||||
)]
|
||||
async fn save_config(
|
||||
headers: HeaderMap,
|
||||
Extension(db): Extension<PgPool>,
|
||||
Json(payload): Json<UserConfig>,
|
||||
) -> Result<Response, APIError> {
|
||||
let claims = verify_jwt(headers)?;
|
||||
|
||||
let user_id = user_id_from_uuid(&db, claims.sub).await?;
|
||||
|
||||
sqlx::query("UPDATE user_config_ SET config = $1 WHERE user_id = $2")
|
||||
.bind(sqlx::types::Json(&payload))
|
||||
.bind(user_id)
|
||||
.execute(&db)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
tracing::debug!("Failed to save user config: {e}");
|
||||
APIError::UserNotFound
|
||||
})?;
|
||||
|
||||
Ok((StatusCode::OK, Json(payload)).into_response())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user