From 7cf5977da2362e2e485b4349a095e3b8bbdc8c90 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 6 Feb 2021 16:26:53 +0100 Subject: [PATCH] Propagate unread conversations number update --- src/controllers/conversations_controller.rs | 14 ++++++++++++++ src/helpers/conversations_helper.rs | 15 +++++++++++++-- src/helpers/events_helper.rs | 3 +++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/controllers/conversations_controller.rs b/src/controllers/conversations_controller.rs index 7640f93..49a98ec 100644 --- a/src/controllers/conversations_controller.rs +++ b/src/controllers/conversations_controller.rs @@ -344,6 +344,20 @@ pub fn delete_message(r: &mut HttpRequestHandler) -> RequestResult { /// Events handler pub fn handle_event(e: &events_helper::Event) -> Res { match e { + Event::UpdatedNumberUnreadConversations(users) => { + for user in users.iter() { + if user_ws_controller::is_user_connected(user) { + user_ws_controller::send_message_to_user( + &UserWsMessage::no_id_message( + "number_unread_conversations", + conversations_helper::count_unread_for_user(user)?, + )?, + user, + )?; + } + } + } + Event::NewConversationMessage(msg) => { user_ws_controller::send_message_to_specific_connections( |f| f.conversations.contains(&msg.conv_id), diff --git a/src/helpers/conversations_helper.rs b/src/helpers/conversations_helper.rs index f10ded4..d3c754b 100644 --- a/src/helpers/conversations_helper.rs +++ b/src/helpers/conversations_helper.rs @@ -312,6 +312,15 @@ pub fn send_message(msg: &NewConversationMessage) -> ResultBoxError<()> { .set_u64("last_active", t) .exec()?; + // Get the list of users to notify after the update + let list_to_notify = database::QueryInfo::new(CONV_USERS_TABLE) + .cond_u64("conv_id", msg.conv_id) + .cond_legacy_bool("saw_last_message", true) + .cond_legacy_bool("following", true) + .set_custom_where("user_id != ?") + .add_custom_where_argument_user_id(&msg.user_id) + .exec(|r| r.get_user_id("user_id"))?; + // Mark all the users of the conversation as unread database::UpdateInfo::new(CONV_USERS_TABLE) .cond_u64("conv_id", msg.conv_id) @@ -323,7 +332,8 @@ pub fn send_message(msg: &NewConversationMessage) -> ResultBoxError<()> { .set_legacy_bool("saw_last_message", false) .exec()?; - // TODO : send an event (updated_number_unread_conversations) + // Send an event (updated_number_unread_conversations) + events_helper::propagate_event(&Event::UpdatedNumberUnreadConversations(&list_to_notify))?; // Send an event (sent_conversation_message) events_helper::propagate_event(&Event::NewConversationMessage(&get_single_message(msg_id)?))?; @@ -411,7 +421,8 @@ pub fn mark_user_seen(conv_id: u64, user_id: &UserID) -> ResultBoxError<()> { .set_legacy_bool("saw_last_message", true) .exec()?; - // TODO : push an event (updated_number_unread_conversations) + // Push an event (updated_number_unread_conversations) + events_helper::propagate_event(&Event::UpdatedNumberUnreadConversations(&vec![user_id.clone()]))?; Ok(()) } diff --git a/src/helpers/events_helper.rs b/src/helpers/events_helper.rs index 7d3da3c..7cb4886 100644 --- a/src/helpers/events_helper.rs +++ b/src/helpers/events_helper.rs @@ -14,6 +14,9 @@ pub enum Event<'a> { /// Updated the number of notifications of one of multiple users user UpdatedNotificationsNumber(&'a Vec), + /// Updated the number of unread conversations + UpdatedNumberUnreadConversations(&'a Vec), + /// Created a new conversation message NewConversationMessage(&'a ConversationMessage),