Skip to content

Commit

Permalink
pfp remove
Browse files Browse the repository at this point in the history
  • Loading branch information
Dromader2137 committed Jun 1, 2024
1 parent 03a9819 commit a837e00
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 16 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,22 @@ AddImageToPostRequest {
- Effect: Image is added to post
- Return: 200 ("Image added to post") / 400 ("Image already added to this post") / 401 ("Wrong token" / "User not authorized") / 404 ("Image not found" / "Post not found")
- Headers: 'Content-Type: application/json' 'Content-Type: text/plain'
#### /api/post/set-pfp
- Post:
```
SetPFPRequest {
image_id: i64,
user_id: i64
}
```
- With cookies
- Effect: User's PFP is set to the image
#### /api/post/remove-pfp
- Post:
```
RemovePFPRequest {
user_id: i64
}
```
- With cookies
- Effect: User's PFP is deleted
11 changes: 11 additions & 0 deletions scripts/remove-pfp.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

path="$1/api/post/remove-pfp"

curl --location --request POST "$path" \
--cookie "token=$3" \
--header 'Content-Type: application/json' \
--header 'Content-Type: text/plain' \
--data-raw '{
"user_id": '$2'
}'
12 changes: 12 additions & 0 deletions scripts/test-pfp.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

ip=$1

tok_0=$(./scripts/login.sh $ip admin admin false)
tok_1=$(./scripts/signup.sh $ip dr 1234 false)
echo $tok_0
echo $tok_1
./scripts/create-post.sh $ip hello welcome yo $tok_1
./scripts/upload-image.sh $ip "media/profile_pictures/default.png" $tok_0
./scripts/set-pfp.sh $ip 0 0 $tok_0
./scripts/remove-pfp.sh $ip 0 $tok_0
68 changes: 52 additions & 16 deletions src/api_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,7 @@ pub async fn delete_user(
}
}

pub async fn delete_post(token: String, request: PostDeleteRequest,) -> Result<impl warp::Reply, warp::Rejection> {
pub async fn delete_post(token: String, _request: PostDeleteRequest,) -> Result<impl warp::Reply, warp::Rejection> {
info!("{}", token);
let token = match verify_token(token) {
Ok(val) => val,
Expand All @@ -1267,23 +1267,9 @@ pub async fn delete_post(token: String, request: PostDeleteRequest,) -> Result<i
.unwrap();
let id = token.claims.uid;
if check_user_id(&connection, id).await || token.claims.is_admin == 1 {
let delete_query = "
DELETE FROM posts WHERE post_id = ?;
DELETE FROM posts_tags WHERE post_id = ?;
DELETE FROM posts_images WHERE post_id = ?"; // nie wszystko jest usuwane, naprawic
connection
.call(move |conn| {
let mut statement = conn.prepare(delete_query).unwrap();
statement.execute(params![request.post_id]).unwrap();
Ok(0)
})
.await
.unwrap();

info!("Post {} deleted", id);
purge_data(&connection, id).await;
let r = "Post deleted";
let res = warp::reply::with_status(r, warp::http::StatusCode::OK);
let res = warp::reply::with_header(res, "Access-Control-Allow-Origin", "*");
Ok(res)
} else {
Err(warp::reject::custom(UserNotFound))
Expand Down Expand Up @@ -1802,6 +1788,51 @@ pub async fn set_pfp(
}
}

pub async fn remove_pfp(
token: String,
_request: RemovePFPRequest,
) -> Result<impl warp::Reply, warp::Rejection> {
let connection = tokio_rusqlite::Connection::open("projekt-db")
.await
.unwrap();

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,
));
}
};

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, token.claims.uid).await {
let r = "User not found";
return Ok(warp::reply::with_status(
warp::reply::json(&r),
warp::http::StatusCode::NOT_FOUND,
));
}

add_upload_db(&connection, token.claims.uid, 1).await;
remove_image_from_user(&connection, token.claims.uid).await;

let r = "PFP deleted";
Ok(warp::reply::with_status(
warp::reply::json(&r),
warp::http::StatusCode::OK,
))
}

pub async fn add_image_to_post(
token: String,
request: AddImageToPostRequest,
Expand Down Expand Up @@ -1994,3 +2025,8 @@ pub fn set_pfp_json(
) -> impl Filter<Extract = (SetPFPRequest,), Error = warp::Rejection> + Clone {
warp::body::content_length_limit(1024 * 16).and(warp::body::json())
}

pub fn remove_pfp_json(
) -> impl Filter<Extract = (RemovePFPRequest,), Error = warp::Rejection> + Clone {
warp::body::content_length_limit(1024 * 16).and(warp::body::json())
}
17 changes: 17 additions & 0 deletions src/database_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,13 @@ pub async fn purge_data(connection: &Connection, user_id: i64) {
statement.execute(params![user_id]).unwrap();
Ok(0)
}).await.unwrap();

let user_delete_query = "DELETE FROM users WHERE user_id = ?";
connection.call(move |conn| {
let mut statement = conn.prepare(user_delete_query).unwrap();
statement.execute(params![user_id]).unwrap();
Ok(0)
}).await.unwrap();
}

pub async fn get_next_post_id(connection: &Connection) -> Result<i64, &str> {
Expand Down Expand Up @@ -505,6 +512,16 @@ pub async fn assign_image_to_user(connection: &Connection, user_id: i64, image_i
Ok(())
}

pub async fn remove_image_from_user(connection: &Connection, user_id: i64) {
let image_query = "UPDATE users SET pfp_id='' WHERE user_id=?";

connection.call(move |conn| {
let mut statement = conn.prepare(image_query).unwrap();
statement.execute(params![user_id]).unwrap();
Ok(0)
}).await.unwrap();
}

pub async fn add_upload_db(connection: &Connection, user_id: i64, weight: i16) {
let time_since_epoch: i64 = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs() as i64;
let add_query = "INSERT INTO uploads VALUES (?, ?, ?)";
Expand Down
7 changes: 7 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ pub fn routes() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejecti
.and(warp::cookie::<String>("token"))
.and(set_pfp_json())
.and_then(set_pfp);

let remove_pfp = warp::post()
.and(warp::path!("api" / "post" / "remove-pfp"))
.and(warp::cookie::<String>("token"))
.and(remove_pfp_json())
.and_then(remove_pfp);

get_posts_by_user
.or(post)
Expand Down Expand Up @@ -218,6 +224,7 @@ pub fn routes() -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejecti
.or(get_comments_from_post)
.or(get_posts_from_search)
.or(get_users_from_search)
.or(remove_pfp)
}

#[tokio::main]
Expand Down
5 changes: 5 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,8 @@ pub struct SetPFPRequest {
pub image_id: i64,
pub user_id: i64
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct RemovePFPRequest {
pub user_id: i64
}

0 comments on commit a837e00

Please sign in to comment.