From a82b65f18d993644b7356ee6f3abe320655588ea Mon Sep 17 00:00:00 2001 From: Pierre Date: Mon, 26 Jun 2017 10:38:58 +0200 Subject: [PATCH] Users are marked as "read" when their browser download last messages --- RestControllers/conversationsController.php | 10 +++++-- classes/components/conversations.php | 30 +++++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/RestControllers/conversationsController.php b/RestControllers/conversationsController.php index 27c2280..f23049a 100644 --- a/RestControllers/conversationsController.php +++ b/RestControllers/conversationsController.php @@ -306,6 +306,9 @@ class conversationsController{ //Then we can get the ten last messages of the conversation system $conversationsMessages["conversation-".$conversationID] = CS::get()->components->conversations->getLastMessages($conversationID, 10); + + //Specify that user has seen last messages + CS::get()->components->conversations->markUserAsRead(userID, $conversationID); } } @@ -329,11 +332,11 @@ class conversationsController{ //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); + 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"); + 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 @@ -342,6 +345,9 @@ class conversationsController{ //Then we can get informations about the conversation $conversationsMessages["conversation-".$conversationID] = CS::get()->components->conversations->getNewMessages($conversationID, $last_message_id); + + //Specify that user has seen last messages + CS::get()->components->conversations->markUserAsRead(userID, $conversationID); } } diff --git a/classes/components/conversations.php b/classes/components/conversations.php index b40a5b9..9d77cc2 100644 --- a/classes/components/conversations.php +++ b/classes/components/conversations.php @@ -509,6 +509,36 @@ class conversations { return true; } + /** + * Mark the user of a conversation as "read" for a conversation + * + * @param Integer $userID The ID of the user to update + * @param Integer $conversationID The ID of a conversation to update + * @return Boolean True for a success + */ + public function markUserAsRead($userID, $conversationID) : bool { + + //Prepare database request + $tableName = $this->conversationsUsersTable; + $conditions = "ID_".$this->conversationsListTable." = ? AND ID_utilisateurs = ?"; + $condVals = array( + $conversationID, + $userID + ); + + //Define new values + $newValues = array( + "saw_last_message" => 1 + ); + + //Try to perform a request on the database + if(!CS::get()->db->updateDB($tableName, $conditions, $newValues, $condVals)) + return false; //An error occured + + //Success + return true; + } + /** * Send a new message *