From 4014fc14545de3a7edeb09e007b30013726b7630 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 8 Mar 2021 18:45:10 +0100 Subject: [PATCH] Propagate user is writing message event --- src/api_data/mod.rs | 3 ++- ...e.rs => removed_user_from_conv_message.rs} | 0 ...user_is_writing_message_in_conversation.rs | 22 +++++++++++++++++ src/controllers/conversations_controller.rs | 24 ++++++++++++++++++- src/data/user_ws_request_handler.rs | 16 +++++++++++-- src/helpers/events_helper.rs | 3 +++ src/user_ws_routes.rs | 5 +++- 7 files changed, 68 insertions(+), 5 deletions(-) rename src/api_data/{remove_user_from_conv_message.rs => removed_user_from_conv_message.rs} (100%) create mode 100644 src/api_data/user_is_writing_message_in_conversation.rs diff --git a/src/api_data/mod.rs b/src/api_data/mod.rs index 98a6cba..1f43be5 100644 --- a/src/api_data/mod.rs +++ b/src/api_data/mod.rs @@ -66,4 +66,5 @@ pub mod new_call_signal; pub mod call_peer_ready; pub mod call_peer_interrupted_streaming; pub mod res_check_password_token; -pub mod remove_user_from_conv_message; \ No newline at end of file +pub mod removed_user_from_conv_message; +pub mod user_is_writing_message_in_conversation; \ No newline at end of file diff --git a/src/api_data/remove_user_from_conv_message.rs b/src/api_data/removed_user_from_conv_message.rs similarity index 100% rename from src/api_data/remove_user_from_conv_message.rs rename to src/api_data/removed_user_from_conv_message.rs diff --git a/src/api_data/user_is_writing_message_in_conversation.rs b/src/api_data/user_is_writing_message_in_conversation.rs new file mode 100644 index 0000000..26e907a --- /dev/null +++ b/src/api_data/user_is_writing_message_in_conversation.rs @@ -0,0 +1,22 @@ +//! # User is writing message in conversation event +//! +//! @author Pierre Hubert +use serde::Serialize; + +use crate::data::conversation::ConvID; +use crate::data::user::UserID; + +#[derive(Serialize)] +pub struct UserIsWritingMessageInConversation { + user_id: u64, + conv_id: u64, +} + +impl UserIsWritingMessageInConversation { + 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 0f9b9e3..0ea5f5c 100644 --- a/src/controllers/conversations_controller.rs +++ b/src/controllers/conversations_controller.rs @@ -7,10 +7,11 @@ 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::removed_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; +use crate::api_data::user_is_writing_message_in_conversation::UserIsWritingMessageInConversation; 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}; @@ -22,6 +23,7 @@ use crate::data::new_conversation::NewConversation; use crate::data::new_conversation_message::NewConversationMessage; use crate::data::user_ws_connection::UserWsConnection; use crate::data::user_ws_message::UserWsMessage; +use crate::data::user_ws_request_handler::UserWsRequestHandler; use crate::helpers::{conversations_helper, events_helper}; use crate::helpers::events_helper::Event; use crate::routes::RequestResult; @@ -468,6 +470,18 @@ pub fn delete_message(r: &mut HttpRequestHandler) -> RequestResult { r.success("The message has been successfully deleted!") } +/// A user is writing a message in a conversation +pub fn member_is_writing(r: &mut UserWsRequestHandler) -> RequestResult { + let conv_id = r.post_registered_conv_id("convID")?; + + // Propagate event + events_helper::propagate_event( + &Event::UserIsWritingMessageInConversation(r.user_id_ref()?, conv_id) + )?; + + r.ok() +} + /// Events handler pub fn handle_event(e: &events_helper::Event) -> Res { match e { @@ -485,6 +499,14 @@ pub fn handle_event(e: &events_helper::Event) -> Res { } } + Event::UserIsWritingMessageInConversation(user_id, conv_id) => { + user_ws_controller::send_message_to_specific_connections( + |s| s.conversations.contains(conv_id) && s.user_id() != user_id, + |_| UserWsMessage::no_id_message("writing_message_in_conv", UserIsWritingMessageInConversation::new(user_id, *conv_id)), + None:: _>, + )?; + } + Event::NewConversationMessage(msg) => { user_ws_controller::send_message_to_specific_connections( |f| f.conversations.contains(&msg.conv_id), diff --git a/src/data/user_ws_request_handler.rs b/src/data/user_ws_request_handler.rs index 52a5d51..54c124a 100644 --- a/src/data/user_ws_request_handler.rs +++ b/src/data/user_ws_request_handler.rs @@ -5,11 +5,12 @@ use std::collections::HashMap; use serde::Serialize; use crate::api_data::http_error::HttpError; -use crate::routes::RequestResult; use crate::data::base_request_handler::{BaseRequestHandler, RequestValue}; -use crate::data::error::ResultBoxError; +use crate::data::conversation::ConvID; +use crate::data::error::{Res, ResultBoxError}; use crate::data::user_token::UserAccessToken; use crate::data::user_ws_connection::UserWsConnection; +use crate::routes::RequestResult; pub enum UserWsResponseType { SUCCESS, @@ -61,6 +62,17 @@ impl UserWsRequestHandler { Ok(()) } + + /// Get the ID of a conversation the user is registered to + pub fn post_registered_conv_id(&mut self, name: &str) -> Res { + let conv_id = ConvID::new(self.post_u64(name)?); + + if !self.get_conn().conversations.contains(&conv_id) { + self.forbidden("You are not currently registered to this conversation!".to_string())?; + } + + Ok(conv_id) + } } impl BaseRequestHandler for UserWsRequestHandler { diff --git a/src/helpers/events_helper.rs b/src/helpers/events_helper.rs index b183c92..721f753 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), + /// User is writing a message in a conversation + UserIsWritingMessageInConversation(&'a UserID, ConvID), + /// Removed a user from a conversation RemovedUserFromConversation(&'a UserID, ConvID), diff --git a/src/user_ws_routes.rs b/src/user_ws_routes.rs index f1fa82c..f1d2170 100644 --- a/src/user_ws_routes.rs +++ b/src/user_ws_routes.rs @@ -2,7 +2,7 @@ //! //! @author Pierre Hubert -use crate::controllers::{calls_controller, likes_controller, user_ws_actions}; +use crate::controllers::{calls_controller, conversations_controller, likes_controller, user_ws_actions}; use crate::data::error::Res; use crate::data::user_ws_request_handler::UserWsRequestHandler; @@ -37,6 +37,9 @@ pub fn get_user_ws_routes() -> Vec { // Likes controller UserWsRoute::new("likes/update", likes_controller::update), + // Conversations controller + UserWsRoute::new("conversations/is_writing", conversations_controller::member_is_writing), + // Calls controller UserWsRoute::new("calls/config", calls_controller::get_config), UserWsRoute::new("calls/join", calls_controller::join_call),