From e95f6cfb982b8afa8da565966ba5202bd856398d Mon Sep 17 00:00:00 2001 From: Pierre Date: Sun, 31 Dec 2017 13:49:10 +0100 Subject: [PATCH] A user can delete his membership from a conversation --- RestControllers/conversationsController.php | 12 +++---- classes/components/conversations.php | 35 +++++++++++++++++++++ 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/RestControllers/conversationsController.php b/RestControllers/conversationsController.php index 08edfc9..85f9c52 100644 --- a/RestControllers/conversationsController.php +++ b/RestControllers/conversationsController.php @@ -34,12 +34,8 @@ class conversationsController{ public function getOneInformations(){ user_login_required(); - //First, check the parametres - if(!isset($_POST['conversationID'])) - Rest_fatal_error(501, "No conversation ID specified with the request"); - - //Extract data - $conversationID = toInt($_POST['conversationID']); + //Get conversation ID + $conversationID = getPostConversationID("conversationID"); //Try to get informations about the conversation $conversationsList = CS::get()->components->conversations->getList(userID, $conversationID); @@ -412,7 +408,9 @@ class conversationsController{ Rest_fatal_error(500, "Couldn't delete the conversation from the server !"); } else { - Rest_fatal_error(500, "Not implemented yet !!"); + //Delete the membership to the conversation + if(!CS::get()->components->conversations->delete_member($conversationID, userID)) + Rest_fatal_error(500, "Couldn't delete conversation membership !"); } diff --git a/classes/components/conversations.php b/classes/components/conversations.php index 19756f5..a9c54bd 100644 --- a/classes/components/conversations.php +++ b/classes/components/conversations.php @@ -635,6 +635,8 @@ class conversations { /** * Delete a conversation * + * Delete all the messages, membership and informations about a conversation + * * @param int $conID The conversation to delete * @return bool True in case of success / False else */ @@ -661,6 +663,39 @@ class conversations { return true; } + /** + * Delete a conversation membership + * + * This function is just removing a user from a conversation by + * deleting all the messages the user has posted and removing its + * membership + * + * @param int $convID The target conversation + * @param int $memberID The ID of the member to delete from the conversation + * @return bool True in case of success / false else + */ + public function delete_member(int $convID, int $memberID) : bool { + + //Get all the messages of member the conversation + $messages = $this->getMessages( + "WHERE ID_".$this->conversationsListTable." = ? AND ID_utilisateurs = ?", + array($convID, $memberID)); + + //Delete each message + foreach($messages as $message){ + if(!$this->delete_message($message['ID'], $convID)) + return false; + } + + //Delete user membership + if(!$this->removeMember($convID, $memberID)) + return false; + + //Success + return true; + } + + /** * Delete a single message of a conversation *