From abf9a7fa82adfeb5780f256da103859325331799 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 23 Jun 2020 14:09:52 +0200 Subject: [PATCH] Can delete a single conversation message --- src/controllers/conversations_controller.rs | 13 +++++++++++++ src/controllers/routes.rs | 2 ++ src/helpers/conversations_helper.rs | 12 ++++++++++++ 3 files changed, 27 insertions(+) diff --git a/src/controllers/conversations_controller.rs b/src/controllers/conversations_controller.rs index 9a4c6fd..76c7523 100644 --- a/src/controllers/conversations_controller.rs +++ b/src/controllers/conversations_controller.rs @@ -312,4 +312,17 @@ pub fn update_message(r: &mut HttpRequestHandler) -> RequestResult { conversations_helper::update_message_content(msg_id, &new_content)?; r.success("Conversation message content successfully updated") +} + +/// Delete a conversation message +pub fn delete_message(r: &mut HttpRequestHandler) -> RequestResult { + let msg_id = r.post_u64("messageID")?; + + if !conversations_helper::is_message_owner(r.user_id()?, msg_id)? { + r.forbidden("You are not the owner of this message!".to_string())?; + } + + conversations_helper::delete_message_by_id(msg_id)?; + + r.success("The message has been successfully deleted!") } \ No newline at end of file diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index debd548..a4f990d 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -116,6 +116,8 @@ pub fn get_routes() -> Vec { Route::post("/conversations/updateMessage", Box::new(conversations_controller::update_message)), + Route::post("/conversations/deleteMessage", Box::new(conversations_controller::delete_message)), + // Virtual directory controller Route::post("/user/findbyfolder", Box::new(virtual_directory_controller::find_user)), diff --git a/src/helpers/conversations_helper.rs b/src/helpers/conversations_helper.rs index 02e1e12..71c8949 100644 --- a/src/helpers/conversations_helper.rs +++ b/src/helpers/conversations_helper.rs @@ -260,6 +260,13 @@ pub fn get_all_messages(conv_id: u64) -> ResultBoxError .exec(db_to_conversation_message) } +/// Get a single message specified by its ID +pub fn get_single_message(msg_id: u64) -> ResultBoxError { + database::QueryInfo::new(CONV_MESSAGES_TABLE) + .cond_u64("id", msg_id) + .query_row(db_to_conversation_message) +} + /// Send a new conversation message pub fn send_message(msg: &NewConversationMessage) -> ResultBoxError<()> { let t = time(); @@ -319,6 +326,11 @@ pub fn delete_message(msg: &ConversationMessage) -> ResultBoxError<()> { Ok(()) } +/// Delete a message with a specific ID +pub fn delete_message_by_id(id: u64) -> ResultBoxError<()> { + delete_message(&get_single_message(id)?) +} + /// 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)