diff --git a/src/controllers/friends_controller.rs b/src/controllers/friends_controller.rs index c044479..ab62ce3 100644 --- a/src/controllers/friends_controller.rs +++ b/src/controllers/friends_controller.rs @@ -120,11 +120,21 @@ pub fn respond_request(r: &mut HttpRequestHandler) -> RequestResult { /// Remove a friend from the list pub fn remove_friend(r: &mut HttpRequestHandler) -> RequestResult { - let friend_id = r.post_user_id("friendID")?; + let friend_id = r.post_friend_id("friendID")?; friends_helper::remove_friendship(r.user_id_ref()?, &friend_id)?; // TODO : Delete any related notification r.success("The friend was removed from the list!") +} + +/// Update following status +pub fn set_following(r: &mut HttpRequestHandler) -> RequestResult { + let friend_id = r.post_friend_id("friendID")?; + let follow = r.post_bool("follow")?; + + friends_helper::set_following(r.user_id_ref()?, &friend_id, follow)?; + + r.success("Following status updated!") } \ No newline at end of file diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index abc67b8..9934886 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -105,6 +105,8 @@ pub fn get_routes() -> Vec { Route::post("/friends/remove", Box::new(friends_controller::remove_friend)), + Route::post("/friends/setFollowing", Box::new(friends_controller::set_following)), + // Conversations controller Route::post("/conversations/create", Box::new(conversations_controller::create)), diff --git a/src/data/http_request_handler.rs b/src/data/http_request_handler.rs index d865884..7468fa1 100644 --- a/src/data/http_request_handler.rs +++ b/src/data/http_request_handler.rs @@ -17,7 +17,7 @@ use crate::data::error::{ExecError, ResultBoxError}; use crate::data::group::GroupAccessLevel; use crate::data::group_id::GroupID; use crate::data::user::UserID; -use crate::helpers::{account_helper, api_helper, conversations_helper, groups_helper, user_helper, virtual_directory_helper}; +use crate::helpers::{account_helper, api_helper, conversations_helper, friends_helper, groups_helper, user_helper, virtual_directory_helper}; use crate::helpers::virtual_directory_helper::VirtualDirType; use crate::utils::string_utils::{check_url, remove_html_nodes}; use crate::utils::user_data_utils::{generate_new_user_data_file_name, prepare_file_creation, user_data_path}; @@ -438,6 +438,21 @@ impl HttpRequestHandler { Ok(user_id) } + /// Get the ID of a friend included in a POST request + /// + /// *Note :* This function does not check whether the user exists or not before checking if the + /// two users are friend because as it is not possible to be friend with a non existent person + /// A single check is enough + pub fn post_friend_id(&mut self, name: &str) -> ResultBoxError { + let friend_id = UserID::new(self.post_u64(name)?); + + if !friends_helper::are_friend(&friend_id, self.user_id_ref()?)? { + self.forbidden("You are not friend with this person!".to_string())?; + } + + Ok(friend_id) + } + /// Get a virtual directory included in a POST request pub fn post_virtual_directory(&mut self, name: &str) -> ResultBoxError { let dir = self.post_string(name)?; diff --git a/src/helpers/friends_helper.rs b/src/helpers/friends_helper.rs index e817505..4c1abb6 100644 --- a/src/helpers/friends_helper.rs +++ b/src/helpers/friends_helper.rs @@ -163,6 +163,15 @@ pub fn is_following(user_id: &UserID, friend_id: &UserID) -> ResultBoxError 0) } +/// Update following status of a friendship +pub fn set_following(user_id: &UserID, friend_id: &UserID, follow: bool) -> ResultBoxError { + database::UpdateInfo::new(FRIENDS_TABLE) + .cond_user_id("ID_personne", user_id) + .cond_user_id("ID_amis", friend_id) + .set_legacy_bool("abonnement", follow) + .exec() +} + /// Get the status of a friendship pub fn get_status(user_id: &UserID, friend_id: &UserID) -> ResultBoxError { let mut status = FriendshipStatus {