mirror of
https://github.com/pierre42100/ComunicAPI
synced 2024-11-27 07:49:27 +00:00
API use ConversationMessage object
This commit is contained in:
parent
f39d162277
commit
829e21d65a
@ -327,6 +327,14 @@ class ConversationsController{
|
||||
}
|
||||
}
|
||||
|
||||
//Process results
|
||||
foreach($conversationsMessages as $name=>$values){
|
||||
|
||||
foreach($conversationsMessages[$name] as $num => $message)
|
||||
$conversationsMessages[$name][$num] = ConversationsController::ConvMessageToAPI($message);
|
||||
|
||||
}
|
||||
|
||||
//Return result
|
||||
return $conversationsMessages;
|
||||
}
|
||||
@ -367,6 +375,10 @@ class ConversationsController{
|
||||
//Specify that user has seen last messages
|
||||
CS::get()->components->conversations->markUserAsRead(userID, $conversationID);
|
||||
|
||||
//Process the list of messages
|
||||
foreach($messages as $num => $val)
|
||||
$messages[$num] = ConversationsController::ConvMessageToAPI($val);
|
||||
|
||||
//Return the messges
|
||||
return $messages;
|
||||
|
||||
@ -475,7 +487,7 @@ class ConversationsController{
|
||||
$data["ID_user"] = $message->get_userID();
|
||||
$data["time_insert"] = $message->get_time_sent();
|
||||
$data["message"] = $message->has_message() ? $message->get_message() : "";
|
||||
$data["image_path"] = $message->has_image_path() ? $message->get_image_path() : null;
|
||||
$data["image_path"] = $message->has_image_path() ? $message->get_image_url() : null;
|
||||
|
||||
return $data;
|
||||
|
||||
|
@ -540,7 +540,7 @@ class Conversations {
|
||||
public function sendMessage(int $userID, int $conversationID, string $message, string $image_path = "") : bool{
|
||||
|
||||
//GUIDE LINE : this method act like a "controller" : it doesn't perform any database operation
|
||||
//But it manage all operations (insert message; save image; inform other users; ...)
|
||||
//But it manages all operations (insert message; save image; inform other users; ...)
|
||||
|
||||
//First, try to insert the message
|
||||
if(!$this->insertMessage($userID, $conversationID, $message, $image_path))
|
||||
@ -636,7 +636,7 @@ class Conversations {
|
||||
|
||||
//Delete each message
|
||||
foreach($messages as $message){
|
||||
if(!$this->delete_message($message['ID'], $convID))
|
||||
if(!$this->delete_message($message))
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -672,7 +672,7 @@ class Conversations {
|
||||
|
||||
//Delete each message
|
||||
foreach($messages as $message){
|
||||
if(!$this->delete_message($message['ID'], $convID))
|
||||
if(!$this->delete_message($message))
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -688,28 +688,16 @@ class Conversations {
|
||||
/**
|
||||
* Delete a single message of a conversation
|
||||
*
|
||||
* @param int $messageID The ID of the message to delete
|
||||
* @param int $convID The target conversation
|
||||
* @param array $informations Optionnal, informations about the message
|
||||
* @param ConversationMessage $message The message to delete
|
||||
* @return bool True in case of success / false else
|
||||
*/
|
||||
private function delete_message(int $messageID, int $convID, array $informations = null) : bool {
|
||||
|
||||
//Check if we have to fetch informations about the message
|
||||
if(is_null($informations)){
|
||||
$messages = $this->getMessages("WHERE ID_".$this->conversationsListTable." = ? AND ID = ?", array($convID, $messageID), false);
|
||||
|
||||
if(count($messages) == 0)
|
||||
return false;
|
||||
|
||||
$informations = $messages[0];
|
||||
}
|
||||
private function delete_message(ConversationMessage $message) : bool {
|
||||
|
||||
//Check if we have to delete an image
|
||||
if(!is_null($informations["image_path"]) AND $informations["image_path"] !== ""){
|
||||
if($message->has_image_path()){
|
||||
|
||||
//Get system path of the image
|
||||
$img_sys_path = path_user_data($informations["image_path"], true);
|
||||
$img_sys_path = path_user_data($message->get_image_path(), true);
|
||||
|
||||
if(file_exists($img_sys_path)){
|
||||
unlink($img_sys_path);
|
||||
@ -719,7 +707,7 @@ class Conversations {
|
||||
|
||||
//Delete message from the database
|
||||
$conditions = "ID = ?";
|
||||
$condValues = array($messageID);
|
||||
$condValues = array($message->get_id());
|
||||
return CS::get()->db->deleteEntry($this->conversationsMessagesTable, $conditions, $condValues);
|
||||
|
||||
}
|
||||
@ -811,11 +799,10 @@ class Conversations {
|
||||
* 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)
|
||||
* @param bool $transformPath Transform the path of the files into URLS (true by default)
|
||||
* @return Array The list of messages
|
||||
* @param array $conditionsValues The values of the conditions (Optionnal)
|
||||
* @return array The list of messages as ConversationMessage objects
|
||||
*/
|
||||
private function getMessages(string $conditions, array $conditionsValues = array(), bool $transformPath = true) : array{
|
||||
private function getMessages(string $conditions, array $conditionsValues = array()) : array{
|
||||
|
||||
//Prepare database request
|
||||
$tableName = $this->conversationsMessagesTable;
|
||||
@ -833,16 +820,8 @@ class Conversations {
|
||||
$messages = CS::get()->db->select($tableName, $conditions, $conditionsValues, $requiredFields);
|
||||
|
||||
//Process each message
|
||||
if($transformPath){
|
||||
array_walk($messages, function(&$item){
|
||||
|
||||
//Check if the image of the message is not null
|
||||
if($item["image_path"] !== null && $item["image_path"] != ""){
|
||||
//Replace image name with full URL
|
||||
$item["image_path"] = path_user_data($item["image_path"]);
|
||||
}
|
||||
});
|
||||
}
|
||||
foreach($messages as $num=>$message)
|
||||
$messages[$num] = self::dbToConvMessage($message);
|
||||
|
||||
//Return result
|
||||
return $messages;
|
||||
@ -871,6 +850,28 @@ class Conversations {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn a conversation message into ConversationMessage object
|
||||
*
|
||||
* @param array $entry Conversation entry in the database
|
||||
* @return ConversationMessage Generated ConversationMessage object
|
||||
*/
|
||||
private function dbToConvMessage(array $entry) : ConversationMessage {
|
||||
|
||||
$message = new ConversationMessage();
|
||||
|
||||
$message->set_id($entry["ID"]);
|
||||
$message->set_userID($entry["ID_user"]);
|
||||
$message->set_time_sent($entry["time_insert"]);
|
||||
if($entry["image_path"] != null)
|
||||
$message->set_image_path($entry["image_path"]);
|
||||
if($entry["message"] != null)
|
||||
$message->set_message($entry["message"]);
|
||||
|
||||
return $message;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//Register component
|
||||
|
@ -24,6 +24,10 @@ class ConversationMessage extends BaseUniqueObjectFromUser {
|
||||
return $this->image_path != null ? $this->image_path : "null";
|
||||
}
|
||||
|
||||
public function get_image_url() : string {
|
||||
return path_user_data($this->get_image_path());
|
||||
}
|
||||
|
||||
|
||||
//Set and get message
|
||||
public function set_message(string $message){
|
||||
|
Loading…
Reference in New Issue
Block a user