From 3960cbde6c19a707d2dd40569720eec9c7affd4b Mon Sep 17 00:00:00 2001 From: Pierre Date: Sat, 16 Dec 2017 14:02:39 +0100 Subject: [PATCH] Can get the messages of a single conversation --- RestControllers/conversationsController.php | 39 +++++++++++++++++++++ classes/components/conversations.php | 2 +- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/RestControllers/conversationsController.php b/RestControllers/conversationsController.php index f23049a..d5cf767 100644 --- a/RestControllers/conversationsController.php +++ b/RestControllers/conversationsController.php @@ -354,4 +354,43 @@ class conversationsController{ //Return result return $conversationsMessages; } + + /** + * Refresh the messages of a specific conversation + * + * @url POST /conversations/refresh_single + */ + public function refresh_single(){ + user_login_required(); + + //Get the ID of the conversation to refresh + if(!isset($_POST['conversationID'])) + Rest_fatal_error(400, "Please specify a conversation ID !"); + + //Get the last message ID downloaded by the client + if(!isset($_POST['last_message_id'])) + Rest_fatal_error(400, "Please specify the ID of the last message you've got!"); + + $conversationID = toInt($_POST['conversationID']); + $last_message_id = toInt($_POST['last_message_id']); + + //Check if the current user can access the conversation + if(!CS::get()->components->conversations->userBelongsTo(userID, $conversationID)) + Rest_fatal_error(401, "Specified user doesn't belongs to the conversation number ".$conversationID." !"); + + //Check if user has already some of the messages of the conversations, or + //If we have to return the list of the last ten messages + if($last_message_id == 0){ + $messages = + CS::get()->components->conversations->getLastMessages($conversationID, 10); + } + else { + $messages = + CS::get()->components->conversations->getNewMessages($conversationID, $last_message_id); + } + + //Return the messges + return $messages; + + } } \ No newline at end of file diff --git a/classes/components/conversations.php b/classes/components/conversations.php index 9d77cc2..c351bf9 100644 --- a/classes/components/conversations.php +++ b/classes/components/conversations.php @@ -576,7 +576,7 @@ class conversations { * @param Integer $numberOfMessages The number of messages to return * @return Array The messages of the conversation */ - public function getLastMessages($conversationID, $numberOfMessages) : array { + public function getLastMessages(int $conversationID, int $numberOfMessages) : array { //Define conditions $conditions = "WHERE ID_".$this->conversationsListTable." = ? ORDER BY ID DESC LIMIT ".($numberOfMessages*1);