From 9a3b565f0020d205d848474dcdfc119ec707ba47 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 30 Jun 2020 07:49:23 +0200 Subject: [PATCH] Can get the list of friends of another user --- src/controllers/friends_controller.rs | 19 ++++++++++++++++++- src/controllers/routes.rs | 2 ++ src/data/http_request_handler.rs | 5 +++++ src/helpers/user_helper.rs | 5 +++++ 4 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/controllers/friends_controller.rs b/src/controllers/friends_controller.rs index 759a915..2c7e97a 100644 --- a/src/controllers/friends_controller.rs +++ b/src/controllers/friends_controller.rs @@ -5,7 +5,7 @@ use crate::api_data::friend_api::FriendAPI; use crate::controllers::routes::RequestResult; use crate::data::http_request_handler::HttpRequestHandler; -use crate::helpers::{account_helper, friends_helper}; +use crate::helpers::{account_helper, friends_helper, user_helper}; /// Get the list of friends of the current user pub fn get_list(r: &mut HttpRequestHandler) -> RequestResult { @@ -17,4 +17,21 @@ pub fn get_list(r: &mut HttpRequestHandler) -> RequestResult { } r.set_response(FriendAPI::from_list(&list)) +} + +/// Get the list of friends of another user +pub fn get_other_user_list(r: &mut HttpRequestHandler) -> RequestResult { + let user_id = r.post_user_id("userID")?; + + if !user_helper::can_see_user_page(&r.user_id_or_invalid(), &user_id)? { + r.forbidden("You can not access this user information!".to_string())?; + } + + if !user_helper::is_user_friends_list_public(&user_id)? { + r.forbidden("The friend list of this user is not public!".to_string())?; + } + + let friends = friends_helper::get_list(&user_id)?; + + r.set_response(friends.iter().map(|f| f.friend_id.id()).collect::>()) } \ No newline at end of file diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index 463f629..cd44606 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -91,6 +91,8 @@ pub fn get_routes() -> Vec { // Friends controller Route::post("/friends/getList", Box::new(friends_controller::get_list)), + Route::post("/friends/get_user_list", Box::new(friends_controller::get_other_user_list)), + // 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 2148361..946963b 100644 --- a/src/data/http_request_handler.rs +++ b/src/data/http_request_handler.rs @@ -379,6 +379,11 @@ impl HttpRequestHandler { self.curr_user_id.clone() } + /// Get current user ID, return invalid user id value if there is none + pub fn user_id_or_invalid(&self) -> UserID { + self.user_id_opt().unwrap_or(UserID::invalid()) + } + /// 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/user_helper.rs b/src/helpers/user_helper.rs index d861ca0..9e66e61 100644 --- a/src/helpers/user_helper.rs +++ b/src/helpers/user_helper.rs @@ -111,6 +111,11 @@ pub fn allow_posts_on_his_page(user_id: &UserID) -> ResultBoxError { Ok(find_user_by_id(user_id)?.allow_posts_from_friends) } +/// Check out whether the friends list of a user is public or not +pub fn is_user_friends_list_public(user_id: &UserID) -> ResultBoxError { + Ok(find_user_by_id(user_id)?.public_friends_list) +} + /// Check out if a user can create posts on another user page pub fn can_create_posts(user_id: &UserID, target_id: &UserID) -> ResultBoxError {