feat: ✨ is_follower, is_followed
docs: 📝 is_follower, is_followed
This commit is contained in:
parent
4f46797169
commit
f49de4ce50
6 changed files with 194 additions and 3 deletions
|
@ -22,4 +22,8 @@ Unfollow User: "/unfollow/:follower/:followed"
|
||||||
|
|
||||||
Ban User: "/ban/:victim/:judge"
|
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"
|
|
@ -100,3 +100,11 @@ pub async fn unban(victim: &String, judge: &String, db: &Surreal<Client>) -> Opt
|
||||||
None => None,
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -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
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -24,6 +24,8 @@ pub async fn routing(State(state): State<AppState>) -> Router {
|
||||||
.route("/unfollow/:follower/:followed", get(unfollow))
|
.route("/unfollow/:follower/:followed", get(unfollow))
|
||||||
.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-followed/:follower/:followed", get(is_followed))
|
||||||
.layer(CorsLayer::permissive())
|
.layer(CorsLayer::permissive())
|
||||||
.with_state(state.clone())
|
.with_state(state.clone())
|
||||||
}
|
}
|
||||||
|
@ -157,3 +159,45 @@ async fn unban(
|
||||||
None => (StatusCode::NOT_ACCEPTABLE, Json(serde_json::json!(""))),
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
use crate::db::db_operations::*;
|
use crate::db::db_operations::*;
|
||||||
use tokio::test;
|
use tokio::test;
|
||||||
|
@ -6,7 +7,7 @@ use tokio::test;
|
||||||
async fn create_connection_for_tests(
|
async fn create_connection_for_tests(
|
||||||
db_name: &str,
|
db_name: &str,
|
||||||
) -> surrealdb::Surreal<surrealdb::engine::remote::ws::Client> {
|
) -> 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
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
connection
|
connection
|
||||||
|
@ -444,3 +445,79 @@ async fn test_search_id_noncreated() {
|
||||||
|
|
||||||
let _cleaning = connection.query("DELETE channel;").await;
|
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;
|
||||||
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ pub async fn database_config() -> DataBaseConfig {
|
||||||
let dirty: Vec<&str> = element.split(": ").collect();
|
let dirty: Vec<&str> = element.split(": ").collect();
|
||||||
configs_cleaned.push(dirty[1]);
|
configs_cleaned.push(dirty[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
DataBaseConfig {
|
DataBaseConfig {
|
||||||
address: configs_cleaned[0].to_string(),
|
address: configs_cleaned[0].to_string(),
|
||||||
username: configs_cleaned[1].to_string(),
|
username: configs_cleaned[1].to_string(),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue