From 0899d7c3729367958a5c07ce225a4f748f06c8f9 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 23 Jun 2020 07:44:42 +0200 Subject: [PATCH] Can remove a member from a conversation --- src/controllers/conversations_controller.rs | 6 ++- src/data/conversation_message.rs | 1 + src/helpers/conversations_helper.rs | 52 ++++++++++++++++++++- 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/controllers/conversations_controller.rs b/src/controllers/conversations_controller.rs index cdf3041..188d515 100644 --- a/src/controllers/conversations_controller.rs +++ b/src/controllers/conversations_controller.rs @@ -293,5 +293,9 @@ pub fn list_unread(r: &mut HttpRequestHandler) -> RequestResult { /// Delete a conversation pub fn delete_conversation(r: &mut HttpRequestHandler) -> RequestResult { - r.success("Delete a conversation") + let conv_id = r.post_conv_id("conversationID")?; + + conversations_helper::remove_user_from_conversation(r.user_id()?, conv_id)?; + + r.success("The conversation has been deleted") } \ No newline at end of file diff --git a/src/data/conversation_message.rs b/src/data/conversation_message.rs index a7a6e57..5a6212b 100644 --- a/src/data/conversation_message.rs +++ b/src/data/conversation_message.rs @@ -5,6 +5,7 @@ use crate::data::user::UserID; /// Information about a single conversation message +#[derive(Debug)] pub struct ConversationMessage { pub id: u64, pub time_sent: u64, diff --git a/src/helpers/conversations_helper.rs b/src/helpers/conversations_helper.rs index 7667947..42496fc 100644 --- a/src/helpers/conversations_helper.rs +++ b/src/helpers/conversations_helper.rs @@ -13,6 +13,7 @@ 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 pub fn create(conv: &NewConversation) -> ResultBoxError { @@ -244,6 +245,14 @@ 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> { + database::QueryInfo::new(CONV_MESSAGES_TABLE) + .cond_u64("conv_id", conv_id) + .cond_user_id("user_id", user_id) + .exec(db_to_conversation_message) +} + /// Send a new conversation message pub fn send_message(msg: &NewConversationMessage) -> ResultBoxError<()> { let t = time(); @@ -277,6 +286,24 @@ pub fn send_message(msg: &NewConversationMessage) -> ResultBoxError<()> { Ok(()) } +/// Remove a message from a conversation +pub fn delete_message(msg: &ConversationMessage) -> ResultBoxError<()> { + + // Delete associated image (if any) + if let Some(img) = &msg.image_path { + let path = user_data_path(img.as_ref()); + if path.exists() { + std::fs::remove_file(path)?; + } + } + + database::DeleteQuery::new(CONV_MESSAGES_TABLE) + .cond_u64("ID", msg.id) + .exec()?; + + Ok(()) +} + /// Count the number of unread conversation for a specified user pub fn count_unread_for_user(user_id: UserID) -> ResultBoxError { database::QueryInfo::new(CONV_USERS_TABLE) @@ -300,12 +327,12 @@ pub fn get_list_unread(user_id: UserID) -> ResultBoxError ResultBoxError<()> { .exec() } +/// 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!() + } else { + delete_member(user_id, conv_id) + } +} + +/// 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)? { + delete_message(&msg)?; + } + + // Delete membership + remove_member(conv_id, user_id)?; + + Ok(()) +} + /// Turn a database entry into a ConversationInfo object fn db_to_conversation_info(row: &database::RowResult) -> ResultBoxError { let conv_id = row.get_u64("id")?;