From 952779f527cdd543087140532eb66d4f82f43537 Mon Sep 17 00:00:00 2001 From: Pierre Date: Sun, 18 Jun 2017 10:07:52 +0200 Subject: [PATCH] Follow state of a conversation can be updated --- RestControllers/conversationsController.php | 36 +++++++++++++++++-- RestControllers/friendsController.php | 2 +- RestControllers/searchController.php | 2 +- RestControllers/userController.php | 12 +++---- classes/components/conversations.php | 39 +++++++++++++++++++++ functions/requests.php | 3 +- index.php | 5 ++- 7 files changed, 84 insertions(+), 15 deletions(-) diff --git a/RestControllers/conversationsController.php b/RestControllers/conversationsController.php index a8d6ca5..9c71dd7 100644 --- a/RestControllers/conversationsController.php +++ b/RestControllers/conversationsController.php @@ -12,7 +12,7 @@ class conversationsController{ * * @url POST /conversations/getList */ - public function getConversationsList(){ + public function getList(){ user_login_required(); //Try to get the list @@ -31,7 +31,7 @@ class conversationsController{ * * @url POST /conversations/getInfosOne */ - public function getOneConversationInformations(){ + public function getOneInformations(){ user_login_required(); //First, check the parametres @@ -62,7 +62,7 @@ class conversationsController{ * * @url POST /conversations/create */ - public function createConversation(){ + public function create(){ user_login_required(); //Check for parametres @@ -96,4 +96,34 @@ class conversationsController{ ); } + /** + * Update a conversation settings + * + * @url POST /conversations/updateSettings + */ + public function updateSettings(){ + user_login_required(); + + //Check conversation ID was specified + if(!isset($_POST["conversationID"])) + Rest_fatal_error("501", "Please specify a conversation ID !"); + $conversationID = toInt($_POST["conversationID"]); + + //Check if the user is a conversation moderator or not + if(!CS::get()->components->conversations->userBelongsTo(userID, $conversationID)) + Rest_fatal_error("401", "Specified user doesn't belongs to the conversation !"); + + //Check if user want to update its follow state + if(isset($_POST['following'])){ + $follow = $_POST["following"] === "true" ? true : false; + + //Try to update follow state + if(!CS::get()->components->conversations->changeFollowState(userID, $conversationID, $follow)) + Rest_fatal_error(500, "Couldn't update user follow state !"); + } + + + //Success + return array("success" => "User informations were successfully updated !"); + } } \ No newline at end of file diff --git a/RestControllers/friendsController.php b/RestControllers/friendsController.php index 005d0fc..22e1978 100644 --- a/RestControllers/friendsController.php +++ b/RestControllers/friendsController.php @@ -39,7 +39,7 @@ class friendsController{ Rest_fatal_error(501, "Please check your parametres !"); //Extract informations and process request - $friendID = $_POST['friendID']*1; + $friendID = toInt($_POST['friendID']); $acceptRequest = $_POST['accept'] == "true"; //Try to perform request diff --git a/RestControllers/searchController.php b/RestControllers/searchController.php index 19fa0e6..26c8dd8 100644 --- a/RestControllers/searchController.php +++ b/RestControllers/searchController.php @@ -21,7 +21,7 @@ class searchController Rest_fatal_error(400, "Please specify search terms"); //Check for search limit - $searchLimit = (isset($_POST['searchLimit']) ? $_POST['searchLimit']*1 : 5); + $searchLimit = (isset($_POST['searchLimit']) ? toInt($_POST['searchLimit']) : 5); //Check the limit if($searchLimit < 1 || $searchLimit > 25) diff --git a/RestControllers/userController.php b/RestControllers/userController.php index c1c021c..05d8052 100644 --- a/RestControllers/userController.php +++ b/RestControllers/userController.php @@ -72,18 +72,14 @@ class userController //Determine userID if(isset($_POST['userID'])){ - $usersID = array($_POST['userID']*1); + $usersID = array(toInt($_POST['userID'])); } elseif(isset($_POST['usersID'])){ //Generate users ID list - $usersID = array(); - foreach(explode(",", $_POST['usersID']) as $userID){ - if($userID*1 > 0) - $usersID[$userID*1] = $userID*1; - } - + $usersID = users_list_to_array($_POST['usersID']); + //Check for errors - if(count($userID) == 0) + if(count($usersID) == 0) Rest_fatal_error(400, "No user ID were specified!"); } else diff --git a/classes/components/conversations.php b/classes/components/conversations.php index a2463ab..24781d0 100644 --- a/classes/components/conversations.php +++ b/classes/components/conversations.php @@ -207,6 +207,45 @@ class conversations { return $result != 0; } + /** + * Change the follow state of a user on conversation + * + * @param Integer $userID The ID to update + * @param Integer $conversationID The ID of the conversation + * @param Boolean $follow Specify if the conversation is followed or not + * @return Boolean True for a success + */ + public function changeFollowState($userID, $conversationID, $follow){ + + //Prepare the request on the database + $tableName = $this->conversationsUsersTable; + $conditions = "ID_".$this->conversationsListTable." = ? AND ID_utilisateurs = ?"; + $condVals = array( + $conversationID, + $userID + ); + + //Defines modifications + $modifs = array( + "following" => $follow ? 1 : 0, + ); + + //Update the table + if(!CS::get()->db->updateDB($tableName, $conditions, $modifs, $condVals)) + return false; //An error occured + + //Success + return true; + } + + /** + * Check if a user is a conversation moderator or not + * + * @param Integer $userID The ID of the user to check + * @param Integer $conversationID The ID of the conversation to check + * @return Boolean True if the user is a conversation moderator or not + */ + } //Register component diff --git a/functions/requests.php b/functions/requests.php index 6e92088..6aef9fb 100644 --- a/functions/requests.php +++ b/functions/requests.php @@ -45,7 +45,8 @@ function users_list_to_array($list) : array{ //Check the entry is valid if(toInt($process) < 1) - return array(); + //return array(); + continue; //Ignore entry //Add the entry to the list $usersList[toInt($process)] = toInt($process); diff --git a/index.php b/index.php index 59f0a0a..4d13cec 100644 --- a/index.php +++ b/index.php @@ -24,8 +24,11 @@ require PROJECT_PATH."3rdparty/RestServer/RestServer.php"; if(!isset($_GET["format"])) $_GET['format'] = "json"; +//Specify we are on Comunic API Server +header("Technology: Official Comunic API Server"); + //Set debug clients tokens -if($cs->config->get("site_mode") == "debug"){ //DEBUG ONLY +if($cs->config->get("site_mode") === "debug"){ //DEBUG ONLY $_POST['serviceName'] = "testService"; $_POST['serviceToken'] = "testPasswd"; }