Skip to content

Commit

Permalink
Merge pull request #45 from kochamaltki/tomek
Browse files Browse the repository at this point in the history
zmiany wyswietlanej nazwy
  • Loading branch information
malinowy5 authored Apr 2, 2024
2 parents d81eb6b + 5449aa1 commit 3cc7755
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 17 deletions.
11 changes: 11 additions & 0 deletions change-displayname.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

path="$1/api/post/change/display-name"

curl --location --request POST "$path" \
--header 'Content-Type: application/json' \
--header 'Content-Type: text/plain' \
--data-raw '{
"new_display_name" : "'"$2"'",
"token": "'"$3"'"
}'
46 changes: 46 additions & 0 deletions src/api_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,48 @@ pub async fn ban_user(request: UserBanRequest) -> Result<impl warp::Reply, warp:
}
}


pub async fn change_display_name(request: DisplayNameChangeRequest) -> Result<impl warp::Reply, warp::Rejection> {
let token: TokenData<Claims>;
match verify_token::verify_token(request.token) {
Ok(val) => {token = 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 check_user_id(&connection, id).await {
let change_query = "UPDATE users SET display_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!("Display name changed for user with id: {}", token.claims.uid);
let r = "Display 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 fn post_json() -> impl Filter<Extract = (PostCreateRequest,), Error = warp::Rejection> + Clone {
warp::body::content_length_limit(1024 * 16).and(warp::body::json())
}
Expand Down Expand Up @@ -640,3 +682,7 @@ pub fn ban_json() -> impl Filter<Extract = (UserBanRequest,), Error = warp::Reje
pub fn react_json() -> impl Filter<Extract = (ReactRequest,), Error = warp::Rejection> + Clone {
warp::body::content_length_limit(1024 * 16).and(warp::body::json())
}

pub fn display_name_change_json() -> impl Filter<Extract = (DisplayNameChangeRequest,), Error = warp::Rejection> + Clone {
warp::body::content_length_limit(1024 * 16).and(warp::body::json())
}
7 changes: 0 additions & 7 deletions src/database_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,3 @@ pub async fn add_reaction_db(connection: &Connection, user_id: i64, post_id: i64
false
}

// pub async fn search_users(connection: &Connection, user_id: i64, post_id: i64, reaction_type: i64) -> bool {

// }

// pub async fn search_posts(connection: &Connection, user_id: i64, post_id: i64, reaction_type: i64) -> bool {

// }
25 changes: 15 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ pub fn routes() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejecti
.and(warp::path::end())
.and_then(get_reactions_from_post);

let get_profile_picture = warp::path("api")
.and(warp::path("get"))
.and(warp::path("profile-picture"))
.and(warp::fs::dir("./media/profile_pictures"));

// let get_posts_from_search = warp::get()
// .and(warp::path("api"))
// .and(warp::path("get"))
Expand Down Expand Up @@ -165,15 +170,15 @@ pub fn routes() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejecti
.and(ban_json())
.and_then(ban_user);

let get_profile_picture = warp::path("api")
.and(warp::path("get"))
.and(warp::path("profile-picture"))
.and(warp::fs::dir("./media/profile_pictures"));

let get_background = warp::path("api")
.and(warp::path("get"))
.and(warp::path("background"))
.and(warp::fs::dir("./media/background"));
let change_display_name = warp::post()
.and(warp::path("api"))
.and(warp::path("post"))
.and(warp::path("change"))
.and(warp::path("display-name"))
.and(warp::path::end())
.and(display_name_change_json())
.and_then(change_display_name);



get_posts_by_user
Expand All @@ -193,7 +198,7 @@ pub fn routes() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejecti
.or(get_reactions_from_post)
.or(get_profile_by_id)
.or(get_profile_picture)
.or(get_background)
.or(change_display_name)
// .or(get_posts_from_search)
// .or(get_users_from_search)
}
Expand Down
6 changes: 6 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,9 @@ pub struct UserBanRequest {
pub user_id: i64,
pub token: String
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct DisplayNameChangeRequest {
pub new_display_name: String,
pub token: String
}
2 changes: 2 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ echo
echo
./react-curl.sh $ip 1 1 $tok_1
echo
./change-displayname.sh $ip nowanazwa $tok_2
echo

0 comments on commit 3cc7755

Please sign in to comment.