Skip to content

Commit

Permalink
feat: ✨ is_banned
Browse files Browse the repository at this point in the history
docs: 📝 is_banned
revert: 🔥 remove is_followed
  • Loading branch information
Tahinli committed Apr 25, 2024
1 parent f49de4c commit 8c04db0
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 52 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ Unban User: "/unban/:victim/:judge"

Is Follower: "/is-follower/:follower/:follower"

Is Followed: "/is-follower/:follower/:follower"
Is Banned: "/is-banned/:victim/:judge"
4 changes: 2 additions & 2 deletions src/db/db_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,6 @@ pub async fn is_follower(follower: &String, followed: &String, db: &Surreal<Clie
is_follower_by_username(follower, followed, db).await
}

pub async fn is_followed(follower: &String, followed: &String, db: &Surreal<Client>) -> bool {
is_followed_by_username(follower, followed, db).await
pub async fn is_banned(victim: &String, judge: &String, db: &Surreal<Client>) -> bool {
is_banned_by_username(victim, judge, db).await
}
24 changes: 10 additions & 14 deletions src/db/db_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,30 +505,26 @@ pub async fn is_follower_by_username(
}
}

pub async fn is_followed_by_username(
follower: &String,
followed: &String,
db: &Surreal<Client>,
) -> 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<Client>) -> 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
}
}
Expand Down
22 changes: 11 additions & 11 deletions src/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub async fn routing(State(state): State<AppState>) -> 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())
}
Expand Down Expand Up @@ -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<AppState>,
) -> 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))
}
}
}
39 changes: 15 additions & 24 deletions src/tests/db_tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#[cfg(test)]
use crate::db::db_operations::*;
use tokio::test;
Expand Down Expand Up @@ -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;
}

0 comments on commit 8c04db0

Please sign in to comment.