diff --git a/README.md b/README.md index 3b1c751..77dc22b 100644 --- a/README.md +++ b/README.md @@ -26,4 +26,4 @@ Unban User: "/unban/:victim/:judge" Is Follower: "/is-follower/:follower/:follower" -Is Followed: "/is-follower/:follower/:follower" \ No newline at end of file +Is Banned: "/is-banned/:victim/:judge" \ No newline at end of file diff --git a/src/db/db_operations.rs b/src/db/db_operations.rs index 5b5314f..f5f05a1 100644 --- a/src/db/db_operations.rs +++ b/src/db/db_operations.rs @@ -105,6 +105,6 @@ pub async fn is_follower(follower: &String, followed: &String, db: &Surreal) -> bool { - is_followed_by_username(follower, followed, db).await +pub async fn is_banned(victim: &String, judge: &String, db: &Surreal) -> bool { + is_banned_by_username(victim, judge, db).await } diff --git a/src/db/db_utils.rs b/src/db/db_utils.rs index 2403459..bf3acf3 100644 --- a/src/db/db_utils.rs +++ b/src/db/db_utils.rs @@ -505,30 +505,26 @@ pub async fn is_follower_by_username( } } -pub async fn is_followed_by_username( - follower: &String, - followed: &String, - db: &Surreal, -) -> bool { - match search_channel_by_username(follower, db).await { - Some(mut follower_channel) => match search_channel_by_username(followed, db).await { - Some(followed_channel) => { - follower_channel.followed_list.sort(); - match follower_channel - .followed_list - .binary_search(&followed_channel.id.unwrap().id) +pub async fn is_banned_by_username(victim: &String, judge: &String, db: &Surreal) -> bool { + match search_channel_by_username(victim, db).await { + Some(victim_channel) => match search_channel_by_username(judge, db).await { + Some(mut judge_channel) => { + judge_channel.banned_list.sort(); + match judge_channel + .banned_list + .binary_search(&victim_channel.id.unwrap().id) { Ok(_) => true, Err(_) => false, } } None => { - eprintln!("Error: Can't Check Is Follower | Followed Not Exists"); + eprintln!("Error: Can't Check Is Banned | Judge Not Exists"); false } }, None => { - eprintln!("Error: Can't Check Is Follower | Follower Not Exists"); + eprintln!("Error: Can't Check Is Banned | Victim Not Exists"); false } } diff --git a/src/routing.rs b/src/routing.rs index 59e1d19..2779119 100644 --- a/src/routing.rs +++ b/src/routing.rs @@ -25,7 +25,7 @@ pub async fn routing(State(state): State) -> Router { .route("/ban/:victim/:judge", get(ban)) .route("/unban/:victim/:judge", get(unban)) .route("/is-follower/:follower/:followed", get(is_follower)) - .route("/is-followed/:follower/:followed", get(is_followed)) + .route("/is-banned/:victim/:judge", get(is_banned)) .layer(CorsLayer::permissive()) .with_state(state.clone()) } @@ -181,23 +181,23 @@ async fn is_follower( } } -async fn is_followed( - Path((follower, followed)): Path<(String, String)>, +async fn is_banned( + Path((victim, judge)): Path<(String, String)>, State(state): State, ) -> impl IntoResponse { - let is_followed: bool = db_operations::is_followed(&follower, &followed, &state.db).await; - match is_followed { + let is_banned = db_operations::is_banned(&victim, &judge, &state.db).await; + match is_banned { true => { - let is_followed = serde_json::json!({ - "is_followed":true + let is_banned = serde_json::json!({ + "is_banned":true }); - (StatusCode::OK, Json(is_followed)) + (StatusCode::OK, Json(is_banned)) } false => { - let is_followed = serde_json::json!({ - "is_followed":false + let is_banned = serde_json::json!({ + "is_banned":false }); - (StatusCode::OK, Json(is_followed)) + (StatusCode::OK, Json(is_banned)) } } } diff --git a/src/tests/db_tests.rs b/src/tests/db_tests.rs index c56a02c..775faa2 100644 --- a/src/tests/db_tests.rs +++ b/src/tests/db_tests.rs @@ -1,4 +1,3 @@ - #[cfg(test)] use crate::db::db_operations::*; use tokio::test; @@ -485,39 +484,31 @@ async fn test_is_follower_nonfollower() { } #[test] -async fn test_is_followed_already_followed() { - let connection = create_connection_for_tests("test_is_followed_already_followed").await; - let name_follower = &"Ahmet".to_string(); - let name_followed = &"Kaan".to_string(); +async fn test_is_banned_already_banned() { + let connection = create_connection_for_tests("test_is_banned_already_banned").await; + let name_victim = &"Ahmet".to_string(); + let name_judge = &"Kaan".to_string(); - let _follower = create(name_follower, &connection).await.unwrap(); - let _followed = create(name_followed, &connection).await.unwrap(); + let _victim = create(name_victim, &connection).await.unwrap(); + let _judge = create(name_judge, &connection).await.unwrap(); - let _follower = follow(name_follower, name_followed, &connection) - .await - .unwrap(); + let _victim = ban(name_victim, name_judge, &connection).await.unwrap(); - assert_eq!( - is_followed(name_follower, name_followed, &connection).await, - true - ); + assert_eq!(is_banned(name_victim, name_judge, &connection).await, true); let _cleaning = connection.query("DELETE channel;").await; } #[test] -async fn test_is_followed_nonfollowed() { - let connection = create_connection_for_tests("test_is_follower_nonfollowed").await; - let name_follower = &"Ahmet".to_string(); - let name_followed = &"Kaan".to_string(); +async fn test_is_banned_nonbanned() { + let connection = create_connection_for_tests("test_is_banned_nonbanned").await; + let name_victim = &"Ahmet".to_string(); + let name_judge = &"Kaan".to_string(); - let _follower = create(name_follower, &connection).await.unwrap(); - let _followed = create(name_followed, &connection).await.unwrap(); + let _victim = create(name_victim, &connection).await.unwrap(); + let _judge = create(name_judge, &connection).await.unwrap(); - assert_eq!( - is_followed(name_follower, name_followed, &connection).await, - false - ); + assert_eq!(is_banned(name_victim, name_judge, &connection).await, false); let _cleaning = connection.query("DELETE channel;").await; }