feat: is_banned

docs: 📝 is_banned
revert: 🔥 remove is_followed
This commit is contained in:
Ahmet Kaan GÜMÜŞ 2024-04-26 00:01:32 +03:00
parent f49de4ce50
commit 8c04db05b8
5 changed files with 39 additions and 52 deletions

View file

@ -26,4 +26,4 @@ Unban User: "/unban/:victim/:judge"
Is Follower: "/is-follower/:follower/:follower" Is Follower: "/is-follower/:follower/:follower"
Is Followed: "/is-follower/:follower/:follower" Is Banned: "/is-banned/:victim/:judge"

View file

@ -105,6 +105,6 @@ pub async fn is_follower(follower: &String, followed: &String, db: &Surreal<Clie
is_follower_by_username(follower, followed, db).await is_follower_by_username(follower, followed, db).await
} }
pub async fn is_followed(follower: &String, followed: &String, db: &Surreal<Client>) -> bool { pub async fn is_banned(victim: &String, judge: &String, db: &Surreal<Client>) -> bool {
is_followed_by_username(follower, followed, db).await is_banned_by_username(victim, judge, db).await
} }

View file

@ -505,30 +505,26 @@ pub async fn is_follower_by_username(
} }
} }
pub async fn is_followed_by_username( pub async fn is_banned_by_username(victim: &String, judge: &String, db: &Surreal<Client>) -> bool {
follower: &String, match search_channel_by_username(victim, db).await {
followed: &String, Some(victim_channel) => match search_channel_by_username(judge, db).await {
db: &Surreal<Client>, Some(mut judge_channel) => {
) -> bool { judge_channel.banned_list.sort();
match search_channel_by_username(follower, db).await { match judge_channel
Some(mut follower_channel) => match search_channel_by_username(followed, db).await { .banned_list
Some(followed_channel) => { .binary_search(&victim_channel.id.unwrap().id)
follower_channel.followed_list.sort();
match follower_channel
.followed_list
.binary_search(&followed_channel.id.unwrap().id)
{ {
Ok(_) => true, Ok(_) => true,
Err(_) => false, Err(_) => false,
} }
} }
None => { None => {
eprintln!("Error: Can't Check Is Follower | Followed Not Exists"); eprintln!("Error: Can't Check Is Banned | Judge Not Exists");
false false
} }
}, },
None => { None => {
eprintln!("Error: Can't Check Is Follower | Follower Not Exists"); eprintln!("Error: Can't Check Is Banned | Victim Not Exists");
false false
} }
} }

View file

@ -25,7 +25,7 @@ pub async fn routing(State(state): State<AppState>) -> Router {
.route("/ban/:victim/:judge", get(ban)) .route("/ban/:victim/:judge", get(ban))
.route("/unban/:victim/:judge", get(unban)) .route("/unban/:victim/:judge", get(unban))
.route("/is-follower/:follower/:followed", get(is_follower)) .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()) .layer(CorsLayer::permissive())
.with_state(state.clone()) .with_state(state.clone())
} }
@ -181,23 +181,23 @@ async fn is_follower(
} }
} }
async fn is_followed( async fn is_banned(
Path((follower, followed)): Path<(String, String)>, Path((victim, judge)): Path<(String, String)>,
State(state): State<AppState>, State(state): State<AppState>,
) -> impl IntoResponse { ) -> impl IntoResponse {
let is_followed: bool = db_operations::is_followed(&follower, &followed, &state.db).await; let is_banned = db_operations::is_banned(&victim, &judge, &state.db).await;
match is_followed { match is_banned {
true => { true => {
let is_followed = serde_json::json!({ let is_banned = serde_json::json!({
"is_followed":true "is_banned":true
}); });
(StatusCode::OK, Json(is_followed)) (StatusCode::OK, Json(is_banned))
} }
false => { false => {
let is_followed = serde_json::json!({ let is_banned = serde_json::json!({
"is_followed":false "is_banned":false
}); });
(StatusCode::OK, Json(is_followed)) (StatusCode::OK, Json(is_banned))
} }
} }
} }

View file

@ -1,4 +1,3 @@
#[cfg(test)] #[cfg(test)]
use crate::db::db_operations::*; use crate::db::db_operations::*;
use tokio::test; use tokio::test;
@ -485,39 +484,31 @@ async fn test_is_follower_nonfollower() {
} }
#[test] #[test]
async fn test_is_followed_already_followed() { async fn test_is_banned_already_banned() {
let connection = create_connection_for_tests("test_is_followed_already_followed").await; let connection = create_connection_for_tests("test_is_banned_already_banned").await;
let name_follower = &"Ahmet".to_string(); let name_victim = &"Ahmet".to_string();
let name_followed = &"Kaan".to_string(); let name_judge = &"Kaan".to_string();
let _follower = create(name_follower, &connection).await.unwrap(); let _victim = create(name_victim, &connection).await.unwrap();
let _followed = create(name_followed, &connection).await.unwrap(); let _judge = create(name_judge, &connection).await.unwrap();
let _follower = follow(name_follower, name_followed, &connection) let _victim = ban(name_victim, name_judge, &connection).await.unwrap();
.await
.unwrap();
assert_eq!( assert_eq!(is_banned(name_victim, name_judge, &connection).await, true);
is_followed(name_follower, name_followed, &connection).await,
true
);
let _cleaning = connection.query("DELETE channel;").await; let _cleaning = connection.query("DELETE channel;").await;
} }
#[test] #[test]
async fn test_is_followed_nonfollowed() { async fn test_is_banned_nonbanned() {
let connection = create_connection_for_tests("test_is_follower_nonfollowed").await; let connection = create_connection_for_tests("test_is_banned_nonbanned").await;
let name_follower = &"Ahmet".to_string(); let name_victim = &"Ahmet".to_string();
let name_followed = &"Kaan".to_string(); let name_judge = &"Kaan".to_string();
let _follower = create(name_follower, &connection).await.unwrap(); let _victim = create(name_victim, &connection).await.unwrap();
let _followed = create(name_followed, &connection).await.unwrap(); let _judge = create(name_judge, &connection).await.unwrap();
assert_eq!( assert_eq!(is_banned(name_victim, name_judge, &connection).await, false);
is_followed(name_follower, name_followed, &connection).await,
false
);
let _cleaning = connection.query("DELETE channel;").await; let _cleaning = connection.query("DELETE channel;").await;
} }