mirror of
https://github.com/pierre42100/ComunicAPI
synced 2024-11-23 22:09:29 +00:00
Can get older messages of a conversation
This commit is contained in:
parent
957c308e49
commit
a7eb02aa06
@ -388,6 +388,40 @@ class ConversationsController{
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get older messages of a conversation
|
||||||
|
*
|
||||||
|
* @url POST /conversations/get_older_messages
|
||||||
|
*/
|
||||||
|
public function getOlderMessages(){
|
||||||
|
|
||||||
|
user_login_required();
|
||||||
|
|
||||||
|
//Get the ID of the conversation to refresh
|
||||||
|
$conversationID = $this->getSafePostConversationID("conversationID");
|
||||||
|
|
||||||
|
//Get the ID of the oldest message ID
|
||||||
|
$maxID = postInt("oldest_message_id") - 1;
|
||||||
|
|
||||||
|
//Get the limit
|
||||||
|
$limit = postInt("limit");
|
||||||
|
|
||||||
|
//Security
|
||||||
|
if($limit < 1)
|
||||||
|
$limit = 1;
|
||||||
|
if($limit > 30)
|
||||||
|
$limit = 30;
|
||||||
|
|
||||||
|
//Retrieve the list of messages in the database
|
||||||
|
$messages = components()->conversations->getOlderMessages($conversationID, $maxID, $limit);
|
||||||
|
|
||||||
|
//Process the list of messages
|
||||||
|
foreach($messages as $num => $process)
|
||||||
|
$messages[$num] = self::ConvMessageToAPI($process);
|
||||||
|
|
||||||
|
return $messages;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the number of unread conversations of the user
|
* Get the number of unread conversations of the user
|
||||||
*
|
*
|
||||||
@ -459,6 +493,29 @@ class ConversationsController{
|
|||||||
return array("success" => "The conversation has been deleted");
|
return array("success" => "The conversation has been deleted");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get and return safely a conversation ID specified in a $_POST Request
|
||||||
|
*
|
||||||
|
* This methods check the existence of the conversation, but also check if the
|
||||||
|
* user is a member or not of the conversation
|
||||||
|
*
|
||||||
|
* @param string $name The name of the $_POST field containing the conversation ID
|
||||||
|
* @return int The ID of the conversation
|
||||||
|
*/
|
||||||
|
private function getSafePostConversationID(string $name) : int {
|
||||||
|
|
||||||
|
user_login_required();
|
||||||
|
|
||||||
|
//Get the ID of the conversation
|
||||||
|
$conversationID = getPostConversationID("conversationID");
|
||||||
|
|
||||||
|
//Check if the user belongs to the conversation
|
||||||
|
if(!components()->conversations->userBelongsTo(userID, $conversationID))
|
||||||
|
Rest_fatal_error(401, "Specified user doesn't belongs to the conversation number ".$conversationID." !");
|
||||||
|
|
||||||
|
return $conversationID;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Turn a ConversationInfo object into a valid API entry
|
* Turn a ConversationInfo object into a valid API entry
|
||||||
*
|
*
|
||||||
|
@ -600,6 +600,34 @@ class Conversations {
|
|||||||
return $messages;
|
return $messages;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the older messages of a conversation
|
||||||
|
*
|
||||||
|
* @param int $conversationID The ID of the target conversation
|
||||||
|
* @param int $startID The ID from which the research should start
|
||||||
|
* @param int $limit The maximum number of message to get (positive number)
|
||||||
|
* @return array The list of messages found
|
||||||
|
*/
|
||||||
|
public function getOlderMessages(int $conversationID, int $startID, int $limit) : array {
|
||||||
|
|
||||||
|
//Define conditions
|
||||||
|
$conditions = "WHERE ID_".$this->conversationsListTable." = ? AND ID < ? ORDER BY ID DESC LIMIT ".($limit);
|
||||||
|
$condVals = array(
|
||||||
|
$conversationID,
|
||||||
|
$startID + 1
|
||||||
|
);
|
||||||
|
|
||||||
|
//Perform request
|
||||||
|
$messages = $this->getMessages($conditions, $condVals);
|
||||||
|
|
||||||
|
//Revert messages order
|
||||||
|
$messages = array_reverse($messages);
|
||||||
|
|
||||||
|
//Return messages
|
||||||
|
return $messages;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check whether a conversation exists or not
|
* Check whether a conversation exists or not
|
||||||
*
|
*
|
||||||
|
@ -82,7 +82,7 @@ function postString(string $name, int $minLength = 1) : string {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a boolean given in a $_POST request safely.
|
* Get a boolean given in a $_POST request safely.
|
||||||
* This function make a REST_Error if an error occur while
|
* This function makes a REST_Error if an error occurr while
|
||||||
* processing the value
|
* processing the value
|
||||||
*
|
*
|
||||||
* @param string $name The name of the $_POST field
|
* @param string $name The name of the $_POST field
|
||||||
@ -97,6 +97,22 @@ function postBool(string $name) : bool {
|
|||||||
return $_POST[$name] == "true";
|
return $_POST[$name] == "true";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get an integer given in a $_POST request safely
|
||||||
|
* This function makes a REST_Error in case of error
|
||||||
|
*
|
||||||
|
* @param string $name The name of the $_POST field
|
||||||
|
* @return int The integer
|
||||||
|
*/
|
||||||
|
function postInt(string $name) : int {
|
||||||
|
|
||||||
|
//Check the variable
|
||||||
|
if(!isset($_POST[$name]))
|
||||||
|
Rest_fatal_error(400, "Please add a POST integer named '".$name."' in the request !");
|
||||||
|
|
||||||
|
return (int)$_POST[$name];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Securely transform user given number (mixed) to integer (int)
|
* Securely transform user given number (mixed) to integer (int)
|
||||||
*
|
*
|
||||||
|
Loading…
Reference in New Issue
Block a user