diff --git a/src/controllers/conversations_controller.rs b/src/controllers/conversations_controller.rs index 49db413..8b532c9 100644 --- a/src/controllers/conversations_controller.rs +++ b/src/controllers/conversations_controller.rs @@ -49,7 +49,7 @@ pub fn create(r: &mut HttpRequestHandler) -> RequestResult { can_everyone_add_members: r.post_bool_opt("canEveryoneAddMembers", true), color: r.post_color_opt("color")?, group_id: None, - logo: None,// TODO : add support for logo + logo: None, }; // Create the conversation @@ -285,8 +285,6 @@ pub fn get_older_messages(r: &mut HttpRequestHandler) -> RequestResult { pub fn send_message(r: &mut HttpRequestHandler) -> RequestResult { let conv = r.post_conv("conversationID")?; - // TODO : add support for other files type - // Get associated file let file = match r.post_parameter_opt("file") { Some(RequestValue::File(file)) => { diff --git a/src/data/new_conversation_message.rs b/src/data/new_conversation_message.rs index 0a54338..0f2c2ea 100644 --- a/src/data/new_conversation_message.rs +++ b/src/data/new_conversation_message.rs @@ -12,5 +12,18 @@ pub struct NewConversationMessage { pub conv_id: ConvID, pub message: Option, pub file: Option, - pub server_message: Option + pub server_message: Option, +} + +impl NewConversationMessage { + /// Create a new server message + pub fn new_server_message(conv_id: ConvID, message: ConversationServerMessageType) -> Self { + Self { + user_id: None, + conv_id, + message: None, + file: None, + server_message: Some(message), + } + } } \ No newline at end of file diff --git a/src/helpers/conversations_helper.rs b/src/helpers/conversations_helper.rs index 30d3626..c07942d 100644 --- a/src/helpers/conversations_helper.rs +++ b/src/helpers/conversations_helper.rs @@ -4,7 +4,7 @@ use crate::constants::database_tables_names::{CONV_LIST_TABLE, CONV_MEMBERS_TABLE, CONV_MESSAGES_TABLE}; use crate::data::conversation::{Conversation, ConversationMember, ConvID, NewConversationSettings}; -use crate::data::conversation_message::{ConversationMessage, ConversationMessageFile, ConversationServerMessageType}; +use crate::data::conversation_message::{ConversationMessage, ConversationMessageFile, ConversationServerMessageType, UserAddedAnotherUserToConversation}; use crate::data::error::{ExecError, Res, ResultBoxError}; use crate::data::new_conversation::NewConversation; use crate::data::new_conversation_message::NewConversationMessage; @@ -48,7 +48,7 @@ pub fn create(conv: &NewConversation) -> Res { } /// Add a member to a conversation -pub fn add_member(conv_id: ConvID, user_id: &UserID, following: bool, admin: bool, _adder: &UserID) -> Res { +pub fn add_member(conv_id: ConvID, user_id: &UserID, following: bool, admin: bool, adder: &UserID) -> Res { InsertQuery::new(CONV_MEMBERS_TABLE) .add_conv_id("conv_id", conv_id) .add_user_id("user_id", user_id) @@ -58,7 +58,18 @@ pub fn add_member(conv_id: ConvID, user_id: &UserID, following: bool, admin: boo .add_u64("last_message_seen", 0) .insert()?; - // TODO : create a message + // Create a message + if adder != user_id { + send_message( + &NewConversationMessage::new_server_message( + conv_id, + ConversationServerMessageType::UserAddedAnotherUserToConversation(UserAddedAnotherUserToConversation { + user_who_added: adder.clone(), + user_added: user_id.clone() + }) + ) + )?; + } Ok(()) } @@ -276,7 +287,7 @@ pub fn send_message(msg: &NewConversationMessage) -> ResultBoxError<()> { // Insert the message in the database let mut msg_request = database::InsertQuery::new(CONV_MESSAGES_TABLE) .add_conv_id("conv_id", msg.conv_id) - .add_u64("user_id", msg.user_id.as_ref().map(|u| u.id()).unwrap_or(0)) + .add_opt_user_id("user_id", msg.user_id.clone()) .add_u64("time_sent", t); if let Some(server_msg) = &msg.server_message { diff --git a/src/helpers/database.rs b/src/helpers/database.rs index c1fe502..de6a0e1 100644 --- a/src/helpers/database.rs +++ b/src/helpers/database.rs @@ -615,7 +615,12 @@ impl InsertQuery { self } - /// Add an optional number. If None, Null will be inserted + /// Add an optional user ID. If None, Null will be inserted + pub fn add_opt_user_id(self, key: &str, value: Option) -> InsertQuery { + self.add_opt_u64(key, value.map(|u| u.id())) + } + + /// Add an optional group ID. If None, Null will be inserted pub fn add_opt_group_id(self, key: &str, value: Option) -> InsertQuery { self.add_opt_u64(key, value.map(|u| u.id())) }