2020-06-04 13:36:57 +02:00
|
|
|
//! # Conversations helper
|
|
|
|
//!
|
|
|
|
//! @author Pierre Hubert
|
|
|
|
|
|
|
|
use crate::data::new_conversation::NewConversation;
|
|
|
|
use crate::data::error::{ResultBoxError, ExecError};
|
2020-06-04 19:01:35 +02:00
|
|
|
use crate::helpers::database::{InsertQuery};
|
2020-06-04 13:36:57 +02:00
|
|
|
use crate::constants::database_tables_names::{CONV_LIST_TABLE, CONV_USERS_TABLE};
|
|
|
|
use crate::utils::date_utils::time;
|
|
|
|
use crate::data::user::UserID;
|
2020-06-04 19:01:35 +02:00
|
|
|
use crate::data::conversation::Conversation;
|
|
|
|
use crate::helpers::database;
|
2020-06-04 13:36:57 +02:00
|
|
|
|
|
|
|
/// 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(())
|
2020-06-04 19:01:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the list of conversations of a specific user
|
|
|
|
pub fn get_list_user(user_id: UserID) -> ResultBoxError<Vec<Conversation>> {
|
|
|
|
database::QueryInfo::new(CONV_LIST_TABLE)
|
|
|
|
.alias("l")
|
|
|
|
|
|
|
|
// Join with conversation members table
|
|
|
|
.join(CONV_USERS_TABLE, "u", "l.id = u.conv_id")
|
|
|
|
|
|
|
|
// Specify selected fields
|
|
|
|
.add_field("*")
|
|
|
|
.add_field("l.id as id")
|
|
|
|
.add_field("l.user_id as owner_id")
|
|
|
|
|
|
|
|
// Filter query
|
|
|
|
.cond_user_id("u.user_id", user_id)
|
|
|
|
|
|
|
|
// Sort results
|
|
|
|
.set_order("l.last_active DESC")
|
|
|
|
|
|
|
|
// Execute query
|
|
|
|
.exec(db_to_conversation_info)
|
|
|
|
}
|
|
|
|
|
2020-06-12 09:20:32 +02:00
|
|
|
/// Get information about a single conversation
|
|
|
|
pub fn get_single(conv_id: u64, user_id: UserID) -> ResultBoxError<Conversation> {
|
|
|
|
// Tables
|
|
|
|
database::QueryInfo::new(CONV_LIST_TABLE)
|
|
|
|
.alias("l")
|
|
|
|
.join(CONV_USERS_TABLE, "u", "l.id = u.conv_id")
|
|
|
|
|
|
|
|
// Fields
|
|
|
|
.add_field("*")
|
|
|
|
.add_field("l.id as id")
|
|
|
|
.add_field("l.user_id as owner_id")
|
|
|
|
|
|
|
|
// Conditions
|
|
|
|
.cond_u64("l.id", conv_id)
|
|
|
|
.cond_user_id("u.user_id", user_id)
|
|
|
|
|
|
|
|
.query_row(db_to_conversation_info)
|
|
|
|
}
|
|
|
|
|
2020-06-04 19:01:35 +02:00
|
|
|
/// Get the list of members of a conversation
|
|
|
|
pub fn get_list_members(conv_id: u64) -> ResultBoxError<Vec<UserID>> {
|
|
|
|
database::QueryInfo::new(CONV_USERS_TABLE)
|
|
|
|
.cond_u64("conv_id", conv_id)
|
|
|
|
.add_field("user_id")
|
2020-06-12 09:04:43 +02:00
|
|
|
.exec(|res| res.get_user_id("user_id"))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Check if a user belongs to a conversation or not
|
|
|
|
pub fn does_user_belongs_to(user_id: UserID, conv_id: u64) -> ResultBoxError<bool> {
|
|
|
|
Ok(database::QueryInfo::new(CONV_USERS_TABLE)
|
|
|
|
.cond_u64("conv_id", conv_id)
|
|
|
|
.cond_user_id("user_id", user_id)
|
|
|
|
.exec_count()? > 0)
|
2020-06-04 19:01:35 +02:00
|
|
|
}
|
|
|
|
|
2020-06-12 11:26:12 +02:00
|
|
|
/// Check out wheter a user is the moderator of a conversation or not
|
|
|
|
pub fn is_user_moderator(user_id: UserID, conv_id: u64) -> ResultBoxError<bool> {
|
|
|
|
Ok(database::QueryInfo::new(CONV_LIST_TABLE)
|
|
|
|
.cond_u64("id", conv_id)
|
|
|
|
.cond_user_id("user_id", user_id)
|
|
|
|
.exec_count()? > 0)
|
|
|
|
}
|
|
|
|
|
2020-06-15 08:23:23 +02:00
|
|
|
/// Set whether a user is following a conversation or not
|
|
|
|
pub fn set_following(user_id: UserID, conv_id: u64, following: bool) -> ResultBoxError<()> {
|
|
|
|
database::UpdateInfo::new(CONV_USERS_TABLE)
|
|
|
|
.cond_u64("conv_id", conv_id)
|
|
|
|
.cond_user_id("user_id", user_id)
|
|
|
|
.set_legacy_bool("following", following)
|
|
|
|
.exec()
|
|
|
|
}
|
|
|
|
|
2020-06-04 19:01:35 +02:00
|
|
|
/// Turn a database entry into a ConversationInfo object
|
|
|
|
fn db_to_conversation_info(row: &database::RowResult) -> ResultBoxError<Conversation> {
|
|
|
|
let conv_id = row.get_u64("id")?;
|
|
|
|
Ok(Conversation {
|
|
|
|
id: conv_id,
|
|
|
|
owner_id: row.get_user_id("owner_id")?,
|
|
|
|
name: row.get_optional_str("name")?,
|
|
|
|
members: get_list_members(conv_id)?,
|
|
|
|
can_everyone_add_members: row.get_legacy_bool("can_everyone_add_members")?,
|
|
|
|
last_active: row.get_u64("last_active")?,
|
|
|
|
time_create: row.get_u64("time_add")?,
|
|
|
|
following: row.get_legacy_bool("following")?,
|
|
|
|
saw_last_message: row.get_legacy_bool("saw_last_message")?,
|
|
|
|
})
|
2020-06-04 13:36:57 +02:00
|
|
|
}
|