From e59541720df5ffbc561a5c0d2a85ac6c2afdfa78 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 30 Jun 2020 14:05:36 +0200 Subject: [PATCH] Can respond to a friendship request --- src/controllers/friends_controller.rs | 16 ++++++++++++++++ src/controllers/routes.rs | 2 ++ src/data/http_request_handler.rs | 8 ++++++++ src/helpers/friends_helper.rs | 22 ++++++++++++++++++++++ 4 files changed, 48 insertions(+) diff --git a/src/controllers/friends_controller.rs b/src/controllers/friends_controller.rs index 926c64c..3037f16 100644 --- a/src/controllers/friends_controller.rs +++ b/src/controllers/friends_controller.rs @@ -100,4 +100,20 @@ pub fn cancel_request(r: &mut HttpRequestHandler) -> RequestResult { // TODO : delete related notifications r.success("Friendship request removed!") +} + +/// Respond to a friendship request +pub fn respond_request(r: &mut HttpRequestHandler) -> RequestResult { + let friend_id = r.post_user_id("friendID")?; + let accept = r.post_bool("accept")?; + + if !friends_helper::sent_request(&friend_id, r.user_id_ref()?)? { + r.forbidden("No friendship request was sent by this user!".to_string())?; + } + + friends_helper::respond_request(r.user_id_ref()?, &friend_id, accept)?; + + // TODO : create a notification + + r.set_response("Response to the friendship request successfully saved!") } \ No newline at end of file diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index f47c435..e7537fa 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -101,6 +101,8 @@ pub fn get_routes() -> Vec { Route::post("/friends/removeRequest", Box::new(friends_controller::cancel_request)), + Route::post("/friends/respondRequest", Box::new(friends_controller::respond_request)), + // 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 946963b..d865884 100644 --- a/src/data/http_request_handler.rs +++ b/src/data/http_request_handler.rs @@ -384,6 +384,14 @@ impl HttpRequestHandler { self.user_id_opt().unwrap_or(UserID::invalid()) } + /// Get user ID as a reference + pub fn user_id_ref(&self) -> ResultBoxError<&UserID> { + match self.curr_user_id.as_ref() { + Some(s) => Ok(s), + None => Err(ExecError::boxed_new("Could not get required user ID!")) + } + } + /// Get an email included in the request pub fn post_email(&mut self, name: &str) -> ResultBoxError { let mail = self.post_string(name)?; diff --git a/src/helpers/friends_helper.rs b/src/helpers/friends_helper.rs index 278e0af..dee3a50 100644 --- a/src/helpers/friends_helper.rs +++ b/src/helpers/friends_helper.rs @@ -89,6 +89,28 @@ pub fn sent_request(user_id: &UserID, target_user: &UserID) -> ResultBoxError 0) } +/// Respond to a friendship request +pub fn respond_request(user_id: &UserID, friend_id: &UserID, accept: bool) -> ResultBoxError { + // Delete the request + remove_request(friend_id, user_id)?; + + if accept { + database::InsertQuery::new(FRIENDS_TABLE) + .add_user_id("ID_personne", user_id) + .add_user_id("ID_amis", friend_id) + .add_legacy_bool("actif", true) + .insert_drop_result()?; + + database::InsertQuery::new(FRIENDS_TABLE) + .add_user_id("ID_personne", friend_id) + .add_user_id("ID_amis", user_id) + .add_legacy_bool("actif", true) + .insert_drop_result()?; + } + + Ok(()) +} + /// 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)