1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-06-21 08:55:16 +00:00

Can send new messages

This commit is contained in:
2020-06-22 12:19:13 +02:00
parent e915e4b7d0
commit 0a21531cb4
6 changed files with 109 additions and 7 deletions

View File

@ -7,6 +7,7 @@ use crate::data::conversation::Conversation;
use crate::data::conversation_message::ConversationMessage;
use crate::data::error::{ExecError, ResultBoxError};
use crate::data::new_conversation::NewConversation;
use crate::data::new_conversation_message::NewConversationMessage;
use crate::data::user::UserID;
use crate::helpers::database;
use crate::helpers::database::InsertQuery;
@ -223,6 +224,39 @@ pub fn get_new_messages(conv_id: u64, last_message_id: u64) -> ResultBoxError<Ve
.exec(db_to_conversation_message)
}
/// Send a new conversation message
pub fn send_message(msg: &NewConversationMessage) -> ResultBoxError<()> {
let t = time();
// Insert the message in the database
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()?;
// Update the last activity of the conversation
database::UpdateInfo::new(CONV_LIST_TABLE)
.cond_u64("id", msg.conv_id)
.set_u64("last_active", t)
.exec()?;
// Mark all the users of the conversation as unread
database::UpdateInfo::new(CONV_USERS_TABLE)
.cond_u64("conv_id", msg.conv_id)
.cond_legacy_bool("saw_last_message", true)
.custom_where("user_id != ?")
.add_custom_where_arg_u64(msg.user_id as u64)
.set_legacy_bool("saw_last_message", false)
.exec()?;
Ok(())
}
/// Indicate that a user has seen the last messages of a conversation
pub fn mark_user_seen(conv_id: u64, user_id: UserID) -> ResultBoxError<()> {
database::UpdateInfo::new(CONV_USERS_TABLE)

View File

@ -445,6 +445,12 @@ impl InsertQuery {
self
}
/// Add an optional string. If None, an empty string will be inserted
pub fn add_opt_str(mut self, key: &str, value: Option<&String>) -> InsertQuery {
self.values.insert(key.to_string(), Value::from(value.unwrap_or(&String::new())));
self
}
/// Add an integer
pub fn add_i64(mut self, key: &str, value: i64) -> InsertQuery {
self.values.insert(key.to_string(), Value::from(value));
@ -583,6 +589,8 @@ pub fn delete(query: DeleteQuery) -> ResultBoxError<()> {
pub struct UpdateInfo {
table: String,
cond: HashMap<String, Value>,
custom_where: Option<String>,
custom_where_args: Vec<mysql::Value>,
set: HashMap<String, Value>,
}
@ -592,6 +600,8 @@ impl UpdateInfo {
UpdateInfo {
table: table.to_string(),
cond: HashMap::new(),
custom_where: None,
custom_where_args: vec![],
set: HashMap::new(),
}
}
@ -619,6 +629,18 @@ impl UpdateInfo {
self
}
/// Add custom WHERE clause
pub fn custom_where(mut self, query: &str) -> UpdateInfo {
self.custom_where = Some(query.to_string());
self
}
/// Add custom WHERE argument
pub fn add_custom_where_arg_u64(mut self, arg: u64) -> UpdateInfo {
self.custom_where_args.push(mysql::Value::UInt(arg));
self
}
/// Set an new optional string
///
/// None => Empty string
@ -639,6 +661,12 @@ impl UpdateInfo {
self
}
/// Set a new unsigned number
pub fn set_u64(mut self, name: &str, val: u64) -> UpdateInfo {
self.set.insert(name.to_string(), Value::UInt(val));
self
}
/// Execute the update
pub fn exec(self) -> ResultBoxError<()> {
update(self)
@ -668,13 +696,20 @@ pub fn update(u: UpdateInfo) -> ResultBoxError<()> {
u.set.values().into_iter().for_each(|f| values.push(f));
// Prepare conditions
let conditions = u.cond.keys()
let mut conditions = u.cond.keys()
.into_iter()
.map(|f| format!("{} = ?", f))
.collect::<Vec<String>>()
.join(" AND ");
u.cond.values().into_iter().for_each(|f| values.push(f));
// Additional conditions
if let Some(custom_where) = u.custom_where {
conditions = format!("{} AND ({})", conditions, custom_where);
u.custom_where_args.iter().for_each(|f| values.push(f))
}
query_sql = format!("{} {} WHERE {}", query_sql, updates, conditions);
get_connection()?.exec_drop(