mirror of
https://github.com/pierre42100/ComunicAPI
synced 2024-11-27 15:59:29 +00:00
Conversations can be refreshed
This commit is contained in:
parent
df1808c6c3
commit
46985a77f9
@ -294,21 +294,57 @@ class conversationsController{
|
|||||||
//Prepare return
|
//Prepare return
|
||||||
$conversationsMessages = array();
|
$conversationsMessages = array();
|
||||||
|
|
||||||
//Check if we have to give the latest messages of a conversation
|
//Check if we have to give the latest messages of some conversations
|
||||||
if(isset($_POST['newConversations'])){
|
if(isset($_POST['newConversations'])){
|
||||||
//Get conversations ID
|
//Get conversations ID
|
||||||
$newConversations = numbers_list_to_array($_POST['newConversations']);
|
$newConversations = numbers_list_to_array($_POST['newConversations']);
|
||||||
|
|
||||||
foreach($newConversations as $conversationID){
|
foreach($newConversations as $conversationID){
|
||||||
//First, check the users belongs to the conversation
|
//First, check the user belongs to the conversation
|
||||||
if(!CS::get()->components->conversations->userBelongsTo(userID, $conversationID))
|
if(!CS::get()->components->conversations->userBelongsTo(userID, $conversationID))
|
||||||
Rest_fatal_error(401, "Specified user doesn't belongs to the conversation number ".$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
|
//Then we can get the ten last messages of the conversation system
|
||||||
$conversationsMessages["conversation-".$conversationID] = CS::get()->components->conversations->getLastMessages($conversationID, 10);
|
$conversationsMessages["conversation-".$conversationID] = CS::get()->components->conversations->getLastMessages($conversationID, 10);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Check if we have to refresh some conversations
|
||||||
|
if(isset($_POST['toRefresh'])){
|
||||||
|
|
||||||
|
//Try to decode informations about the conversations
|
||||||
|
$toRefresh = json_decode($_POST['toRefresh'], true);
|
||||||
|
if($toRefresh === false)
|
||||||
|
Rest_fatal_error(400, "Couldn't get refresh conversations informations !");
|
||||||
|
|
||||||
|
//Process each conversation
|
||||||
|
foreach($toRefresh as $conversationID=>$informations){
|
||||||
|
|
||||||
|
//Get conversation ID
|
||||||
|
$conversationID = toInt(str_replace("conversation-", "", $conversationID));
|
||||||
|
|
||||||
|
//Check if the conversation is not a new conversation too
|
||||||
|
if(isset($conversationsMessages["conversation-".$conversationID]))
|
||||||
|
Rest_fatal_error(401, "Conversation marked as new can't be refreshed !");
|
||||||
|
|
||||||
|
//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);
|
||||||
|
|
||||||
|
//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");
|
||||||
|
$last_message_id = toInt($informations["last_message_id"]);
|
||||||
|
|
||||||
|
//Check if the user 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 informations about the conversation
|
||||||
|
$conversationsMessages["conversation-".$conversationID] = CS::get()->components->conversations->getNewMessages($conversationID, $last_message_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//Return result
|
//Return result
|
||||||
return $conversationsMessages;
|
return $conversationsMessages;
|
||||||
}
|
}
|
||||||
|
@ -548,15 +548,57 @@ class conversations {
|
|||||||
*/
|
*/
|
||||||
public function getLastMessages($conversationID, $numberOfMessages) : array {
|
public function getLastMessages($conversationID, $numberOfMessages) : array {
|
||||||
|
|
||||||
//Prepare database request
|
|
||||||
$tableName = $this->conversationsMessagesTable;
|
|
||||||
|
|
||||||
//Define conditions
|
//Define conditions
|
||||||
$conditions = "WHERE ID_".$this->conversationsListTable." = ? ORDER BY ID DESC LIMIT ".($numberOfMessages*1);
|
$conditions = "WHERE ID_".$this->conversationsListTable." = ? ORDER BY ID DESC LIMIT ".($numberOfMessages*1);
|
||||||
$condVals = array(
|
$condVals = array(
|
||||||
$conversationID
|
$conversationID
|
||||||
);
|
);
|
||||||
|
|
||||||
|
//Perform request
|
||||||
|
$messages = $this->getMessages($conditions, $condVals);
|
||||||
|
|
||||||
|
//Revert messages order
|
||||||
|
$messages = array_reverse($messages);
|
||||||
|
|
||||||
|
//Return messages
|
||||||
|
return $messages;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the new messages of a conversation
|
||||||
|
*
|
||||||
|
* @param Integer $conversationID The ID of the target conversation
|
||||||
|
* @param Integer $lastMessageID The ID of the last know message
|
||||||
|
* @return Array A list of messages
|
||||||
|
*/
|
||||||
|
public function getNewMessages($conversationID, $lastMessageID) : array {
|
||||||
|
|
||||||
|
//Define conditions
|
||||||
|
$conditions = "WHERE ID_".$this->conversationsListTable." = ? AND ID > ? ORDER BY ID";
|
||||||
|
$condVals = array(
|
||||||
|
$conversationID,
|
||||||
|
$lastMessageID
|
||||||
|
);
|
||||||
|
|
||||||
|
//Perform request
|
||||||
|
$messages = $this->getMessages($conditions, $condVals);
|
||||||
|
|
||||||
|
//Return messages
|
||||||
|
return $messages;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get a list of conversation messages based on specified conditions
|
||||||
|
*
|
||||||
|
* @param String $conditions The conditions of the request
|
||||||
|
* @param Array $conditionsValues The values of the conditions (Optionnal)
|
||||||
|
* @return Array The list of messages
|
||||||
|
*/
|
||||||
|
private function getMessages($conditions, $conditionsValues = array()) : array{
|
||||||
|
|
||||||
|
//Prepare database request
|
||||||
|
$tableName = $this->conversationsMessagesTable;
|
||||||
|
|
||||||
//Define required fields
|
//Define required fields
|
||||||
$requiredFields = array(
|
$requiredFields = array(
|
||||||
"ID",
|
"ID",
|
||||||
@ -567,21 +609,19 @@ class conversations {
|
|||||||
);
|
);
|
||||||
|
|
||||||
//Try to perform request on the database
|
//Try to perform request on the database
|
||||||
$messages = CS::get()->db->select($tableName, $conditions, $condVals, $requiredFields);
|
$messages = CS::get()->db->select($tableName, $conditions, $conditionsValues, $requiredFields);
|
||||||
|
|
||||||
//Process each message
|
//Process each message
|
||||||
array_walk($messages, function(&$item){
|
array_walk($messages, function(&$item){
|
||||||
//Check if image is not null
|
|
||||||
|
//Check if the image of the message is not null
|
||||||
if($item["image_path"] !== null && $item["image_path"] != ""){
|
if($item["image_path"] !== null && $item["image_path"] != ""){
|
||||||
//Replace image name with full URL
|
//Replace image name with full URL
|
||||||
$item["image_path"] = path_user_data($item["image_path"]);
|
$item["image_path"] = path_user_data($item["image_path"]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
//Revert messages order
|
//Return result
|
||||||
$messages = array_reverse($messages);
|
|
||||||
|
|
||||||
//Return messages
|
|
||||||
return $messages;
|
return $messages;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user