diff --git a/src/helpers/conversations_helper.rs b/src/helpers/conversations_helper.rs index 42496fc..c15c288 100644 --- a/src/helpers/conversations_helper.rs +++ b/src/helpers/conversations_helper.rs @@ -8,11 +8,11 @@ use crate::data::conversation_message::ConversationMessage; use crate::data::error::{ExecError, ResultBoxError}; use crate::data::new_conversation::NewConversation; use crate::data::new_conversation_message::NewConversationMessage; +use crate::data::unread_conversation::UnreadConversation; use crate::data::user::UserID; use crate::helpers::database; use crate::helpers::database::InsertQuery; use crate::utils::date_utils::time; -use crate::data::unread_conversation::UnreadConversation; use crate::utils::user_data_utils::user_data_path; /// Create a new conversation. This method returns the ID of the created conversation @@ -253,6 +253,13 @@ pub fn get_user_messages_for_conversations(conv_id: u64, user_id: UserID) -> Res .exec(db_to_conversation_message) } +/// Get the entire list of messages of a given conversation +pub fn get_all_messages(conv_id: u64) -> ResultBoxError> { + database::QueryInfo::new(CONV_MESSAGES_TABLE) + .cond_u64("conv_id", conv_id) + .exec(db_to_conversation_message) +} + /// Send a new conversation message pub fn send_message(msg: &NewConversationMessage) -> ResultBoxError<()> { let t = time(); @@ -349,12 +356,32 @@ 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)? { - unimplemented!() + delete_conversation(conv_id) } else { delete_member(user_id, conv_id) } } +/// Remove permanently a conversation +pub fn delete_conversation(conv_id: u64) -> ResultBoxError<()> { + // Delete all the messages of the conversations + for message in get_all_messages(conv_id)? { + delete_message(&message)?; + } + + // Delete all the members of the conversation + database::DeleteQuery::new(CONV_USERS_TABLE) + .cond_u64("conv_id", conv_id) + .exec()?; + + // Delete the conversation entry itself + database::DeleteQuery::new(CONV_LIST_TABLE) + .cond_u64("id", conv_id) + .exec()?; + + Ok(()) +} + /// Delete a conversation membership pub fn delete_member(user_id: UserID, conv_id: u64) -> ResultBoxError<()> { for msg in get_user_messages_for_conversations(conv_id, user_id)? {