From 46985a77f91b4c3b66c81adee943ddfdadcb1283 Mon Sep 17 00:00:00 2001 From: Pierre Date: Mon, 26 Jun 2017 10:27:38 +0200 Subject: [PATCH] Conversations can be refreshed --- RestControllers/conversationsController.php | 42 +++++++++++++-- classes/components/conversations.php | 58 +++++++++++++++++---- 2 files changed, 88 insertions(+), 12 deletions(-) diff --git a/RestControllers/conversationsController.php b/RestControllers/conversationsController.php index ae3b8ea..27c2280 100644 --- a/RestControllers/conversationsController.php +++ b/RestControllers/conversationsController.php @@ -294,21 +294,57 @@ class conversationsController{ //Prepare return $conversationsMessages = array(); - //Check if we have to give the latest messages of a conversation + //Check if we have to give the latest messages of some conversations if(isset($_POST['newConversations'])){ //Get conversations ID $newConversations = numbers_list_to_array($_POST['newConversations']); foreach($newConversations as $conversationID){ - //First, check the users belongs to the conversation + //First, check the user belongs to the conversation if(!CS::get()->components->conversations->userBelongsTo(userID, $conversationID)) Rest_fatal_error(401, "Specified user doesn't belongs to the conversation number ".$conversationID." !"); - //Then we can get the ten las messages of the conversation system + //Then we can get the ten last messages of the conversation system $conversationsMessages["conversation-".$conversationID] = CS::get()->components->conversations->getLastMessages($conversationID, 10); } } + //Check if we have to refresh some conversations + if(isset($_POST['toRefresh'])){ + + //Try to decode informations about the conversations + $toRefresh = json_decode($_POST['toRefresh'], true); + if($toRefresh === false) + Rest_fatal_error(400, "Couldn't get refresh conversations informations !"); + + //Process each conversation + foreach($toRefresh as $conversationID=>$informations){ + + //Get conversation ID + $conversationID = toInt(str_replace("conversation-", "", $conversationID)); + + //Check if the conversation is not a new conversation too + if(isset($conversationsMessages["conversation-".$conversationID])) + Rest_fatal_error(401, "Conversation marked as new can't be refreshed !"); + + //Check if conversation number is valid + if($conversationID < 1) + Rest_fatal_error(401, "An error occured while trying to extract given conversation ID to refresh :".$conversationID); + + //Check if informations where given about the limit of the informations to get + if(!isset($informations["last_message_id"])) + Rest_fatal_error(401, "Conversation ".$conversationID." couldn't be refreshed: not enough informations"); + $last_message_id = toInt($informations["last_message_id"]); + + //Check if the user belongs to the conversation + if(!CS::get()->components->conversations->userBelongsTo(userID, $conversationID)) + Rest_fatal_error(401, "Specified user doesn't belongs to the conversation number ".$conversationID." !"); + + //Then we can get informations about the conversation + $conversationsMessages["conversation-".$conversationID] = CS::get()->components->conversations->getNewMessages($conversationID, $last_message_id); + } + } + //Return result return $conversationsMessages; } diff --git a/classes/components/conversations.php b/classes/components/conversations.php index d171d5c..b40a5b9 100644 --- a/classes/components/conversations.php +++ b/classes/components/conversations.php @@ -548,15 +548,57 @@ class conversations { */ public function getLastMessages($conversationID, $numberOfMessages) : array { - //Prepare database request - $tableName = $this->conversationsMessagesTable; - //Define conditions $conditions = "WHERE ID_".$this->conversationsListTable." = ? ORDER BY ID DESC LIMIT ".($numberOfMessages*1); $condVals = array( $conversationID ); + //Perform request + $messages = $this->getMessages($conditions, $condVals); + + //Revert messages order + $messages = array_reverse($messages); + + //Return messages + return $messages; + } + + /** + * Get the new messages of a conversation + * + * @param Integer $conversationID The ID of the target conversation + * @param Integer $lastMessageID The ID of the last know message + * @return Array A list of messages + */ + public function getNewMessages($conversationID, $lastMessageID) : array { + + //Define conditions + $conditions = "WHERE ID_".$this->conversationsListTable." = ? AND ID > ? ORDER BY ID"; + $condVals = array( + $conversationID, + $lastMessageID + ); + + //Perform request + $messages = $this->getMessages($conditions, $condVals); + + //Return messages + return $messages; + } + + /** + * Get a list of conversation messages based on specified conditions + * + * @param String $conditions The conditions of the request + * @param Array $conditionsValues The values of the conditions (Optionnal) + * @return Array The list of messages + */ + private function getMessages($conditions, $conditionsValues = array()) : array{ + + //Prepare database request + $tableName = $this->conversationsMessagesTable; + //Define required fields $requiredFields = array( "ID", @@ -567,21 +609,19 @@ class conversations { ); //Try to perform request on the database - $messages = CS::get()->db->select($tableName, $conditions, $condVals, $requiredFields); + $messages = CS::get()->db->select($tableName, $conditions, $conditionsValues, $requiredFields); //Process each message array_walk($messages, function(&$item){ - //Check if image is not null + + //Check if the image of the message is not null if($item["image_path"] !== null && $item["image_path"] != ""){ //Replace image name with full URL $item["image_path"] = path_user_data($item["image_path"]); } }); - //Revert messages order - $messages = array_reverse($messages); - - //Return messages + //Return result return $messages; }