Skip to content

Commit

Permalink
change user name
Browse files Browse the repository at this point in the history
  • Loading branch information
Dromader2137 committed May 23, 2024
1 parent f555b09 commit 52fb1ef
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/api_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1480,6 +1480,66 @@ pub async fn change_display_name(
}
}

pub async fn change_user_name(
token: String,
request: UserNameChangeRequest,
) -> Result<impl warp::Reply, warp::Rejection> {
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,
Expand Down Expand Up @@ -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<Extract = (UserNameChangeRequest,), Error = warp::Rejection> + Clone {
warp::body::content_length_limit(1024 * 16).and(warp::body::json())
}

pub fn image_to_post_add_json(
) -> impl Filter<Extract = (AddImageToPostRequest,), Error = warp::Rejection> + Clone {
warp::body::content_length_limit(1024 * 16).and(warp::body::json())
Expand Down
7 changes: 7 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,12 @@ pub fn routes() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejecti
.and(warp::cookie::<String>("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::<String>("token"))
.and(user_name_change_json())
.and_then(change_user_name);

let change_description = warp::post()
.and(warp::path!("api" / "post" / "change" / "description"))
Expand Down Expand Up @@ -203,6 +209,7 @@ pub fn routes() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejecti
.or(get_images_from_post)
.or(get_like_from_post_by_user)
.or(change_display_name)
.or(change_user_name)
.or(change_description)
.or(upload_image)
.or(get_image)
Expand Down
5 changes: 5 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ pub struct DisplayNameChangeRequest {
pub new_display_name: String,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct UserNameChangeRequest {
pub new_display_name: String,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct DescriptionChangeRequest {
pub new_description: String,
Expand Down

0 comments on commit 52fb1ef

Please sign in to comment.