diff --git a/src/api_data/friendship_status_api.rs b/src/api_data/friendship_status_api.rs new file mode 100644 index 0000000..91e81af --- /dev/null +++ b/src/api_data/friendship_status_api.rs @@ -0,0 +1,27 @@ +//! # Friendship status API +//! +//! @author Pierre Hubert + +use serde::Serialize; + +use crate::data::friendship_status::FriendshipStatus; + +#[derive(Serialize)] +pub struct FriendshipStatusAPI { + are_friend: bool, + sent_request: bool, + received_request: bool, + following: bool, +} + +impl FriendshipStatusAPI { + /// Create a new instance + pub fn new(status: &FriendshipStatus) -> FriendshipStatusAPI { + FriendshipStatusAPI { + are_friend: status.are_friend, + sent_request: status.sent_request, + received_request: status.received_request, + following: status.following, + } + } +} \ No newline at end of file diff --git a/src/api_data/mod.rs b/src/api_data/mod.rs index 8e76a02..5e293b1 100644 --- a/src/api_data/mod.rs +++ b/src/api_data/mod.rs @@ -28,4 +28,5 @@ pub mod group_api; pub mod advanced_group_api; pub mod res_change_group_logo; pub mod group_member_api; -pub mod friend_api; \ No newline at end of file +pub mod friend_api; +pub mod friendship_status_api; \ No newline at end of file diff --git a/src/controllers/friends_controller.rs b/src/controllers/friends_controller.rs index 17b0f15..120d51d 100644 --- a/src/controllers/friends_controller.rs +++ b/src/controllers/friends_controller.rs @@ -3,6 +3,7 @@ //! @author Pierre Hubert use crate::api_data::friend_api::FriendAPI; +use crate::api_data::friendship_status_api::FriendshipStatusAPI; use crate::controllers::routes::RequestResult; use crate::data::http_request_handler::HttpRequestHandler; use crate::helpers::{account_helper, friends_helper, user_helper}; @@ -49,4 +50,14 @@ pub fn get_other_user_list(r: &mut HttpRequestHandler) -> RequestResult { let friends = friends_helper::GetFriendsQuery::new(&user_id).exec()?; r.set_response(friends.iter().map(|f| f.friend_id.id()).collect::>()) +} + +/// Get the status of a friendship +pub fn get_status(r: &mut HttpRequestHandler) -> RequestResult { + let friend_id = r.post_user_id("friendID")?; + let curr_user_id = r.user_id()?; + + let status = friends_helper::get_status(&curr_user_id, &friend_id)?; + + r.set_response(FriendshipStatusAPI::new(&status)) } \ No newline at end of file diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index 6da6354..1ee2817 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -95,6 +95,8 @@ pub fn get_routes() -> Vec { Route::post("/friends/get_user_list", Box::new(friends_controller::get_other_user_list)), + Route::post("/friends/getStatus", Box::new(friends_controller::get_status)), + // Conversations controller Route::post("/conversations/create", Box::new(conversations_controller::create)), diff --git a/src/data/friendship_status.rs b/src/data/friendship_status.rs new file mode 100644 index 0000000..546baa8 --- /dev/null +++ b/src/data/friendship_status.rs @@ -0,0 +1,10 @@ +//! # Friendship status +//! +//! @author Pierre Hubert + +pub struct FriendshipStatus { + pub are_friend: bool, + pub sent_request: bool, + pub received_request: bool, + pub following: bool, +} \ No newline at end of file diff --git a/src/data/mod.rs b/src/data/mod.rs index 3a7ffc5..fdc754d 100644 --- a/src/data/mod.rs +++ b/src/data/mod.rs @@ -17,4 +17,5 @@ pub mod group; pub mod new_group; pub mod group_member; pub mod global_search_result; -pub mod friend; \ No newline at end of file +pub mod friend; +pub mod friendship_status; \ No newline at end of file diff --git a/src/helpers/friends_helper.rs b/src/helpers/friends_helper.rs index 2e822b7..678abea 100644 --- a/src/helpers/friends_helper.rs +++ b/src/helpers/friends_helper.rs @@ -3,9 +3,11 @@ //! @author Pierre Hubert + use crate::constants::database_tables_names::{FRIENDS_TABLE, USERS_TABLE}; use crate::data::error::{ExecError, ResultBoxError}; use crate::data::friend::Friend; +use crate::data::friendship_status::FriendshipStatus; use crate::data::user::UserID; use crate::helpers::database; use crate::helpers::database::QueryInfo; @@ -59,6 +61,16 @@ fn get_list(friend_query: &GetFriendsQuery) -> ResultBoxError> { query.exec(db_to_friend) } +/// Check out wheterher a user has sent a request to another user +pub fn sent_request(user_id: &UserID, target_user: &UserID) -> ResultBoxError { + database::QueryInfo::new(FRIENDS_TABLE) + .cond_user_id("ID_personne", target_user) + .cond_user_id("ID_amis", user_id) + .cond_legacy_bool("actif", false) + .exec_count() + .map(|f| f > 0) +} + /// Check out whether two users are friend or not pub fn are_friend(user_one: &UserID, user_two: &UserID) -> ResultBoxError { Ok(database::count(QueryInfo::new(FRIENDS_TABLE) @@ -85,6 +97,42 @@ pub fn can_post_texts(user_id: &UserID, target_user: &UserID) -> ResultBoxError< .or(Ok(false)) } +/// Check out whether a user is following another user or not +pub fn is_following(user_id: &UserID, friend_id: &UserID) -> ResultBoxError { + database::QueryInfo::new(FRIENDS_TABLE) + .cond_user_id("ID_personne", user_id) + .cond_user_id("ID_amis", friend_id) + .cond_legacy_bool("actif", true) + .cond_legacy_bool("abonnement", true) + .exec_count() + .map(|f| f > 0) +} + +/// Get the status of a friendship +pub fn get_status(user_id: &UserID, friend_id: &UserID) -> ResultBoxError { + let mut status = FriendshipStatus { + are_friend: false, + sent_request: false, + received_request: false, + following: false, + }; + + status.are_friend = are_friend(user_id, friend_id)?; + + match status.are_friend { + false => { + status.sent_request = sent_request(user_id, friend_id)?; + status.received_request = sent_request(friend_id, user_id)?; + } + + true => { + status.following = is_following(user_id, friend_id)?; + } + } + + Ok(status) +} + /// Turn a database entry into a Friend structure fn db_to_friend(row: &database::RowResult) -> ResultBoxError { Ok(Friend {