diff --git a/src/api_calls.rs b/src/api_calls.rs index e6b5a14..377b582 100644 --- a/src/api_calls.rs +++ b/src/api_calls.rs @@ -1480,6 +1480,66 @@ pub async fn change_display_name( } } +pub async fn change_user_name( + token: String, + request: UserNameChangeRequest, +) -> Result { + let token = match verify_token(token) { + Ok(val) => val, + Err(_) => { + let r = "Wrong token"; + return Ok(warp::reply::with_status( + warp::reply::json(&r), + warp::http::StatusCode::UNAUTHORIZED, + )); + } + }; + + let connection = tokio_rusqlite::Connection::open("projekt-db") + .await + .unwrap(); + let id = token.claims.uid; + + if is_limited(&connection, token.claims.uid).await && token.claims.is_admin == 0 { + let r = "Ur too fast"; + return Ok(warp::reply::with_status( + warp::reply::json(&r), + warp::http::StatusCode::FORBIDDEN, + )); + } + + if check_user_id(&connection, id).await { + let change_query = "UPDATE users SET user_name = ? WHERE user_id = ?"; + connection + .call(move |conn| { + let mut statement = conn.prepare(change_query).unwrap(); + statement + .execute(params![request.new_display_name, id]) + .unwrap(); + Ok(0) + }) + .await + .unwrap(); + + info!( + "User name changed for user with id: {}", + token.claims.uid + ); + add_upload_db(&connection, token.claims.uid, 1).await; + let r = "User name change successful"; + Ok(warp::reply::with_status( + warp::reply::json(&r), + warp::http::StatusCode::OK, + )) + } else { + let r = "User not found"; + Ok(warp::reply::with_status( + warp::reply::json(&r), + warp::http::StatusCode::NOT_FOUND, + )) + } +} + pub async fn change_description( token: String, request: DescriptionChangeRequest, @@ -1873,6 +1933,11 @@ pub fn description_change_json( warp::body::content_length_limit(1024 * 16).and(warp::body::json()) } +pub fn user_name_change_json( +) -> impl Filter + Clone { + warp::body::content_length_limit(1024 * 16).and(warp::body::json()) +} + pub fn image_to_post_add_json( ) -> impl Filter + Clone { warp::body::content_length_limit(1024 * 16).and(warp::body::json()) diff --git a/src/main.rs b/src/main.rs index b9919c0..700e489 100644 --- a/src/main.rs +++ b/src/main.rs @@ -155,6 +155,12 @@ pub fn routes() -> impl Filter("token")) .and(display_name_change_json()) .and_then(change_display_name); + + let change_user_name = warp::post() + .and(warp::path!("api" / "post" / "change" / "user-name")) + .and(warp::cookie::("token")) + .and(user_name_change_json()) + .and_then(change_user_name); let change_description = warp::post() .and(warp::path!("api" / "post" / "change" / "description")) @@ -203,6 +209,7 @@ pub fn routes() -> impl Filter