From 0d8a514392ad0dcb159caaa3ea534f426ce48308 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 13 Mar 2021 12:22:35 +0100 Subject: [PATCH] Propagate an event when deleting a conversation --- src/controllers/conversations_controller.rs | 20 ++++++++++++++++++++ src/helpers/conversations_helper.rs | 3 +++ src/helpers/events_helper.rs | 3 +++ 3 files changed, 26 insertions(+) diff --git a/src/controllers/conversations_controller.rs b/src/controllers/conversations_controller.rs index 91a63bb..4abebeb 100644 --- a/src/controllers/conversations_controller.rs +++ b/src/controllers/conversations_controller.rs @@ -544,6 +544,26 @@ pub fn handle_event(e: &events_helper::Event) -> Res { })?; } + Event::DeletedConversation(conv_id) => { + // Notify users + user_ws_controller::send_message_to_specific_connections( + |f| f.conversations.contains(conv_id), + |_| UserWsMessage::no_id_message("deleted_conversation", conv_id.id()), + None:: _>, + )?; + + // Disconnect user from conversation + user_ws_controller::foreach_connection(|f| { + if 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 c0ef16e..d8c3eeb 100644 --- a/src/helpers/conversations_helper.rs +++ b/src/helpers/conversations_helper.rs @@ -453,6 +453,9 @@ pub fn delete_conversation(conv: &Conversation) -> ResultBoxError<()> { .cond_conv_id("id", conv.id) .exec()?; + // Propagate information + events_helper::propagate_event(&Event::DeletedConversation(conv.id))?; + Ok(()) } diff --git a/src/helpers/events_helper.rs b/src/helpers/events_helper.rs index 721f753..4ecc21a 100644 --- a/src/helpers/events_helper.rs +++ b/src/helpers/events_helper.rs @@ -44,6 +44,9 @@ pub enum Event<'a> { /// Removed a user from a conversation RemovedUserFromConversation(&'a UserID, ConvID), + /// Delete a conversation + DeletedConversation(ConvID), + /// Created a new comment NewComment(&'a Comment),