1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-22 13:29:21 +00:00

Create first server message for conversations

This commit is contained in:
Pierre HUBERT 2021-03-07 15:44:52 +01:00
parent 466cf1903f
commit 8a33980a2d
4 changed files with 36 additions and 9 deletions

View File

@ -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)) => {

View File

@ -12,5 +12,18 @@ pub struct NewConversationMessage {
pub conv_id: ConvID,
pub message: Option<String>,
pub file: Option<ConversationMessageFile>,
pub server_message: Option<ConversationServerMessageType>
pub server_message: Option<ConversationServerMessageType>,
}
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),
}
}
}

View File

@ -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<ConvID> {
}
/// 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 {

View File

@ -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<UserID>) -> 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<GroupID>) -> InsertQuery {
self.add_opt_u64(key, value.map(|u| u.id()))
}