Return recently opened conversations messages

This commit is contained in:
Pierre 2017-06-25 18:09:18 +02:00
parent 2872a84457
commit eed7db90cf
4 changed files with 63 additions and 8 deletions

View File

@ -72,7 +72,7 @@ class conversationsController{
//Extract parametres //Extract parametres
$conversationName = ($_POST["name"] == "false" ? false : $_POST['name']); $conversationName = ($_POST["name"] == "false" ? false : $_POST['name']);
$followConversation = ($_POST['follow'] == "true" ? true : false); $followConversation = ($_POST['follow'] == "true" ? true : false);
$usersList = users_list_to_array($_POST['users']); $usersList = numbers_list_to_array($_POST['users']);
//Add current user (if not present) //Add current user (if not present)
if(!isset($usersList[userID])) if(!isset($usersList[userID]))
@ -141,7 +141,7 @@ class conversationsController{
//Update conversation users (if required) //Update conversation users (if required)
if(isset($_POST["members"])){ if(isset($_POST["members"])){
//Get user list //Get user list
$conversationMembers = users_list_to_array($_POST['members']); $conversationMembers = numbers_list_to_array($_POST['members']);
//Make sure current user is in the list //Make sure current user is in the list
$conversationMembers[userID] = userID; $conversationMembers[userID] = userID;
@ -282,4 +282,34 @@ class conversationsController{
//Success //Success
return array("success" => "Conversation message with successfully added !"); return array("success" => "Conversation message with successfully added !");
} }
/**
* Refresh conversations
*
* @url POST /conversations/refresh
*/
public function refreshConversations(){
user_login_required();
//Prepare return
$conversationsMessages = array();
//Check if we have to give the latest messages of a conversation
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
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
$conversationsMessages[$conversationID] = CS::get()->components->conversations->getLastMessages($conversationID, 10);
}
}
//Return result
return $conversationsMessages;
}
} }

View File

@ -76,7 +76,7 @@ class userController
} }
elseif(isset($_POST['usersID'])){ elseif(isset($_POST['usersID'])){
//Generate users ID list //Generate users ID list
$usersID = users_list_to_array($_POST['usersID']); $usersID = numbers_list_to_array($_POST['usersID']);
//Check for errors //Check for errors
if(count($usersID) == 0) if(count($usersID) == 0)

View File

@ -20,7 +20,7 @@ class conversations {
/** /**
* @var String $conversationMessagesTabel Name of the conversation messages table * @var String $conversationMessagesTabel Name of the conversation messages table
*/ */
private $conversationMessagesTable; private $conversationsMessagesTable;
/** /**
@ -29,7 +29,7 @@ class conversations {
public function __construct(){ public function __construct(){
$this->conversationsListTable = CS::get()->config->get("dbprefix")."conversations_list"; $this->conversationsListTable = CS::get()->config->get("dbprefix")."conversations_list";
$this->conversationsUsersTable = CS::get()->config->get("dbprefix")."conversations_users"; $this->conversationsUsersTable = CS::get()->config->get("dbprefix")."conversations_users";
$this->conversationMessagesTable = CS::get()->config->get("dbprefix")."conversations_messages"; $this->conversationsMessagesTable = CS::get()->config->get("dbprefix")."conversations_messages";
} }
/** /**
@ -429,7 +429,7 @@ class conversations {
private function insertMessage($userID, $conversationID, $message, $image_path = false){ private function insertMessage($userID, $conversationID, $message, $image_path = false){
//Prepare values //Prepare values
$tableName = $this->conversationMessagesTable; $tableName = $this->conversationsMessagesTable;
$values = array( $values = array(
"ID_".$this->conversationsListTable => $conversationID, "ID_".$this->conversationsListTable => $conversationID,
"ID_utilisateurs" => $userID, "ID_utilisateurs" => $userID,
@ -539,6 +539,31 @@ class conversations {
return true; return true;
} }
/**
* Get the last messages of a conversation
*
* @param Integer $conversationID The ID of the target conversation
* @param Integer $numberOfMessages The number of messages to return
* @return Array The messages of the conversation
*/
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
);
//Try to perform request on the database
$messages = CS::get()->db->select($tableName, $conditions, $condVals);
//Return messages
return $messages;
}
} }
//Register component //Register component

View File

@ -31,12 +31,12 @@ function check_post_parametres(array $varList){
} }
/** /**
* Convert a list of user comma-separated to an array * Convert a list of numbers (anything with IDs) comma-separated to an array
* *
* @param String $list The input list * @param String $list The input list
* @return Array The list of user / an empty list in case of errors * @return Array The list of user / an empty list in case of errors
*/ */
function users_list_to_array($list) : array{ function numbers_list_to_array($list) : array{
//Split the list into an array //Split the list into an array
$array = explode(",", $list); $array = explode(",", $list);
$usersList = array(); $usersList = array();