From 0f4b7af44971ff1baeab8c2cdc2fcc6fb9a26535 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sun, 7 Mar 2021 19:30:02 +0100 Subject: [PATCH] Unregister user when removing him --- src/api_data/mod.rs | 3 ++- src/api_data/remove_user_from_conv_message.rs | 23 +++++++++++++++++++ src/controllers/conversations_controller.rs | 21 +++++++++++++++++ src/helpers/conversations_helper.rs | 3 +++ src/helpers/events_helper.rs | 3 +++ 5 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 src/api_data/remove_user_from_conv_message.rs diff --git a/src/api_data/mod.rs b/src/api_data/mod.rs index dd2b167..98a6cba 100644 --- a/src/api_data/mod.rs +++ b/src/api_data/mod.rs @@ -65,4 +65,5 @@ pub mod left_call_message; pub mod new_call_signal; pub mod call_peer_ready; pub mod call_peer_interrupted_streaming; -pub mod res_check_password_token; \ No newline at end of file +pub mod res_check_password_token; +pub mod remove_user_from_conv_message; \ No newline at end of file diff --git a/src/api_data/remove_user_from_conv_message.rs b/src/api_data/remove_user_from_conv_message.rs new file mode 100644 index 0000000..d7ab901 --- /dev/null +++ b/src/api_data/remove_user_from_conv_message.rs @@ -0,0 +1,23 @@ +//! # Removed user from conversation message +//! +//! @author Pierre Hubert + +use serde::Serialize; + +use crate::data::conversation::ConvID; +use crate::data::user::UserID; + +#[derive(Serialize)] +pub struct RemovedUserFromConversationMessage { + user_id: u64, + conv_id: u64, +} + +impl RemovedUserFromConversationMessage { + pub fn new(user_id: &UserID, conv_id: ConvID) -> Self { + Self { + user_id: user_id.id(), + conv_id: conv_id.id(), + } + } +} \ No newline at end of file diff --git a/src/controllers/conversations_controller.rs b/src/controllers/conversations_controller.rs index 8b532c9..4fd22d6 100644 --- a/src/controllers/conversations_controller.rs +++ b/src/controllers/conversations_controller.rs @@ -7,6 +7,7 @@ use std::collections::HashSet; use crate::api_data::conversation_api::ConversationAPI; use crate::api_data::conversation_message_api::ConversationMessageAPI; use crate::api_data::list_unread_conversations_api::UnreadConversationAPI; +use crate::api_data::remove_user_from_conv_message::RemovedUserFromConversationMessage; use crate::api_data::res_count_unread_conversations::ResultCountUnreadConversations; use crate::api_data::res_create_conversation::ResCreateConversation; use crate::api_data::res_find_private_conversations::ResFindPrivateConversations; @@ -508,6 +509,26 @@ pub fn handle_event(e: &events_helper::Event) -> Res { )?; } + Event::RemovedUserFromConversation(user_id, conv_id) => { + // Notify users + user_ws_controller::send_message_to_specific_connections( + |f| f.conversations.contains(conv_id), + |_| UserWsMessage::no_id_message("removed_user_from_conv", RemovedUserFromConversationMessage::new(user_id, *conv_id)), + None:: _>, + )?; + + // Disconnected users + user_ws_controller::foreach_connection(|f| { + if f.user_id() == user_id && f.conversations.contains(conv_id) { + f.clone().replace(|w| { + w.conversations.remove(conv_id); + }); + } + + Ok(()) + })?; + } + _ => {} } diff --git a/src/helpers/conversations_helper.rs b/src/helpers/conversations_helper.rs index 9f11924..f7ca1b0 100644 --- a/src/helpers/conversations_helper.rs +++ b/src/helpers/conversations_helper.rs @@ -488,6 +488,9 @@ pub fn remove_member(user_id: &UserID, conv_id: ConvID, remover: &UserID) -> Res ))?; } + // Propagate event + events_helper::propagate_event(&Event::RemovedUserFromConversation(user_id, conv_id))?; + Ok(()) } diff --git a/src/helpers/events_helper.rs b/src/helpers/events_helper.rs index 1d859aa..b183c92 100644 --- a/src/helpers/events_helper.rs +++ b/src/helpers/events_helper.rs @@ -38,6 +38,9 @@ pub enum Event<'a> { /// Deleted a conversation message DeleteConversationMessage(&'a ConversationMessage), + /// Removed a user from a conversation + RemovedUserFromConversation(&'a UserID, ConvID), + /// Created a new comment NewComment(&'a Comment),