Skip to content

Commit

Permalink
feat: ✨ is_follower, is_followed
Browse files Browse the repository at this point in the history
docs: 📝 is_follower, is_followed
  • Loading branch information
Tahinli committed Apr 25, 2024
1 parent 4f46797 commit f49de4c
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 3 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ Unfollow User: "/unfollow/:follower/:followed"

Ban User: "/ban/:victim/:judge"

Unban User: "/unban/:victim/:judge"
Unban User: "/unban/:victim/:judge"

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

Is Followed: "/is-follower/:follower/:follower"
8 changes: 8 additions & 0 deletions src/db/db_operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,11 @@ pub async fn unban(victim: &String, judge: &String, db: &Surreal<Client>) -> Opt
None => None,
}
}

pub async fn is_follower(follower: &String, followed: &String, db: &Surreal<Client>) -> bool {
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
}
58 changes: 58 additions & 0 deletions src/db/db_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,3 +475,61 @@ pub async fn remove_all_banned_from(channel: Channel, db: &Surreal<Client>) -> O
}
search_channel_by_username(&channel.username, db).await
}

pub async fn is_follower_by_username(
follower: &String,
followed: &String,
db: &Surreal<Client>,
) -> bool {
match search_channel_by_username(follower, db).await {
Some(follower_channel) => match search_channel_by_username(followed, db).await {
Some(mut followed_channel) => {
followed_channel.follower_list.sort();
match followed_channel
.follower_list
.binary_search(&follower_channel.id.unwrap().id)
{
Ok(_) => true,
Err(_) => false,
}
}
None => {
eprintln!("Error: Can't Check Is Follower | Followed Not Exists");
false
}
},
None => {
eprintln!("Error: Can't Check Is Follower | Follower Not Exists");
false
}
}
}

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)
{
Ok(_) => true,
Err(_) => false,
}
}
None => {
eprintln!("Error: Can't Check Is Follower | Followed Not Exists");
false
}
},
None => {
eprintln!("Error: Can't Check Is Follower | Follower Not Exists");
false
}
}
}
44 changes: 44 additions & 0 deletions src/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub async fn routing(State(state): State<AppState>) -> Router {
.route("/unfollow/:follower/:followed", get(unfollow))
.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))
.layer(CorsLayer::permissive())
.with_state(state.clone())
}
Expand Down Expand Up @@ -157,3 +159,45 @@ async fn unban(
None => (StatusCode::NOT_ACCEPTABLE, Json(serde_json::json!(""))),
}
}

async fn is_follower(
Path((follower, followed)): Path<(String, String)>,
State(state): State<AppState>,
) -> impl IntoResponse {
let is_follower = db_operations::is_follower(&follower, &followed, &state.db).await;
match is_follower {
true => {
let is_follower = serde_json::json!({
"is_follower":true
});
(StatusCode::OK, Json(is_follower))
}
false => {
let is_follower = serde_json::json!({
"is_follower":false
});
(StatusCode::OK, Json(is_follower))
}
}
}

async fn is_followed(
Path((follower, followed)): 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 {
true => {
let is_followed = serde_json::json!({
"is_followed":true
});
(StatusCode::OK, Json(is_followed))
}
false => {
let is_followed = serde_json::json!({
"is_followed":false
});
(StatusCode::OK, Json(is_followed))
}
}
}
79 changes: 78 additions & 1 deletion src/tests/db_tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

#[cfg(test)]
use crate::db::db_operations::*;
use tokio::test;
Expand All @@ -6,7 +7,7 @@ use tokio::test;
async fn create_connection_for_tests(
db_name: &str,
) -> surrealdb::Surreal<surrealdb::engine::remote::ws::Client> {
let connection = surrealdb::Surreal::new::<surrealdb::engine::remote::ws::Ws>("127.0.0.1:8000")
let connection = surrealdb::Surreal::new::<surrealdb::engine::remote::ws::Ws>("127.0.0.1:5000")
.await
.unwrap();
connection
Expand Down Expand Up @@ -444,3 +445,79 @@ async fn test_search_id_noncreated() {

let _cleaning = connection.query("DELETE channel;").await;
}

#[test]
async fn test_is_follower_already_follower() {
let connection = create_connection_for_tests("test_is_follower_already_follower").await;
let name_follower = &"Ahmet".to_string();
let name_followed = &"Kaan".to_string();

let _follower = create(name_follower, &connection).await.unwrap();
let _followed = create(name_followed, &connection).await.unwrap();

let _follower = follow(name_follower, name_followed, &connection)
.await
.unwrap();

assert_eq!(
is_follower(name_follower, name_followed, &connection).await,
true
);

let _cleaning = connection.query("DELETE channel;").await;
}

#[test]
async fn test_is_follower_nonfollower() {
let connection = create_connection_for_tests("test_is_follower_nonfollower").await;
let name_follower = &"Ahmet".to_string();
let name_followed = &"Kaan".to_string();

let _follower = create(name_follower, &connection).await.unwrap();
let _followed = create(name_followed, &connection).await.unwrap();

assert_eq!(
is_follower(name_follower, name_followed, &connection).await,
false
);

let _cleaning = connection.query("DELETE channel;").await;
}

#[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();

let _follower = create(name_follower, &connection).await.unwrap();
let _followed = create(name_followed, &connection).await.unwrap();

let _follower = follow(name_follower, name_followed, &connection)
.await
.unwrap();

assert_eq!(
is_followed(name_follower, name_followed, &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();

let _follower = create(name_follower, &connection).await.unwrap();
let _followed = create(name_followed, &connection).await.unwrap();

assert_eq!(
is_followed(name_follower, name_followed, &connection).await,
false
);

let _cleaning = connection.query("DELETE channel;").await;
}
2 changes: 1 addition & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub async fn database_config() -> DataBaseConfig {
let dirty: Vec<&str> = element.split(": ").collect();
configs_cleaned.push(dirty[1]);
}

DataBaseConfig {
address: configs_cleaned[0].to_string(),
username: configs_cleaned[1].to_string(),
Expand Down

0 comments on commit f49de4c

Please sign in to comment.