Can get informations of a specific conversation

This commit is contained in:
Pierre 2017-06-16 19:08:58 +02:00
parent 93d5ec8044
commit b1e340e0c1
3 changed files with 108 additions and 26 deletions

View File

@ -26,6 +26,37 @@ class conversationsController{
return $conversationsList; return $conversationsList;
} }
/**
* Get informationsd about one conversation
*
* @url POST /conversations/getInfosOne
*/
public function getOneConversationInformations(){
user_login_required();
//First, check the parametres
if(!isset($_POST['conversationID']))
Rest_fatal_error(501, "No conversation ID specified with the request");
//Extract data
$conversationID = toInt($_POST['conversationID']);
//Try to get informations about the conversation
$conversationsList = CS::get()->components->conversations->getList(userID, $conversationID);
//Check for errors
if($conversationsList === false)
Rest_fatal_error(500, "An internal error occured");
//Check if a conversation was found
if(count($conversationsList) < 1)
Rest_fatal_error(401, "Users doesn't belong to the specified conversation,".
"or the conversation doesn't exists !");
//Return conversation informations
return $conversationsList[0];
}
/** /**
* Create a new conversation * Create a new conversation
* *

View File

@ -8,52 +8,64 @@
class conversations { class conversations {
/** /**
* @var String $conversationListTable Name of the conversation list table * @var String $conversationsListTable Name of the conversation list table
*/ */
private $conversationListTable; private $conversationsListTable;
/** /**
* @var String $conversationUsersTable Name of the conversation users table * @var String $conversationsUsersTable Name of the conversation users table
*/ */
private $conversationUsersTable; private $conversationsUsersTable;
/** /**
* Public constructor * Public constructor
*/ */
public function __construct(){ public function __construct(){
$this->conversationListTable = CS::get()->config->get("dbprefix")."conversations_list"; $this->conversationsListTable = CS::get()->config->get("dbprefix")."conversations_list";
$this->conversationUsersTable = CS::get()->config->get("dbprefix")."conversations_users"; $this->conversationsUsersTable = CS::get()->config->get("dbprefix")."conversations_users";
} }
/** /**
* Get the conversations list of a specified user * Get the conversations list of a specified user
* or get informations of a specific conversation
* *
* @param Integer $userID The ID of the user to get the list * @param Integer $userID The ID of the user to get the list
* @param Integer $conversationID Optionnal, the ID of conversation to get informatios from
* @return Mixed Array in case of result / False else * @return Mixed Array in case of result / False else
*/ */
public function getList($userID){ public function getList($userID, $conversationID = 0){
//Prepare database request //Prepare database request
$tablesName = $this->conversationListTable.", ".$this->conversationUsersTable; $tablesName = $this->conversationsListTable.", ".$this->conversationsUsersTable;
//Prepare conditions //Prepare conditions
$tableJoinCondition = $this->conversationListTable.".ID = ".$this->conversationUsersTable.".ID_".$this->conversationListTable.""; $tableJoinCondition = $this->conversationsListTable.".ID = ".$this->conversationsUsersTable.".ID_".$this->conversationsListTable."";
$userCondition = $this->conversationUsersTable.".ID_utilisateurs = ?"; $userCondition = $this->conversationsUsersTable.".ID_utilisateurs = ?";
$orderResults = "ORDER BY ".$this->conversationListTable.".last_active DESC"; $orderResults = "ORDER BY ".$this->conversationsListTable.".last_active DESC";
//Specify conditions values
$conditionsValues = array($userID);
//Check if we have to get informations about just one conversation
if($conversationID != 0){
$specificConditions = "AND ".$this->conversationsListTable.".ID = ?";
$conditionsValues[] = $conversationID;
}
else
$specificConditions = ""; //Nothing now
//Compile conditions //Compile conditions
$conditions = "WHERE ".$tableJoinCondition." AND (".$userCondition.") ".$orderResults; $conditions = "WHERE ".$tableJoinCondition." AND (".$userCondition.") ".$specificConditions." ".$orderResults;
$conditionsValues = array($userID);
//Fields list //Fields list
$requiredFields = array( $requiredFields = array(
$this->conversationListTable.".ID", $this->conversationsListTable.".ID",
$this->conversationListTable.".last_active", $this->conversationsListTable.".last_active",
$this->conversationListTable.".name", $this->conversationsListTable.".name",
$this->conversationListTable.".ID_utilisateurs AS ID_owner", $this->conversationsListTable.".ID_utilisateurs AS ID_owner",
$this->conversationUsersTable.".following", $this->conversationsUsersTable.".following",
$this->conversationUsersTable.".saw_last_message", $this->conversationsUsersTable.".saw_last_message",
); );
//Perform database request //Perform database request
@ -83,6 +95,7 @@ class conversations {
return $conversationsList; return $conversationsList;
} }
/** /**
* Get a conversation members * Get a conversation members
* *
@ -92,8 +105,8 @@ class conversations {
public function getConversationMembers($conversationID) : array { public function getConversationMembers($conversationID) : array {
//Perform a request on the database //Perform a request on the database
$tableName = $this->conversationUsersTable; $tableName = $this->conversationsUsersTable;
$conditions = "WHERE ID_".$this->conversationListTable." = ?"; $conditions = "WHERE ID_".$this->conversationsListTable." = ?";
$conditionsValues = array($conversationID*1); $conditionsValues = array($conversationID*1);
$getFields = array("ID_utilisateurs as userID"); $getFields = array("ID_utilisateurs as userID");
@ -132,7 +145,7 @@ class conversations {
); );
//First, insert the conversation in the main table //First, insert the conversation in the main table
if(!CS::get()->db->addLine($this->conversationListTable, $mainInformations)) if(!CS::get()->db->addLine($this->conversationsListTable, $mainInformations))
return 0; //An error occured return 0; //An error occured
//Get the last inserted ID //Get the last inserted ID
@ -147,7 +160,7 @@ class conversations {
//Prepare informations about the user //Prepare informations about the user
$userInformations = array( $userInformations = array(
"ID_".$this->conversationListTable => $conversationID, "ID_".$this->conversationsListTable => $conversationID,
"time_add" => time(), "time_add" => time(),
"saw_last_message" => 1, "saw_last_message" => 1,
"ID_utilisateurs" => $processUser, "ID_utilisateurs" => $processUser,
@ -158,7 +171,7 @@ class conversations {
$userInformations["following"] = ($follow ? 1 : 0); $userInformations["following"] = ($follow ? 1 : 0);
//Try to insert user in conversation //Try to insert user in conversation
if(!CS::get()->db->addLine($this->conversationUsersTable, $userInformations)) if(!CS::get()->db->addLine($this->conversationsUsersTable, $userInformations))
return 0; //Error return 0; //Error
} }
@ -166,6 +179,34 @@ class conversations {
return $conversationID; return $conversationID;
} }
/**
* Check if a user is a member of a conversation 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 belongs to the conversation
*/
public function userBelongsTo($userID, $conversationID){
//Prepare a request on the database
$tableName = $this->conversationsUsersTable;
$conditions = "WHERE ID_".$this->conversationsListTable." = ? AND ID_utilisateurs = ?";
$values = array(
$conversationID,
$userID
);
//Peform a request on the database
$result = CS::get()->db->count($tableName, $conditions, $values);
//Check if request failed
if($result === false)
return false; // An error occured
//Analyse result and return it
return $result != 0;
}
} }
//Register component //Register component

View File

@ -44,13 +44,23 @@ function users_list_to_array($list) : array{
foreach($array as $process){ foreach($array as $process){
//Check the entry is valid //Check the entry is valid
if(floor($process*1) < 1) if(toInt($process) < 1)
return array(); return array();
//Add the entry to the list //Add the entry to the list
$usersList[floor($process*1)] = floor($process*1); $usersList[toInt($process)] = toInt($process);
} }
//Return the result //Return the result
return $usersList; return $usersList;
}
/**
* Securely transform user given number (mixed) to integer (int)
*
* @param Mixed $input The input variable (mixed)
* @return Integer $output The output (safe integer)
*/
function toInt($input){
return floor($input*1);
} }