From be91602277a0c339f5887f290add72e917d37ddd Mon Sep 17 00:00:00 2001 From: Pierre Date: Mon, 19 Jun 2017 14:53:30 +0200 Subject: [PATCH] getPrivateConversation fully operationnal --- RestControllers/conversationsController.php | 43 ++++++++++++++++++++- classes/components/conversations.php | 2 +- classes/components/user.php | 26 ++++++++++++- 3 files changed, 67 insertions(+), 4 deletions(-) diff --git a/RestControllers/conversationsController.php b/RestControllers/conversationsController.php index 87804db..b761754 100644 --- a/RestControllers/conversationsController.php +++ b/RestControllers/conversationsController.php @@ -167,8 +167,47 @@ class conversationsController{ user_login_required(); //Check for parametres - if(!isset($_POST['user1']) OR !isset($_POST['user2'])) - Rest_fatal_error(400, "Please check your parametres") + if(!isset($_POST['otherUser'])) + Rest_fatal_error(400, "Please check your parametres !"); + + //Extract parametres + $otherUser = toInt($_POST['otherUser']); + if(isset($_POST["allowCreate"])) + $allowCreate = $_POST["allowCreate"] == "true" ? true : false; + else + $allowCreate = false; + + //Check the user exists + if(!CS::get()->components->user->exists($otherUser)) + Rest_fatal_error(400, "Specified user does not exist !"); + //Search the database + $results = CS::get()->components->conversations->findPrivate(userID, $otherUser); + + //Count results number + if(count($results) === 0) { + + //We check if we are not allowed to create a conversation + if(!$allowCreate) + Rest_fatal_error(404, "Not any private conversation were found. The server wasn't allowed to create a new one..."); + + //Now we can try to create the conversation + $ID_owner = userID; + $follow = false; //Not following by default + $conversationMembers = array(userID, $otherUser); + + //Try to create the conversation + $conversationID = CS::get()->components->conversations->create($ID_owner, $follow, $conversationMembers); + + //Check for errors + if($conversationID == 0) + Rest_fatal_error(500, "Couldn't create the conversation !"); + + //Save result + $results = array($conversationID); + } + + //Success + return array("conversationsID" => $results); } } \ No newline at end of file diff --git a/classes/components/conversations.php b/classes/components/conversations.php index 1903e17..78e8f50 100644 --- a/classes/components/conversations.php +++ b/classes/components/conversations.php @@ -135,7 +135,7 @@ class conversations { * @param Mixed $name Optionnal, the name of the conversation * @return Integer 0 for a fail else the ID of the newly created conversation */ - public function create($userID, $follow, array $usersList, $name){ + public function create($userID, $follow, array $usersList, $name = ""){ $mainInformations = array( "ID_utilisateurs" => $userID*1, diff --git a/classes/components/user.php b/classes/components/user.php index e431b39..9694876 100644 --- a/classes/components/user.php +++ b/classes/components/user.php @@ -32,7 +32,7 @@ class User{ * @param String $serviceID The ID of the service * @return String Token if success, false if fails */ - public function generateUserLoginTokens($email, $password, $serviceID){ + public function generateUserLoginTokens($email, $password, $serviceID) : array{ //Try to find user ID in the database $conditions = "WHERE mail = ? AND password = ?"; $values = array( @@ -273,6 +273,30 @@ class User{ return true; } + /** + * Check if a user exists or not + * + * @param Integer $userID The ID of the user to check + * @return Boolean Depends of the existence of the user + */ + public function exists($userID){ + //Perform a request on the database + $tableName = $this->userTable; + $condition = "WHERE ID = ?"; + $condValues = array($userID); + $requiredFields = array("ID"); + + //Try to perform request + $result = CS::get()->db->select($tableName, $condition, $condValues, $requiredFields); + + //Check for errors + if($result === false) + return false; //An error occured + + //Check and return result + return count($result) !== 0; + } + /** * Crypt user password *