mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2025-01-30 22:13:01 +00:00
48 lines
1.7 KiB
Rust
48 lines
1.7 KiB
Rust
|
//! # 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(())
|
||
|
}
|