mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2024-11-26 07:19:22 +00:00
Send message through WebSocket fro new conversation messages
This commit is contained in:
parent
cf6063feef
commit
de2448d496
@ -12,12 +12,17 @@ use crate::api_data::res_count_unread_conversations::ResultCountUnreadConversati
|
|||||||
use crate::api_data::res_create_conversation::ResCreateConversation;
|
use crate::api_data::res_create_conversation::ResCreateConversation;
|
||||||
use crate::api_data::res_find_private_conversations::ResFindPrivateConversations;
|
use crate::api_data::res_find_private_conversations::ResFindPrivateConversations;
|
||||||
use crate::controllers::routes::RequestResult;
|
use crate::controllers::routes::RequestResult;
|
||||||
|
use crate::controllers::user_ws_controller;
|
||||||
|
use crate::controllers::user_ws_controller::WsConnection;
|
||||||
use crate::data::base_request_handler::BaseRequestHandler;
|
use crate::data::base_request_handler::BaseRequestHandler;
|
||||||
|
use crate::data::error::Res;
|
||||||
use crate::data::http_request_handler::HttpRequestHandler;
|
use crate::data::http_request_handler::HttpRequestHandler;
|
||||||
use crate::data::new_conversation::NewConversation;
|
use crate::data::new_conversation::NewConversation;
|
||||||
use crate::data::new_conversation_message::NewConversationMessage;
|
use crate::data::new_conversation_message::NewConversationMessage;
|
||||||
use crate::data::user::UserID;
|
use crate::data::user::UserID;
|
||||||
use crate::helpers::{conversations_helper, user_helper};
|
use crate::data::user_ws_message::UserWsMessage;
|
||||||
|
use crate::helpers::{conversations_helper, events_helper, user_helper};
|
||||||
|
use crate::helpers::events_helper::Event;
|
||||||
use crate::utils::string_utils::remove_html_nodes;
|
use crate::utils::string_utils::remove_html_nodes;
|
||||||
|
|
||||||
/// Create a new conversation
|
/// Create a new conversation
|
||||||
@ -334,4 +339,23 @@ pub fn delete_message(r: &mut HttpRequestHandler) -> RequestResult {
|
|||||||
conversations_helper::delete_message_by_id(msg_id)?;
|
conversations_helper::delete_message_by_id(msg_id)?;
|
||||||
|
|
||||||
r.success("The message has been successfully deleted!")
|
r.success("The message has been successfully deleted!")
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Events handler
|
||||||
|
pub fn handle_event(e: &events_helper::Event) -> Res {
|
||||||
|
match e {
|
||||||
|
Event::NewConversationMessage(msg) => {
|
||||||
|
user_ws_controller::send_message_to_specific_connections(
|
||||||
|
|f| f.conversations.contains(&msg.conv_id),
|
||||||
|
|_| UserWsMessage::no_id_message("new_conv_message", ConversationMessageAPI::new(msg)),
|
||||||
|
Some(|conn: &WsConnection| conversations_helper::mark_user_seen(msg.conv_id, &conn.user_id)),
|
||||||
|
)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
Event::UpdatedConversationMessage(msg) => {}
|
||||||
|
Event::DeleteConversationMessage(msg) => {}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
@ -483,6 +483,31 @@ pub fn send_message_to_users(msg: &UserWsMessage, users: &Vec<UserID>) -> Res {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Send a message to specific users
|
||||||
|
pub fn send_message_to_specific_connections<F, M, A>(filter: F, msg_generator: M, after_send: Option<A>) -> Res
|
||||||
|
where F: Fn(&WsConnection) -> bool,
|
||||||
|
M: Fn(&WsConnection) -> Res<UserWsMessage>,
|
||||||
|
A: Fn(&WsConnection) -> Res
|
||||||
|
{
|
||||||
|
let connections = get_ws_connections_list()
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.iter()
|
||||||
|
.filter(|f| filter(f))
|
||||||
|
.map(|f| f.clone())
|
||||||
|
.collect::<Vec<WsConnection>>();
|
||||||
|
|
||||||
|
for con in connections {
|
||||||
|
send_message(con.session.clone(), &msg_generator(&con)?)?;
|
||||||
|
|
||||||
|
if let Some(cb) = &after_send {
|
||||||
|
cb(&con)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
/// Check out whether user is connected or not
|
/// Check out whether user is connected or not
|
||||||
pub fn is_user_connected(user_id: &UserID) -> bool {
|
pub fn is_user_connected(user_id: &UserID) -> bool {
|
||||||
get_ws_connections_list().lock().unwrap().iter().any(|c| &c.user_id == user_id)
|
get_ws_connections_list().lock().unwrap().iter().any(|c| &c.user_id == user_id)
|
||||||
|
@ -4,9 +4,22 @@
|
|||||||
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
use crate::data::error::Res;
|
||||||
|
|
||||||
#[derive(Clone, Serialize, Deserialize)]
|
#[derive(Clone, Serialize, Deserialize)]
|
||||||
pub struct UserWsMessage {
|
pub struct UserWsMessage {
|
||||||
pub id: Option<String>,
|
pub id: Option<String>,
|
||||||
pub title: String,
|
pub title: String,
|
||||||
pub data: serde_json::Value,
|
pub data: serde_json::Value,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl UserWsMessage {
|
||||||
|
/// Construct a new WebSocket message with no ID
|
||||||
|
pub fn no_id_message<T: Serialize>(title: &str, data: T) -> Res<Self> {
|
||||||
|
Ok(UserWsMessage {
|
||||||
|
id: None,
|
||||||
|
title: title.to_string(),
|
||||||
|
data: serde_json::to_value(data)?,
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
@ -10,8 +10,9 @@ use crate::data::new_conversation::NewConversation;
|
|||||||
use crate::data::new_conversation_message::NewConversationMessage;
|
use crate::data::new_conversation_message::NewConversationMessage;
|
||||||
use crate::data::unread_conversation::UnreadConversation;
|
use crate::data::unread_conversation::UnreadConversation;
|
||||||
use crate::data::user::UserID;
|
use crate::data::user::UserID;
|
||||||
use crate::helpers::database;
|
use crate::helpers::{database, events_helper};
|
||||||
use crate::helpers::database::InsertQuery;
|
use crate::helpers::database::InsertQuery;
|
||||||
|
use crate::helpers::events_helper::Event;
|
||||||
use crate::utils::date_utils::time;
|
use crate::utils::date_utils::time;
|
||||||
use crate::utils::user_data_utils::user_data_path;
|
use crate::utils::user_data_utils::user_data_path;
|
||||||
|
|
||||||
@ -297,13 +298,13 @@ pub fn send_message(msg: &NewConversationMessage) -> ResultBoxError<()> {
|
|||||||
let t = time();
|
let t = time();
|
||||||
|
|
||||||
// Insert the message in the database
|
// Insert the message in the database
|
||||||
database::InsertQuery::new(CONV_MESSAGES_TABLE)
|
let msg_id = database::InsertQuery::new(CONV_MESSAGES_TABLE)
|
||||||
.add_u64("conv_id", msg.conv_id)
|
.add_u64("conv_id", msg.conv_id)
|
||||||
.add_user_id("user_id", &msg.user_id)
|
.add_user_id("user_id", &msg.user_id)
|
||||||
.add_u64("time_insert", t)
|
.add_u64("time_insert", t)
|
||||||
.add_str("message", msg.message.as_str())
|
.add_str("message", msg.message.as_str())
|
||||||
.add_opt_str("image_path", msg.image_path.as_ref())
|
.add_opt_str("image_path", msg.image_path.as_ref())
|
||||||
.insert()?;
|
.insert_expect_result()?;
|
||||||
|
|
||||||
// Update the last activity of the conversation
|
// Update the last activity of the conversation
|
||||||
database::UpdateInfo::new(CONV_LIST_TABLE)
|
database::UpdateInfo::new(CONV_LIST_TABLE)
|
||||||
@ -323,7 +324,9 @@ pub fn send_message(msg: &NewConversationMessage) -> ResultBoxError<()> {
|
|||||||
.exec()?;
|
.exec()?;
|
||||||
|
|
||||||
// TODO : send an event (updated_number_unread_conversations)
|
// TODO : send an event (updated_number_unread_conversations)
|
||||||
// TODO : send an event (sent_conversation_message)
|
|
||||||
|
// Send an event (sent_conversation_message)
|
||||||
|
events_helper::propagate_event(&Event::NewConversationMessage(&get_single_message(msg_id)?))?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -335,7 +338,8 @@ pub fn update_message_content(msg_id: u64, new_content: &str) -> ResultBoxError<
|
|||||||
.set_str("message", new_content)
|
.set_str("message", new_content)
|
||||||
.exec()?;
|
.exec()?;
|
||||||
|
|
||||||
// TODO : send an event (conv_message_updated)
|
// Send an event (conv_message_updated)
|
||||||
|
events_helper::propagate_event(&Event::UpdatedConversationMessage(&get_single_message(msg_id)?))?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@ -355,7 +359,8 @@ pub fn delete_message(msg: &ConversationMessage) -> ResultBoxError<()> {
|
|||||||
.cond_u64("ID", msg.id)
|
.cond_u64("ID", msg.id)
|
||||||
.exec()?;
|
.exec()?;
|
||||||
|
|
||||||
// TODO : send en event (conv_message_deleted)
|
// Send en event (conv_message_deleted)
|
||||||
|
events_helper::propagate_event(&Event::DeleteConversationMessage(msg))?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
30
src/helpers/events_helper.rs
Normal file
30
src/helpers/events_helper.rs
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
//! # Events helper
|
||||||
|
//!
|
||||||
|
//! @author Pierre Hubert
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
use crate::data::error::Res;
|
||||||
|
use crate::data::conversation_message::ConversationMessage;
|
||||||
|
use crate::controllers::conversations_controller;
|
||||||
|
|
||||||
|
pub enum Event<'a> {
|
||||||
|
|
||||||
|
/// Created a new conversation message
|
||||||
|
NewConversationMessage(&'a ConversationMessage),
|
||||||
|
|
||||||
|
/// Updated conversation message
|
||||||
|
UpdatedConversationMessage(&'a ConversationMessage),
|
||||||
|
|
||||||
|
/// Deleted a conversation message
|
||||||
|
DeleteConversationMessage(&'a ConversationMessage),
|
||||||
|
|
||||||
|
/// No event
|
||||||
|
None,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Propagate an event through the different components of the application
|
||||||
|
pub fn propagate_event(e: &Event) -> Res {
|
||||||
|
conversations_controller::handle_event(e)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
@ -16,4 +16,5 @@ pub mod survey_helper;
|
|||||||
pub mod comments_helper;
|
pub mod comments_helper;
|
||||||
pub mod notifications_helper;
|
pub mod notifications_helper;
|
||||||
pub mod webapp_helper;
|
pub mod webapp_helper;
|
||||||
pub mod requests_limit_helper;
|
pub mod requests_limit_helper;
|
||||||
|
pub mod events_helper;
|
Loading…
Reference in New Issue
Block a user