From 435ba326533a6bfb13bc504e5ea14da57c8c1157 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 4 Jun 2020 13:36:57 +0200 Subject: [PATCH] Create the conversation --- src/constants.rs | 5 +++ src/controllers/conversations_controller.rs | 7 ++- src/helpers/conversations_helper.rs | 48 +++++++++++++++++++++ src/helpers/database.rs | 33 ++++++++++++-- src/helpers/mod.rs | 3 +- src/utils/date_utils.rs | 16 +++++++ src/utils/mod.rs | 3 +- 7 files changed, 108 insertions(+), 7 deletions(-) create mode 100644 src/helpers/conversations_helper.rs create mode 100644 src/utils/date_utils.rs diff --git a/src/constants.rs b/src/constants.rs index 5e3d93a..7b26b0f 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -26,6 +26,11 @@ pub mod database_tables_names { /// Groups list table pub const GROUPS_LIST_TABLE: &str = "comunic_groups"; + + /// Conversations tables + pub const CONV_LIST_TABLE: &str = "comunic_conversations_list"; + pub const CONV_USERS_TABLE: &str = "comunic_conversations_users"; + pub const CONV_MESSAGES_TABLE: &str = "comunic_conversations_messages"; } /// The account image to show for user who do not have any diff --git a/src/controllers/conversations_controller.rs b/src/controllers/conversations_controller.rs index a850f4c..ce095da 100644 --- a/src/controllers/conversations_controller.rs +++ b/src/controllers/conversations_controller.rs @@ -4,7 +4,7 @@ use crate::data::http_request_handler::HttpRequestHandler; use crate::controllers::routes::RequestResult; -use crate::helpers::user_helper; +use crate::helpers::{user_helper, conversations_helper}; use crate::data::new_conversation::NewConversation; /// Create a new conversation @@ -39,7 +39,10 @@ pub fn create(r: &mut HttpRequestHandler) -> RequestResult { can_everyone_add_members: r.post_bool_opt("canEveryoneAddMembers", true) }; - println!("Conversation to create: {:#?}", conv); + // TODO : adapt for API + println!("Conversation to create: {:#?}", conv); + let conv_id = conversations_helper::create(&conv)?; + println!("conv: {}", conv_id); r.success("create") } \ No newline at end of file diff --git a/src/helpers/conversations_helper.rs b/src/helpers/conversations_helper.rs new file mode 100644 index 0000000..96c3929 --- /dev/null +++ b/src/helpers/conversations_helper.rs @@ -0,0 +1,48 @@ +//! # Conversations helper +//! +//! @author Pierre Hubert + +use crate::data::new_conversation::NewConversation; +use crate::data::error::{ResultBoxError, ExecError}; +use crate::helpers::database::InsertQuery; +use crate::constants::database_tables_names::{CONV_LIST_TABLE, CONV_USERS_TABLE}; +use crate::utils::date_utils::time; +use crate::data::user::UserID; + +/// Create a new conversation. This method returns the ID of the created conversation +pub fn create(conv: &NewConversation) -> ResultBoxError { + // Create the conversation in the main table + let conv_id = InsertQuery::new(CONV_LIST_TABLE) + .add_user_id("user_id", conv.owner_id) + .add_str("name", conv.name.clone().unwrap_or(String::new()).as_str()) + .add_u64("last_active", time()) + .add_u64("creation_time", time()) + .add_legacy_bool("can_everyone_add_members", conv.can_everyone_add_members) + .insert()?.ok_or(ExecError::new("missing result conv id!"))?; + + // Add the members to the conversation + for member in &conv.members { + // Check following status of the member + let mut follow = true; + if member.eq(&conv.owner_id) { + follow = conv.owner_following; + } + + add_member(conv_id, member.clone(), follow)?; + } + + Ok(conv_id) +} + +/// Add a member to a conversation +pub fn add_member(conv_id: u64, user_id: UserID, following: bool) -> ResultBoxError<()> { + InsertQuery::new(CONV_USERS_TABLE) + .add_u64("conv_id", conv_id) + .add_user_id("user_id", user_id) + .add_u64("time_add", time()) + .add_legacy_bool("following", following) + .add_legacy_bool("saw_last_message", true) + .insert()?; + + Ok(()) +} \ No newline at end of file diff --git a/src/helpers/database.rs b/src/helpers/database.rs index 1e62c9e..50c99b8 100644 --- a/src/helpers/database.rs +++ b/src/helpers/database.rs @@ -356,14 +356,40 @@ impl InsertQuery { self } + pub fn add_u64(mut self, key: &str, value: u64) -> InsertQuery { + self.values.insert(key.to_string(), Value::from(value)); + self + } + pub fn add_u32(mut self, key: &str, value: u32) -> InsertQuery { self.values.insert(key.to_string(), Value::from(value)); self } + + pub fn add_user_id(mut self, key: &str, value: UserID) -> InsertQuery { + self.values.insert(key.to_string(), Value::from(value)); + self + } + + /// Legacy database boolean (1 = true / 0 = false) + pub fn add_legacy_bool(mut self, key: &str, value: bool) -> InsertQuery { + let num = match value { + true => 1, + false => 0 + }; + + self.values.insert(key.to_string(), Value::from(num)); + self + } + + /// Process insert + pub fn insert(self) -> ResultBoxError> { + insert(self) + } } /// Insert a new entry into the database -pub fn insert(query: InsertQuery) -> ResultBoxError<()> { +pub fn insert(query: InsertQuery) -> ResultBoxError> { // Collect keys let keys = query.values @@ -378,12 +404,13 @@ pub fn insert(query: InsertQuery) -> ResultBoxError<()> { keys.iter().map(|_| "?").collect::>().join(", ") ); - get_connection()?.exec_drop( + let mut con = get_connection()?; + let res = con.exec_iter( query_sql, query.values.values().collect::>(), )?; - Ok(()) + Ok(res.last_insert_id()) } diff --git a/src/helpers/mod.rs b/src/helpers/mod.rs index 309f2d0..673057f 100644 --- a/src/helpers/mod.rs +++ b/src/helpers/mod.rs @@ -7,4 +7,5 @@ pub mod friends_helper; pub mod custom_emojies_helper; pub mod background_image_helper; pub mod likes_helper; -pub mod groups_helper; \ No newline at end of file +pub mod groups_helper; +pub mod conversations_helper; \ No newline at end of file diff --git a/src/utils/date_utils.rs b/src/utils/date_utils.rs new file mode 100644 index 0000000..9b92f40 --- /dev/null +++ b/src/utils/date_utils.rs @@ -0,0 +1,16 @@ +//! # Date utilities +//! +//! @author Pierre Hubert + +use std::time::{SystemTime, UNIX_EPOCH}; + +/// Get the current time since epoch +/// +/// ``` +/// use comunic_server::utils::date_utils::time; +/// +/// let time = time(); +/// ``` +pub fn time() -> u64 { + SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs() +} \ No newline at end of file diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 3156a82..a4298ea 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -4,4 +4,5 @@ pub mod crypt_utils; pub mod user_data_utils; -pub mod virtual_directories_utils; \ No newline at end of file +pub mod virtual_directories_utils; +pub mod date_utils; \ No newline at end of file