From 72a9553f1a835866d3e90188136d4cb5164c3ff0 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 6 Mar 2021 17:57:04 +0100 Subject: [PATCH] Can add member to conversations --- src/controllers/conversations_controller.rs | 78 +++++++-------------- src/data/conversation.rs | 10 +++ src/helpers/conversations_helper.rs | 59 +++------------- src/routes.rs | 1 + 4 files changed, 47 insertions(+), 101 deletions(-) diff --git a/src/controllers/conversations_controller.rs b/src/controllers/conversations_controller.rs index 6455bc8..ad7cfa2 100644 --- a/src/controllers/conversations_controller.rs +++ b/src/controllers/conversations_controller.rs @@ -11,7 +11,7 @@ use crate::api_data::res_find_private_conversations::ResFindPrivateConversations use crate::constants::{ALLOWED_CONVERSATION_FILES_TYPES, CONVERSATION_FILES_MAX_SIZE, MAX_CONVERSATION_MESSAGE_LENGTH, MIN_CONVERSATION_MESSAGE_LENGTH}; use crate::controllers::user_ws_controller; use crate::data::base_request_handler::{BaseRequestHandler, RequestValue}; -use crate::data::conversation::{ConversationMemberSetting, NewConversationSettings}; +use crate::data::conversation::NewConversationSettings; use crate::data::conversation_message::ConversationMessageFile; use crate::data::error::Res; use crate::data::http_request_handler::HttpRequestHandler; @@ -87,7 +87,6 @@ pub fn get_single(r: &mut HttpRequestHandler) -> RequestResult { /// Update the settings of a conversation pub fn update_settings(r: &mut HttpRequestHandler) -> RequestResult { let conv_membership = r.post_conv("conversationID")?; - let conv = conversations_helper::get_single(conv_membership.conv_id)?; // Update following state, if required if r.has_post_parameter("following") { @@ -98,57 +97,6 @@ pub fn update_settings(r: &mut HttpRequestHandler) -> RequestResult { )?; } - // Update members list - if r.has_post_parameter("members") && !conv.is_managed() { - let mut members = r.post_users_id("members")? - .into_iter() - .map(|user_id| ConversationMemberSetting { user_id, set_admin: false }) - .collect::>(); - let admins = r.post_users_id("admins")?; - - let can_everyone_add_members = conversations_helper::can_everyone_add_members(conv_membership.conv_id)?; - - if !conv_membership.is_admin && !can_everyone_add_members { - r.forbidden("You can not update the list of members of this conversation!".to_string())?; - } - - // Set same admin status as earlier - if !conv_membership.is_admin { - members = members - .into_iter() - .map(|mut n| { - n.set_admin = conv.is_admin(&n.user_id); - n - }) - .collect() - } - - // If the user is an admin, use the values he gave - else { - members = members - .into_iter() - .map(|mut n| { - n.set_admin = admins.contains(&n.user_id); - n - }) - .collect() - } - - // Current user can not touch his own membership, so revert it back forcefully - members = members - .into_iter() - .filter(|m| !m.user_id.eq(&conv_membership.user_id)) - .collect::>(); - - members.push(ConversationMemberSetting { - user_id: r.user_id()?, - set_admin: conv_membership.is_admin, - }); - - - conversations_helper::set_members(conv_membership.conv_id, &members, conv_membership.is_admin)?; - } - // Change moderator settings if r.has_post_parameter("name") && r.has_post_parameter("canEveryoneAddMembers") @@ -171,6 +119,30 @@ pub fn update_settings(r: &mut HttpRequestHandler) -> RequestResult { r.success("Conversation information successfully updated!") } +/// Add a new member to a conversation +pub fn add_member(r: &mut HttpRequestHandler) -> RequestResult { + let conv_membership = r.post_conv("convID")?; + let conv = conversations_helper::get_single(conv_membership.conv_id)?; + + let user_to_add = r.post_user_id("userID")?; + + if conv.is_managed() { + r.bad_request("This conversation is managed, you can not manually change its members!".to_string())?; + } + + if !conv.can_user_add_members(r.user_id_ref()?) { + r.forbidden("You are not allowed to add members to this conversation!".to_string())?; + } + + if conv.get_membership(&user_to_add).is_some() { + r.bad_request("This user is already a member of this group!".to_string())?; + } + + conversations_helper::add_member(conv.id, &user_to_add, true, false, r.user_id_ref()?)?; + + r.success("The user was added to the conversation!") +} + /// Find a private conversation pub fn find_private(r: &mut HttpRequestHandler) -> RequestResult { let other_user = r.post_user_id("otherUser")?; diff --git a/src/data/conversation.rs b/src/data/conversation.rs index e2072ff..ba717ef 100644 --- a/src/data/conversation.rs +++ b/src/data/conversation.rs @@ -70,6 +70,11 @@ impl Conversation { .any(|m| m.user_id == user_id && m.is_admin) } + /// Check out whether a user can add members to a conversation or not + pub fn can_user_add_members(&self, user_id: &UserID) -> bool { + !self.is_managed() && (self.is_admin(user_id) || self.can_everyone_add_members) + } + /// Check out whether a user is the last administrator of a conversation or not pub fn is_last_admin(&self, user_id: &UserID) -> bool { let admins: Vec<&ConversationMember> = self.members @@ -79,6 +84,11 @@ impl Conversation { admins.len() == 1 && admins[0].user_id == user_id } + + /// Get the membership of a user over a conversation + pub fn get_membership(&self, user_id: &UserID) -> Option<&ConversationMember> { + self.members.iter().filter(|u| u.user_id == user_id).next() + } } /// Structure used to update the list of members of the conversation diff --git a/src/helpers/conversations_helper.rs b/src/helpers/conversations_helper.rs index bcb9a18..02606ab 100644 --- a/src/helpers/conversations_helper.rs +++ b/src/helpers/conversations_helper.rs @@ -3,7 +3,7 @@ //! @author Pierre Hubert use crate::constants::database_tables_names::{CONV_LIST_TABLE, CONV_MEMBERS_TABLE, CONV_MESSAGES_TABLE}; -use crate::data::conversation::{Conversation, ConversationMember, ConversationMemberSetting, ConvID, NewConversationSettings}; +use crate::data::conversation::{Conversation, ConversationMember, ConvID, NewConversationSettings}; use crate::data::conversation_message::{ConversationMessage, ConversationMessageFile, ConversationServerMessageType}; use crate::data::error::{ExecError, Res, ResultBoxError}; use crate::data::new_conversation::NewConversation; @@ -42,14 +42,14 @@ pub fn create(conv: &NewConversation) -> Res { admin = true; } - add_member(conv_id, member, follow, admin)?; + add_member(conv_id, member, follow, admin, &conv.owner_id)?; } Ok(conv_id) } /// Add a member to a conversation -pub fn add_member(conv_id: ConvID, user_id: &UserID, following: bool, admin: bool) -> Res { +pub fn add_member(conv_id: ConvID, user_id: &UserID, following: bool, admin: bool, _adder: &UserID) -> Res { InsertQuery::new(CONV_MEMBERS_TABLE) .add_conv_id("conv_id", conv_id) .add_user_id("user_id", user_id) @@ -73,18 +73,6 @@ pub fn set_admin(conv_id: &ConvID, user_id: &UserID, admin: bool) -> Res { .exec() } -/// Remove a member from a conversation -pub fn remove_member(conv_id: ConvID, user_id: &UserID) -> ResultBoxError<()> { - database::DeleteQuery::new(CONV_MEMBERS_TABLE) - .cond_conv_id("conv_id", conv_id) - .cond_user_id("user_id", user_id) - .exec()?; - - // TODO : create a message - - Ok(()) -} - /// Get the list of conversations of a specific user pub fn get_list_user(user_id: &UserID) -> ResultBoxError> { database::QueryInfo::new(CONV_LIST_TABLE) @@ -147,36 +135,6 @@ pub fn set_following(user_id: &UserID, conv_id: ConvID, following: bool) -> Resu .exec() } -/// Set a new list of members for a given conversation -pub fn set_members(conv_id: ConvID, new_list: &Vec, can_delete: bool) -> ResultBoxError<()> { - let curr_list = get_list_members(conv_id)?; - - // Add new members - for member in new_list { - if let Some(user) = curr_list.iter().filter(|m| m.user_id == member.user_id).next() { - // Check if we have to update admin state - if user.is_admin != member.set_admin { - set_admin(&conv_id, &member.user_id, member.set_admin)?; - } - } else { - add_member(conv_id, &member.user_id, true, member.set_admin)?; - } - } - - // Remove a member - if can_delete { - for member in curr_list { - if new_list.iter().any(|m| m.user_id.eq(&member.user_id)) { - continue; - } - remove_member(conv_id, &member.user_id)?; - } - } - - - Ok(()) -} - /// Set a new name to the conversation pub fn set_settings(settings: NewConversationSettings) -> Res { database::UpdateInfo::new(CONV_LIST_TABLE) @@ -450,7 +408,7 @@ pub fn remove_user_from_conversation(user_id: &UserID, conv: &Conversation, remo if conv.is_last_admin(user_id) { delete_conversation(conv) } else { - delete_member(user_id, conv.id, remover) + remove_member(user_id, conv.id, remover) } } @@ -480,13 +438,18 @@ pub fn delete_conversation(conv: &Conversation) -> ResultBoxError<()> { } /// Delete a conversation membership -pub fn delete_member(user_id: &UserID, conv_id: ConvID, _remover: &UserID) -> ResultBoxError<()> { +pub fn remove_member(user_id: &UserID, conv_id: ConvID, _remover: &UserID) -> ResultBoxError<()> { for msg in get_user_messages_for_conversations(conv_id, user_id)? { delete_message(&msg)?; } // Delete membership - remove_member(conv_id, user_id)?; + database::DeleteQuery::new(CONV_MEMBERS_TABLE) + .cond_conv_id("conv_id", conv_id) + .cond_user_id("user_id", user_id) + .exec()?; + + // TODO : create a message Ok(()) } diff --git a/src/routes.rs b/src/routes.rs index dcae73e..c6f583a 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -194,6 +194,7 @@ pub fn get_routes() -> Vec { Route::post("/conversations/getList", Box::new(conversations_controller::get_list)), Route::post("/conversations/get_single", Box::new(conversations_controller::get_single)), Route::post("/conversations/updateSettings", Box::new(conversations_controller::update_settings)), + Route::post("/conversations/addMember", Box::new(conversations_controller::add_member)), Route::post("/conversations/getPrivate", Box::new(conversations_controller::find_private)), Route::post("/conversations/refresh_single", Box::new(conversations_controller::refresh_single)), Route::post("/conversations/get_older_messages", Box::new(conversations_controller::get_older_messages)),