mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2024-11-26 07:19:22 +00:00
Create the conversation
This commit is contained in:
parent
8465f0da65
commit
435ba32653
@ -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
|
||||
|
@ -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")
|
||||
}
|
48
src/helpers/conversations_helper.rs
Normal file
48
src/helpers/conversations_helper.rs
Normal file
@ -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<u64> {
|
||||
// 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(())
|
||||
}
|
@ -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<Option<u64>> {
|
||||
insert(self)
|
||||
}
|
||||
}
|
||||
|
||||
/// Insert a new entry into the database
|
||||
pub fn insert(query: InsertQuery) -> ResultBoxError<()> {
|
||||
pub fn insert(query: InsertQuery) -> ResultBoxError<Option<u64>> {
|
||||
|
||||
// Collect keys
|
||||
let keys = query.values
|
||||
@ -378,12 +404,13 @@ pub fn insert(query: InsertQuery) -> ResultBoxError<()> {
|
||||
keys.iter().map(|_| "?").collect::<Vec<&str>>().join(", ")
|
||||
);
|
||||
|
||||
get_connection()?.exec_drop(
|
||||
let mut con = get_connection()?;
|
||||
let res = con.exec_iter(
|
||||
query_sql,
|
||||
query.values.values().collect::<Vec<&mysql::Value>>(),
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
Ok(res.last_insert_id())
|
||||
}
|
||||
|
||||
|
||||
|
@ -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;
|
||||
pub mod groups_helper;
|
||||
pub mod conversations_helper;
|
16
src/utils/date_utils.rs
Normal file
16
src/utils/date_utils.rs
Normal file
@ -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()
|
||||
}
|
@ -4,4 +4,5 @@
|
||||
|
||||
pub mod crypt_utils;
|
||||
pub mod user_data_utils;
|
||||
pub mod virtual_directories_utils;
|
||||
pub mod virtual_directories_utils;
|
||||
pub mod date_utils;
|
Loading…
Reference in New Issue
Block a user