mirror of
				https://gitlab.com/comunic/comunicapiv3
				synced 2025-11-04 01:24:04 +00:00 
			
		
		
		
	Send message through WebSocket fro new conversation messages
This commit is contained in:
		@@ -10,8 +10,9 @@ use crate::data::new_conversation::NewConversation;
 | 
			
		||||
use crate::data::new_conversation_message::NewConversationMessage;
 | 
			
		||||
use crate::data::unread_conversation::UnreadConversation;
 | 
			
		||||
use crate::data::user::UserID;
 | 
			
		||||
use crate::helpers::database;
 | 
			
		||||
use crate::helpers::{database, events_helper};
 | 
			
		||||
use crate::helpers::database::InsertQuery;
 | 
			
		||||
use crate::helpers::events_helper::Event;
 | 
			
		||||
use crate::utils::date_utils::time;
 | 
			
		||||
use crate::utils::user_data_utils::user_data_path;
 | 
			
		||||
 | 
			
		||||
@@ -297,13 +298,13 @@ pub fn send_message(msg: &NewConversationMessage) -> ResultBoxError<()> {
 | 
			
		||||
    let t = time();
 | 
			
		||||
 | 
			
		||||
    // 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_user_id("user_id", &msg.user_id)
 | 
			
		||||
        .add_u64("time_insert", t)
 | 
			
		||||
        .add_str("message", msg.message.as_str())
 | 
			
		||||
        .add_opt_str("image_path", msg.image_path.as_ref())
 | 
			
		||||
        .insert()?;
 | 
			
		||||
        .insert_expect_result()?;
 | 
			
		||||
 | 
			
		||||
    // Update the last activity of the conversation
 | 
			
		||||
    database::UpdateInfo::new(CONV_LIST_TABLE)
 | 
			
		||||
@@ -323,7 +324,9 @@ pub fn send_message(msg: &NewConversationMessage) -> ResultBoxError<()> {
 | 
			
		||||
        .exec()?;
 | 
			
		||||
 | 
			
		||||
    // 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(())
 | 
			
		||||
}
 | 
			
		||||
@@ -335,7 +338,8 @@ pub fn update_message_content(msg_id: u64, new_content: &str) -> ResultBoxError<
 | 
			
		||||
        .set_str("message", new_content)
 | 
			
		||||
        .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(())
 | 
			
		||||
}
 | 
			
		||||
@@ -355,7 +359,8 @@ pub fn delete_message(msg: &ConversationMessage) -> ResultBoxError<()> {
 | 
			
		||||
        .cond_u64("ID", msg.id)
 | 
			
		||||
        .exec()?;
 | 
			
		||||
 | 
			
		||||
    // TODO : send en event (conv_message_deleted)
 | 
			
		||||
    // Send en event (conv_message_deleted)
 | 
			
		||||
    events_helper::propagate_event(&Event::DeleteConversationMessage(msg))?;
 | 
			
		||||
 | 
			
		||||
    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 notifications_helper;
 | 
			
		||||
pub mod webapp_helper;
 | 
			
		||||
pub mod requests_limit_helper;
 | 
			
		||||
pub mod requests_limit_helper;
 | 
			
		||||
pub mod events_helper;
 | 
			
		||||
		Reference in New Issue
	
	Block a user