mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2025-09-19 11:28:46 +00:00
Continue refactoring
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
//! @author Pierre Hubert
|
||||
|
||||
use crate::constants::database_tables_names::{CONV_LIST_TABLE, CONV_MEMBERS_TABLE, CONV_MESSAGES_TABLE};
|
||||
use crate::data::conversation::Conversation;
|
||||
use crate::data::conversation::{Conversation, ConversationMember, ConversationMemberSetting, ConvID, NewConversationSettings};
|
||||
use crate::data::conversation_message::{ConversationMessage, ConversationMessageFile, ConversationServerMessageType};
|
||||
use crate::data::error::{ExecError, Res, ResultBoxError};
|
||||
use crate::data::new_conversation::NewConversation;
|
||||
@@ -11,57 +11,81 @@ use crate::data::new_conversation_message::NewConversationMessage;
|
||||
use crate::data::unread_conversation::UnreadConversation;
|
||||
use crate::data::user::{User, UserID};
|
||||
use crate::helpers::{database, events_helper};
|
||||
use crate::helpers::database::InsertQuery;
|
||||
use crate::helpers::database::{InsertQuery, UpdateInfo};
|
||||
use crate::helpers::events_helper::Event;
|
||||
use crate::utils::date_utils::time;
|
||||
use crate::utils::user_data_utils::delete_user_data_file_if_exists;
|
||||
|
||||
/// Create a new conversation. This method returns the ID of the created conversation
|
||||
pub fn create(conv: &NewConversation) -> ResultBoxError<u64> {
|
||||
pub fn create(conv: &NewConversation) -> Res<ConvID> {
|
||||
// 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("last_activity", time())
|
||||
.add_u64("creation_time", time())
|
||||
.add_opt_str("color", Option::from(&conv.color))
|
||||
.add_opt_str("background", Option::from(&conv.background))
|
||||
.add_legacy_bool("can_everyone_add_members", conv.can_everyone_add_members)
|
||||
.insert()?.ok_or(ExecError::new("missing result conv id!"))?;
|
||||
.add_opt_group_id("group_id", conv.group_id.clone())
|
||||
.insert()?
|
||||
.map(|i| ConvID::new(i))
|
||||
.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;
|
||||
let mut admin = false;
|
||||
if member.eq(&conv.owner_id) {
|
||||
follow = conv.owner_following;
|
||||
admin = true;
|
||||
}
|
||||
|
||||
add_member(conv_id, member, follow)?;
|
||||
add_member(conv_id, member, follow, admin)?;
|
||||
}
|
||||
|
||||
Ok(conv_id)
|
||||
}
|
||||
|
||||
/// Add a member to a conversation
|
||||
pub fn add_member(conv_id: u64, user_id: &UserID, following: bool) -> ResultBoxError<()> {
|
||||
pub fn add_member(conv_id: ConvID, user_id: &UserID, following: bool, admin: bool) -> Res {
|
||||
InsertQuery::new(CONV_MEMBERS_TABLE)
|
||||
.add_u64("conv_id", conv_id)
|
||||
.add_conv_id("conv_id", conv_id)
|
||||
.add_user_id("user_id", user_id)
|
||||
.add_u64("time_add", time())
|
||||
.add_u64("added_on", time())
|
||||
.add_legacy_bool("following", following)
|
||||
.add_legacy_bool("saw_last_message", true)
|
||||
.add_legacy_bool("is_admin", admin)
|
||||
.add_u64("last_message_seen", 0)
|
||||
.insert()?;
|
||||
|
||||
// TODO : create a message
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Remove a member from a conversation
|
||||
pub fn remove_member(conv_id: u64, user_id: &UserID) -> ResultBoxError<()> {
|
||||
database::DeleteQuery::new(CONV_MEMBERS_TABLE)
|
||||
.cond_u64("conv_id", conv_id)
|
||||
/// Update admin status of a member for a conversation
|
||||
pub fn set_admin(conv_id: &ConvID, user_id: &UserID, admin: bool) -> Res {
|
||||
UpdateInfo::new(CONV_MEMBERS_TABLE)
|
||||
.cond_user_id("user_id", user_id)
|
||||
.cond_conv_id("conv_id", conv_id.clone())
|
||||
.set_legacy_bool("is_admin", admin)
|
||||
.exec()
|
||||
}
|
||||
|
||||
/// Remove a member from a conversation
|
||||
pub fn remove_member(conv_id: ConvID, user_id: &UserID) -> ResultBoxError<()> {
|
||||
database::DeleteQuery::new(CONV_MEMBERS_TABLE)
|
||||
.cond_conv_id("conv_id", conv_id)
|
||||
.cond_user_id("user_id", user_id)
|
||||
.exec()?;
|
||||
|
||||
// TODO : create a message
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// 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)
|
||||
@@ -71,67 +95,46 @@ pub fn get_list_user(user_id: &UserID) -> ResultBoxError<Vec<Conversation>> {
|
||||
.join(CONV_MEMBERS_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")
|
||||
.add_field("l.*")
|
||||
|
||||
// Filter query
|
||||
.cond_user_id("u.user_id", user_id)
|
||||
|
||||
// Sort results
|
||||
.set_order("l.last_active DESC")
|
||||
.set_order("l.last_activity DESC")
|
||||
|
||||
// Execute query
|
||||
.exec(db_to_conversation_info)
|
||||
}
|
||||
|
||||
/// Get information about a single conversation
|
||||
pub fn get_single(conv_id: u64, user_id: &UserID) -> ResultBoxError<Conversation> {
|
||||
pub fn get_single(conv_id: ConvID) -> ResultBoxError<Conversation> {
|
||||
// Tables
|
||||
database::QueryInfo::new(CONV_LIST_TABLE)
|
||||
.alias("l")
|
||||
.join(CONV_MEMBERS_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)
|
||||
|
||||
.cond_conv_id("id", conv_id)
|
||||
.query_row(db_to_conversation_info)
|
||||
}
|
||||
|
||||
/// Get the list of members of a conversation
|
||||
pub fn get_list_members(conv_id: u64) -> ResultBoxError<Vec<UserID>> {
|
||||
pub fn get_list_members(conv_id: ConvID) -> Res<Vec<ConversationMember>> {
|
||||
database::QueryInfo::new(CONV_MEMBERS_TABLE)
|
||||
.cond_u64("conv_id", conv_id)
|
||||
.add_field("user_id")
|
||||
.exec(|res| res.get_user_id("user_id"))
|
||||
.cond_conv_id("conv_id", conv_id)
|
||||
.exec(db_to_conversation_member)
|
||||
}
|
||||
|
||||
/// 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_MEMBERS_TABLE)
|
||||
.cond_u64("conv_id", conv_id)
|
||||
pub fn get_user_membership(user_id: &UserID, conv_id: ConvID) -> Res<ConversationMember> {
|
||||
database::QueryInfo::new(CONV_MEMBERS_TABLE)
|
||||
.cond_conv_id("conv_id", conv_id)
|
||||
.cond_user_id("user_id", user_id)
|
||||
.exec_count()? > 0)
|
||||
.query_row(db_to_conversation_member)
|
||||
}
|
||||
|
||||
/// 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)
|
||||
}
|
||||
|
||||
/// Check out whether all the members of a conversation can add members to it or not
|
||||
pub fn can_everyone_add_members(conv_id: u64) -> ResultBoxError<bool> {
|
||||
pub fn can_everyone_add_members(conv_id: ConvID) -> ResultBoxError<bool> {
|
||||
database::QueryInfo::new(CONV_LIST_TABLE)
|
||||
.cond_u64("id", conv_id)
|
||||
.cond_conv_id("id", conv_id)
|
||||
.add_field("can_everyone_add_members")
|
||||
.query_row(|f| f.get_legacy_bool("can_everyone_add_members"))
|
||||
}
|
||||
@@ -146,25 +149,28 @@ pub fn set_following(user_id: &UserID, conv_id: u64, following: bool) -> ResultB
|
||||
}
|
||||
|
||||
/// Set a new list of members for a given conversation
|
||||
pub fn set_members(conv_id: u64, new_list: &Vec<UserID>, can_delete: bool) -> ResultBoxError<()> {
|
||||
pub fn set_members(conv_id: ConvID, new_list: &Vec<ConversationMemberSetting>, can_delete: bool) -> ResultBoxError<()> {
|
||||
let curr_list = get_list_members(conv_id)?;
|
||||
|
||||
// Add new members
|
||||
for member in new_list {
|
||||
if curr_list.contains(member) {
|
||||
continue;
|
||||
if let Some(user) = curr_list.iter().filter(|m| m.user_id == member.user_id).next() {
|
||||
// Check if we have to update admin state
|
||||
if user.is_admin != member.set_admin {
|
||||
set_admin(&conv_id, &member.user_id, member.set_admin)?;
|
||||
}
|
||||
} else {
|
||||
add_member(conv_id, &member.user_id, true, member.set_admin)?;
|
||||
}
|
||||
|
||||
add_member(conv_id, member, true)?;
|
||||
}
|
||||
|
||||
// Remove a member
|
||||
if can_delete {
|
||||
for member in curr_list {
|
||||
if new_list.contains(&member) {
|
||||
if new_list.iter().any(|m| m.user_id.eq(&member.user_id)) {
|
||||
continue;
|
||||
}
|
||||
remove_member(conv_id, &member)?;
|
||||
remove_member(conv_id, &member.user_id)?;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -173,23 +179,17 @@ pub fn set_members(conv_id: u64, new_list: &Vec<UserID>, can_delete: bool) -> Re
|
||||
}
|
||||
|
||||
/// Set a new name to the conversation
|
||||
pub fn set_name(conv_id: u64, name: Option<String>) -> ResultBoxError<()> {
|
||||
pub fn set_settings(settings: NewConversationSettings) -> Res {
|
||||
database::UpdateInfo::new(CONV_LIST_TABLE)
|
||||
.cond_u64("id", conv_id)
|
||||
.set_opt_str("name", name)
|
||||
.exec()
|
||||
}
|
||||
|
||||
/// Specify whether any member of this conversation can invite other users to join it
|
||||
pub fn set_can_everyone_add_members(conv_id: u64, allow: bool) -> ResultBoxError<()> {
|
||||
database::UpdateInfo::new(CONV_LIST_TABLE)
|
||||
.cond_u64("id", conv_id)
|
||||
.set_legacy_bool("can_everyone_add_members", allow)
|
||||
.cond_conv_id("id", settings.conv_id)
|
||||
.set_opt_str("name", settings.name)
|
||||
.set_opt_str("color", settings.color)
|
||||
.set_legacy_bool("can_everyone_add_members", settings.can_everyone_add_members)
|
||||
.exec()
|
||||
}
|
||||
|
||||
/// Search for private conversation between two users
|
||||
pub fn find_private(user_1: &UserID, user_2: &UserID) -> ResultBoxError<Vec<u64>> {
|
||||
pub fn find_private(user_1: &UserID, user_2: &UserID) -> ResultBoxError<Vec<ConvID>> {
|
||||
database::QueryInfo::new(CONV_MEMBERS_TABLE)
|
||||
.alias("t1")
|
||||
|
||||
@@ -201,7 +201,7 @@ pub fn find_private(user_1: &UserID, user_2: &UserID) -> ResultBoxError<Vec<u64>
|
||||
.cond_user_id("t2.user_id", user_2)
|
||||
.set_custom_where(format!("(SELECT COUNT(*) FROM {} WHERE conv_id = t1.conv_id) = 2", CONV_MEMBERS_TABLE).as_ref())
|
||||
.add_field("t1.conv_id AS conv_id")
|
||||
.exec(|f| f.get_u64("conv_id"))
|
||||
.exec(|f| f.get_conv_id("conv_id"))
|
||||
}
|
||||
|
||||
/// Get the last messages posted in a conversation
|
||||
@@ -247,9 +247,9 @@ pub fn get_older_messages(conv_id: u64, start_id: u64, limit: u64) -> ResultBoxE
|
||||
}
|
||||
|
||||
/// Get all the messages of a single user for a conversation
|
||||
pub fn get_user_messages_for_conversations(conv_id: u64, user_id: &UserID) -> ResultBoxError<Vec<ConversationMessage>> {
|
||||
pub fn get_user_messages_for_conversations(conv_id: ConvID, user_id: &UserID) -> ResultBoxError<Vec<ConversationMessage>> {
|
||||
database::QueryInfo::new(CONV_MESSAGES_TABLE)
|
||||
.cond_u64("conv_id", conv_id)
|
||||
.cond_conv_id("conv_id", conv_id)
|
||||
.cond_user_id("user_id", user_id)
|
||||
.exec(db_to_conversation_message)
|
||||
}
|
||||
@@ -293,16 +293,16 @@ pub fn delete_all_user_messages(user_id: &UserID) -> ResultBoxError {
|
||||
/// Remove the user from all the conversations he belongs to
|
||||
pub fn delete_all_user_conversations(user_id: &UserID) -> ResultBoxError {
|
||||
for conversation in &get_list_user(user_id)? {
|
||||
remove_user_from_conversation(user_id, conversation.id)?;
|
||||
remove_user_from_conversation(user_id, conversation, user_id)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Get the entire list of messages of a given conversation
|
||||
pub fn get_all_messages(conv_id: u64) -> ResultBoxError<Vec<ConversationMessage>> {
|
||||
pub fn get_all_messages(conv_id: ConvID) -> ResultBoxError<Vec<ConversationMessage>> {
|
||||
database::QueryInfo::new(CONV_MESSAGES_TABLE)
|
||||
.cond_u64("conv_id", conv_id)
|
||||
.cond_conv_id("conv_id", conv_id)
|
||||
.exec(db_to_conversation_message)
|
||||
}
|
||||
|
||||
@@ -318,39 +318,45 @@ pub fn send_message(msg: &NewConversationMessage) -> ResultBoxError<()> {
|
||||
let t = time();
|
||||
|
||||
// Insert the message in the database
|
||||
let msg_id = 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_expect_result()?;
|
||||
let mut msg_request = database::InsertQuery::new(CONV_MESSAGES_TABLE)
|
||||
.add_conv_id("conv_id", msg.conv_id)
|
||||
.add_u64("user_id", msg.user_id.as_ref().map(|u| u.id()).unwrap_or(0))
|
||||
.add_u64("time_sent", t);
|
||||
|
||||
if let Some(server_msg) = &msg.server_message {
|
||||
msg_request = msg_request.add_str("message", &server_msg.to_db());
|
||||
} else if let Some(message) = &msg.message {
|
||||
msg_request = msg_request.add_str("message", message);
|
||||
}
|
||||
|
||||
if let Some(file) = &msg.file {
|
||||
msg_request = msg_request.add_str("file_path", &file.path)
|
||||
.add_u64("file_size", file.size)
|
||||
.add_str("file_name", &file.name)
|
||||
.add_str("file_type", &file.r#type)
|
||||
.add_opt_str("file_thumbnail", Option::from(&file.thumbnail));
|
||||
}
|
||||
|
||||
let msg_id = msg_request.insert_expect_result()?;
|
||||
|
||||
// Update the last activity of the conversation
|
||||
database::UpdateInfo::new(CONV_LIST_TABLE)
|
||||
.cond_u64("id", msg.conv_id)
|
||||
.set_u64("last_active", t)
|
||||
.cond_conv_id("id", msg.conv_id)
|
||||
.set_u64("last_activity", t)
|
||||
.exec()?;
|
||||
|
||||
// Get the list of users to notify after the update
|
||||
let list_to_notify = database::QueryInfo::new(CONV_MEMBERS_TABLE)
|
||||
.cond_u64("conv_id", msg.conv_id)
|
||||
.cond_legacy_bool("saw_last_message", true)
|
||||
.cond_conv_id("conv_id", msg.conv_id)
|
||||
.cond_legacy_bool("following", true)
|
||||
.set_custom_where("user_id != ?")
|
||||
.add_custom_where_argument_user_id(&msg.user_id)
|
||||
.add_custom_where_argument_user_id(msg.user_id.as_ref().unwrap_or(&UserID::invalid()))
|
||||
.exec(|r| r.get_user_id("user_id"))?;
|
||||
|
||||
// Mark all the users of the conversation as unread
|
||||
database::UpdateInfo::new(CONV_MEMBERS_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.id())
|
||||
|
||||
.set_legacy_bool("saw_last_message", false)
|
||||
.exec()?;
|
||||
// Mark the user has seen his message
|
||||
if let Some(user_id) = &msg.user_id {
|
||||
mark_user_seen(msg.conv_id, user_id, msg_id)?;
|
||||
}
|
||||
|
||||
// Send an event (updated_number_unread_conversations)
|
||||
events_helper::propagate_event(&Event::UpdatedNumberUnreadConversations(&list_to_notify))?;
|
||||
@@ -419,34 +425,32 @@ pub fn get_list_unread(user_id: &UserID) -> ResultBoxError<Vec<UnreadConversatio
|
||||
|
||||
.cond_user_id("users.user_id", user_id)
|
||||
.cond_legacy_bool("users.following", true)
|
||||
.cond_legacy_bool("users.saw_last_message", false)
|
||||
|
||||
.set_custom_where("list.last_active = messages.time_insert")
|
||||
.set_custom_where("list.last_activity = messages.time_insert AND user.last_message_seen < messages.id")
|
||||
|
||||
.set_order("list.last_active DESC")
|
||||
.set_order("list.last_activity DESC")
|
||||
|
||||
.add_field("messages.conv_id")
|
||||
.add_field("name")
|
||||
.add_field("last_active")
|
||||
.add_field("last_activity")
|
||||
.add_field("messages.user_id")
|
||||
.add_field("message")
|
||||
|
||||
.exec(|res| Ok(UnreadConversation {
|
||||
id: res.get_u64("conv_id")?,
|
||||
name: res.get_optional_str("name")?,
|
||||
last_active: res.get_u64("last_active")?,
|
||||
last_active: res.get_u64("last_activity")?,
|
||||
user_id: res.get_user_id("user_id")?,
|
||||
message: res.get_str("message")?,
|
||||
}))
|
||||
}
|
||||
|
||||
/// Indicate that a user has seen the last messages of a conversation
|
||||
pub fn mark_user_seen(conv_id: u64, user_id: &UserID) -> ResultBoxError<()> {
|
||||
pub fn mark_user_seen(conv_id: ConvID, user_id: &UserID, last_msg: u64) -> ResultBoxError<()> {
|
||||
database::UpdateInfo::new(CONV_MEMBERS_TABLE)
|
||||
.cond_u64("conv_id", conv_id)
|
||||
.cond_conv_id("conv_id", conv_id)
|
||||
.cond_user_id("user_id", user_id)
|
||||
.cond_legacy_bool("saw_last_message", false)
|
||||
.set_legacy_bool("saw_last_message", true)
|
||||
.set_u64("last_message_seen", last_msg)
|
||||
.exec()?;
|
||||
|
||||
// Push an event (updated_number_unread_conversations)
|
||||
@@ -456,36 +460,41 @@ pub fn mark_user_seen(conv_id: u64, user_id: &UserID) -> ResultBoxError<()> {
|
||||
}
|
||||
|
||||
/// Remove a user from a conversation
|
||||
pub fn remove_user_from_conversation(user_id: &UserID, conv_id: u64) -> ResultBoxError<()> {
|
||||
if is_user_moderator(user_id, conv_id)? {
|
||||
delete_conversation(conv_id)
|
||||
pub fn remove_user_from_conversation(user_id: &UserID, conv: &Conversation, remover: &UserID) -> ResultBoxError<()> {
|
||||
if conv.is_last_admin(user_id) {
|
||||
delete_conversation(conv)
|
||||
} else {
|
||||
delete_member(user_id, conv_id)
|
||||
delete_member(user_id, conv.id, remover)
|
||||
}
|
||||
}
|
||||
|
||||
/// Remove permanently a conversation
|
||||
pub fn delete_conversation(conv_id: u64) -> ResultBoxError<()> {
|
||||
pub fn delete_conversation(conv: &Conversation) -> ResultBoxError<()> {
|
||||
// Delete all the messages of the conversations
|
||||
for message in get_all_messages(conv_id)? {
|
||||
for message in get_all_messages(conv.id)? {
|
||||
delete_message(&message)?;
|
||||
}
|
||||
|
||||
// Delete all the members of the conversation
|
||||
database::DeleteQuery::new(CONV_MEMBERS_TABLE)
|
||||
.cond_u64("conv_id", conv_id)
|
||||
.cond_conv_id("conv_id", conv.id)
|
||||
.exec()?;
|
||||
|
||||
// Delete associated background image, if any
|
||||
if let Some(image) = &conv.background {
|
||||
delete_user_data_file_if_exists(image)?;
|
||||
}
|
||||
|
||||
// Delete the conversation entry itself
|
||||
database::DeleteQuery::new(CONV_LIST_TABLE)
|
||||
.cond_u64("id", conv_id)
|
||||
.cond_conv_id("id", conv.id)
|
||||
.exec()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Delete a conversation membership
|
||||
pub fn delete_member(user_id: &UserID, conv_id: u64) -> ResultBoxError<()> {
|
||||
pub fn delete_member(user_id: &UserID, conv_id: ConvID, _remover: &UserID) -> ResultBoxError<()> {
|
||||
for msg in get_user_messages_for_conversations(conv_id, user_id)? {
|
||||
delete_message(&msg)?;
|
||||
}
|
||||
@@ -507,17 +516,30 @@ pub fn is_message_owner(user_id: &UserID, message_id: u64) -> ResultBoxError<boo
|
||||
|
||||
/// 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")?;
|
||||
let conv_id = row.get_conv_id("id")?;
|
||||
Ok(Conversation {
|
||||
id: conv_id,
|
||||
owner_id: row.get_user_id("owner_id")?,
|
||||
color: row.get_optional_str("color")?,
|
||||
background: row.get_optional_str("background")?,
|
||||
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")?,
|
||||
last_activity: row.get_u64("last_activity")?,
|
||||
creation_time: row.get_u64("creation_time")?,
|
||||
group_id: row.get_optional_group_id("group_id")?,
|
||||
})
|
||||
}
|
||||
|
||||
/// Turn a database entry into a ConversationMember object
|
||||
fn db_to_conversation_member(row: &database::RowResult) -> Res<ConversationMember> {
|
||||
Ok(ConversationMember {
|
||||
member_id: row.get_u64("id")?,
|
||||
conv_id: row.get_conv_id("conv_id")?,
|
||||
user_id: row.get_user_id("user_id")?,
|
||||
added_on: row.get_u64("added_on")?,
|
||||
following: row.get_legacy_bool("following")?,
|
||||
saw_last_message: row.get_legacy_bool("saw_last_message")?,
|
||||
is_admin: row.get_legacy_bool("is_admin")?,
|
||||
last_message_seen: row.get_u64("last_message_seen")?,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -529,10 +551,10 @@ fn db_to_conversation_message(row: &database::RowResult) -> ResultBoxError<Conve
|
||||
false => Some(row.get_user_id("user_id")?)
|
||||
};
|
||||
|
||||
let file = match row.is_null_or_empty("filepath")? {
|
||||
let file = match row.is_null_or_empty("file_path")? {
|
||||
true => None,
|
||||
false => Some(ConversationMessageFile {
|
||||
path: row.get_str("filepath")?,
|
||||
path: row.get_str("file_path")?,
|
||||
size: row.get_u64("file_size")?,
|
||||
name: row.get_str("file_name")?,
|
||||
thumbnail: row.get_optional_str("file_thumbnail")?,
|
||||
@@ -553,7 +575,7 @@ fn db_to_conversation_message(row: &database::RowResult) -> ResultBoxError<Conve
|
||||
Ok(ConversationMessage {
|
||||
id: row.get_u64("id")?,
|
||||
time_sent: row.get_u64("time_sent")?,
|
||||
conv_id: row.get_u64("conv_id")?,
|
||||
conv_id: row.get_conv_id("conv_id")?,
|
||||
user_id,
|
||||
message,
|
||||
server_message,
|
||||
|
@@ -12,6 +12,7 @@ use crate::data::config::{conf, DatabaseConfig};
|
||||
use crate::data::error::{ExecError, ResultBoxError};
|
||||
use crate::data::group_id::GroupID;
|
||||
use crate::data::user::UserID;
|
||||
use crate::data::conversation::ConvID;
|
||||
|
||||
/// Database access helper
|
||||
///
|
||||
@@ -174,6 +175,11 @@ impl QueryInfo {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn cond_conv_id(mut self, key: &str, val: ConvID) -> QueryInfo {
|
||||
self.conditions.insert(key.to_string(), mysql::Value::from(val.id()));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn cond_legacy_bool(mut self, key: &str, val: bool) -> QueryInfo {
|
||||
let val = match val {
|
||||
true => 1,
|
||||
@@ -358,6 +364,19 @@ impl<'a> RowResult<'a> {
|
||||
Ok(GroupID::new(self.get_u64(name)?))
|
||||
}
|
||||
|
||||
/// Get the optional ID of a group included in the response
|
||||
pub fn get_optional_group_id(&self, name: &str) -> ResultBoxError<Option<GroupID>> {
|
||||
Ok(match self.get_optional_u64(name)? {
|
||||
None | Some(0) => None,
|
||||
Some(id) => Some(GroupID::new(id))
|
||||
})
|
||||
}
|
||||
|
||||
/// Get the ID of a conversation included in the response
|
||||
pub fn get_conv_id(&self, name: &str) -> ResultBoxError<ConvID> {
|
||||
Ok(ConvID::new(self.get_u64(name)?))
|
||||
}
|
||||
|
||||
/// Find a string included in the request
|
||||
pub fn get_str(&self, name: &str) -> Result<String, Box<dyn Error>> {
|
||||
let value = self.row.get_opt(self.find_col(name)?);
|
||||
@@ -588,6 +607,19 @@ impl InsertQuery {
|
||||
self
|
||||
}
|
||||
|
||||
/// Add an optional number. If None, Null will be inserted
|
||||
pub fn add_opt_u64(mut self, key: &str, value: Option<u64>) -> InsertQuery {
|
||||
self.values.insert(key.to_string(), value
|
||||
.map(|u| Value::UInt(u))
|
||||
.unwrap_or(Value::NULL));
|
||||
self
|
||||
}
|
||||
|
||||
/// Add an optional number. If None, Null will be inserted
|
||||
pub fn add_opt_group_id(self, key: &str, value: Option<GroupID>) -> InsertQuery {
|
||||
self.add_opt_u64(key, value.map(|u| u.id()))
|
||||
}
|
||||
|
||||
/// Add an integer
|
||||
pub fn add_i64(mut self, key: &str, value: i64) -> InsertQuery {
|
||||
self.values.insert(key.to_string(), Value::from(value));
|
||||
@@ -619,6 +651,12 @@ impl InsertQuery {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn add_conv_id(mut self, key: &str, value: ConvID) -> InsertQuery {
|
||||
self.values.insert(key.to_string(), Value::from(value.id()));
|
||||
self
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// Legacy database boolean (1 = true / 0 = false)
|
||||
pub fn add_legacy_bool(mut self, key: &str, value: bool) -> InsertQuery {
|
||||
@@ -758,6 +796,11 @@ impl DeleteQuery {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn cond_conv_id(mut self, key: &str, value: ConvID) -> DeleteQuery {
|
||||
self.conditions.insert(key.to_string(), Value::from(value.id()));
|
||||
self
|
||||
}
|
||||
|
||||
/// Execute the delete query
|
||||
pub fn exec(self) -> ResultBoxError<()> {
|
||||
delete(self)
|
||||
@@ -832,6 +875,13 @@ impl UpdateInfo {
|
||||
self
|
||||
}
|
||||
|
||||
/// Filter with a conversation id
|
||||
pub fn cond_conv_id(mut self, name: &str, val: ConvID) -> UpdateInfo {
|
||||
self.cond.insert(name.to_string(), Value::UInt(val.id()));
|
||||
self
|
||||
}
|
||||
|
||||
|
||||
/// Filter with an unsigned integer
|
||||
pub fn cond_u64(mut self, name: &str, val: u64) -> UpdateInfo {
|
||||
self.cond.insert(name.to_string(), Value::UInt(val));
|
||||
|
Reference in New Issue
Block a user