From 92787dbb4de89183c69118ed1ee918fc31493046 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sun, 25 Apr 2021 15:58:24 +0200 Subject: [PATCH] Replace get_members_info with a more appropriate route --- src/api_data/forez_member_api.rs | 26 -------------------------- src/api_data/mod.rs | 3 +-- src/controllers/forez_controller.rs | 16 ++++++++-------- src/data/group_member.rs | 4 ++++ src/routes.rs | 2 +- 5 files changed, 14 insertions(+), 37 deletions(-) delete mode 100644 src/api_data/forez_member_api.rs diff --git a/src/api_data/forez_member_api.rs b/src/api_data/forez_member_api.rs deleted file mode 100644 index bfb990b..0000000 --- a/src/api_data/forez_member_api.rs +++ /dev/null @@ -1,26 +0,0 @@ -//! #Forez member information -//! -//! @author Pierre Hubert - -use serde::Serialize; - -use crate::api_data::group_member_api::GroupMemberAPI; -use crate::api_data::user_info::{APIAdvancedInfo, APIUserInfo}; -use crate::data::error::Res; -use crate::data::group_member::GroupMember; -use crate::data::user::{User, UserID}; - -#[derive(Serialize)] -pub struct ForezMemberAPI { - user: APIAdvancedInfo, - membership: GroupMemberAPI, -} - -impl ForezMemberAPI { - pub fn new(user: &User, membership: &GroupMember, request_from: UserID) -> Res { - Ok(Self { - user: APIUserInfo::new_advanced_info(&Some(request_from.clone()), user)?, - membership: GroupMemberAPI::new(membership), - }) - } -} \ No newline at end of file diff --git a/src/api_data/mod.rs b/src/api_data/mod.rs index 7ee7962..a61204a 100644 --- a/src/api_data/mod.rs +++ b/src/api_data/mod.rs @@ -70,5 +70,4 @@ pub mod removed_user_from_conv_message; pub mod user_is_writing_message_in_conversation; pub mod res_create_conversation_for_group; pub mod notification_settings_api; -pub mod push_notifications_status_api; -pub mod forez_member_api; \ No newline at end of file +pub mod push_notifications_status_api; \ No newline at end of file diff --git a/src/controllers/forez_controller.rs b/src/controllers/forez_controller.rs index 2fc0398..d48f26e 100644 --- a/src/controllers/forez_controller.rs +++ b/src/controllers/forez_controller.rs @@ -5,8 +5,8 @@ //! //! @author Pierre Hubert -use crate::api_data::forez_member_api::ForezMemberAPI; use crate::api_data::group_api::GroupApi; +use crate::api_data::user_info::APIUserInfo; use crate::data::base_request_handler::BaseRequestHandler; use crate::data::config::conf; use crate::data::http_request_handler::HttpRequestHandler; @@ -27,18 +27,18 @@ pub fn get_list_groups(r: &mut HttpRequestHandler) -> RequestResult { } /// Get advanced information about all the members of a group -pub fn get_members_info(r: &mut HttpRequestHandler) -> RequestResult { +pub fn get_member_info(r: &mut HttpRequestHandler) -> RequestResult { let group = r.post_forez_group("group")?; + let member = r.post_user_id("user")?; - let members = groups_helper::get_list_members(&group)?; - let mut users = vec![]; + let membership = groups_helper::get_membership(&group, Some(member.clone()))?; - for member in members { - let user = user_helper::find_user_by_id(&member.user_id)?; - users.push(ForezMemberAPI::new(&user, &member, r.user_id()?)?); + if !membership.is_at_least_member() { + r.bad_request("This user is not a member of the group yet!".to_string())?; } - r.set_response(users) + let user = user_helper::find_user_by_id(&member)?; + r.set_response(APIUserInfo::new_advanced_info(&Some(member), &user)?) } /// Set presence diff --git a/src/data/group_member.rs b/src/data/group_member.rs index ede51fc..def15ac 100644 --- a/src/data/group_member.rs +++ b/src/data/group_member.rs @@ -69,6 +69,10 @@ impl GroupMember { pub fn is_moderator(&self) -> bool { self.level <= GroupMembershipLevel::MODERATOR } + + pub fn is_at_least_member(&self) -> bool { + self.level <= GroupMembershipLevel::MEMBER + } } #[cfg(test)] diff --git a/src/routes.rs b/src/routes.rs index a6a01df..78d505b 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -300,6 +300,6 @@ pub fn get_routes() -> Vec { // Forez controller Route::post("/forez/get_groups", Box::new(forez_controller::get_list_groups)), - Route::post("/forez/get_members_info", Box::new(forez_controller::get_members_info)), + Route::post("/forez/get_member_info", Box::new(forez_controller::get_member_info)), ] } \ No newline at end of file