From e6906cccc33ad8f1d01864b28c2bb31512de750c Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sun, 7 Mar 2021 15:49:34 +0100 Subject: [PATCH] Send message when user leave conversation --- src/helpers/conversations_helper.rs | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/helpers/conversations_helper.rs b/src/helpers/conversations_helper.rs index c07942d..8590d56 100644 --- a/src/helpers/conversations_helper.rs +++ b/src/helpers/conversations_helper.rs @@ -4,7 +4,7 @@ use crate::constants::database_tables_names::{CONV_LIST_TABLE, CONV_MEMBERS_TABLE, CONV_MESSAGES_TABLE}; use crate::data::conversation::{Conversation, ConversationMember, ConvID, NewConversationSettings}; -use crate::data::conversation_message::{ConversationMessage, ConversationMessageFile, ConversationServerMessageType, UserAddedAnotherUserToConversation}; +use crate::data::conversation_message::{ConversationMessage, ConversationMessageFile, ConversationServerMessageType, UserAddedAnotherUserToConversation, UserRemovedAnotherUserToConversation}; use crate::data::error::{ExecError, Res, ResultBoxError}; use crate::data::new_conversation::NewConversation; use crate::data::new_conversation_message::NewConversationMessage; @@ -65,8 +65,8 @@ pub fn add_member(conv_id: ConvID, user_id: &UserID, following: bool, admin: boo conv_id, ConversationServerMessageType::UserAddedAnotherUserToConversation(UserAddedAnotherUserToConversation { user_who_added: adder.clone(), - user_added: user_id.clone() - }) + user_added: user_id.clone(), + }), ) )?; } @@ -448,7 +448,7 @@ pub fn delete_conversation(conv: &Conversation) -> ResultBoxError<()> { } /// Delete a conversation membership -pub fn remove_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)?; } @@ -459,7 +459,21 @@ pub fn remove_member(user_id: &UserID, conv_id: ConvID, _remover: &UserID) -> Re .cond_user_id("user_id", user_id) .exec()?; - // TODO : create a message + // Create a message + if remover == user_id { + send_message(&NewConversationMessage::new_server_message( + conv_id, + ConversationServerMessageType::UserLeftConversation(user_id.clone()), + ))?; + } else { + send_message(&NewConversationMessage::new_server_message( + conv_id, + ConversationServerMessageType::UserRemovedFromConversation(UserRemovedAnotherUserToConversation { + user_who_removed: remover.clone(), + user_removed: user_id.clone(), + }), + ))?; + } Ok(()) }