Upgraded messages creation

This commit is contained in:
Pierre 2018-04-24 20:31:06 +02:00
parent 829e21d65a
commit 0928248ae3
3 changed files with 43 additions and 21 deletions

View File

@ -236,7 +236,7 @@ class ConversationsController{
Rest_fatal_error(401, "New conversation messages must contain a message (can be empty if there is an image) !"); Rest_fatal_error(401, "New conversation messages must contain a message (can be empty if there is an image) !");
//Else extract informations //Else extract informations
$message = (string) (isset($_POST['message']) ? $_POST['message'] : ""); $content = (string) (isset($_POST['message']) ? $_POST['message'] : "");
//Check for image //Check for image
$image = ""; $image = "";
@ -248,11 +248,16 @@ class ConversationsController{
} }
//Check message validity //Check message validity
if(!check_string_before_insert($message) && $image == "") if(!check_string_before_insert($content) && $image == "")
Rest_fatal_error(401, "Invalid message creation request !"); Rest_fatal_error(401, "Invalid message creation request !");
//Insert the new message //Insert the new message
if(!CS::get()->components->conversations->sendMessage(userID, $conversationID, $message, $image)) $newMessage = new NewConversationMessage();
$newMessage->set_userID(userID);
$newMessage->set_conversationID($conversationID);
$newMessage->set_message($content);
$newMessage->set_image_path($image);
if(!CS::get()->components->conversations->sendMessage($newMessage))
Rest_fatal_error(500, "Couldn't send the message !"); Rest_fatal_error(500, "Couldn't send the message !");
//Success //Success

View File

@ -409,26 +409,23 @@ class Conversations {
/** /**
* Insert a new message in the database * Insert a new message in the database
* *
* @param int $userID The ID of the user inserting the message * @param NewConversationMessage $message The message to send
* @param int $conversationID The ID of the target conversation
* @param string $message The message to insert
* @param string $image_path Optionnal, the path to an image associated with the message (empty string by default)
* @return bool True for a success * @return bool True for a success
*/ */
private function insertMessage(int $userID, int $conversationID, string $message, string $image_path = "") : bool{ private function insertMessage(NewConversationMessage $message) : bool {
//Prepare values //Prepare values
$tableName = $this->conversationsMessagesTable; $tableName = $this->conversationsMessagesTable;
$values = array( $values = array(
"ID_".$this->conversationsListTable => $conversationID, "ID_".$this->conversationsListTable => $message->get_conversationID(),
"ID_utilisateurs" => $userID, "ID_utilisateurs" => $message->get_userID(),
"time_insert" => time(), "time_insert" => time(),
"message" => $message "message" => $message->has_message() ? $message->get_message() : ""
); );
//Add image path (if required) //Add image path (if required)
if($image_path != "") if($message->has_image_path())
$values['image_path'] = $image_path; $values['image_path'] = $message->get_image_path();
//Try to insert new value in database //Try to insert new value in database
if(!CS::get()->db->addLine($tableName, $values)) if(!CS::get()->db->addLine($tableName, $values))
@ -531,27 +528,24 @@ class Conversations {
/** /**
* Send a new message * Send a new message
* *
* @param int $userID The ID of the user sending the message * @param NewConversationMessage $message The message to send
* @param int $conversationID The ID of the target conversation
* @param string $message The message
* @param string $image_path Optionnal, define the path to an image associated with the message (empty string by default)
* @return bool True for a success * @return bool True for a success
*/ */
public function sendMessage(int $userID, int $conversationID, string $message, string $image_path = "") : bool{ public function sendMessage(NewConversationMessage $message) : bool{
//GUIDE LINE : this method act like a "controller" : it doesn't perform any database operation //GUIDE LINE : this method act like a "controller" : it doesn't perform any database operation
//But it manages 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 //First, try to insert the message
if(!$this->insertMessage($userID, $conversationID, $message, $image_path)) if(!$this->insertMessage($message))
return false; //An error occured return false; //An error occured
//Then, update the last activity of the conversation //Then, update the last activity of the conversation
if(!$this->updateLastActive($conversationID, time())) if(!$this->updateLastActive($message->get_conversationID(), time()))
return false; //An error occured (2) return false; //An error occured (2)
//Then, set all the users of the conversation as unread //Then, set all the users of the conversation as unread
if(!$this->allUsersAsUnread($conversationID, array($userID))) if(!$this->allUsersAsUnread($message->get_conversationID(), array($message->get_userID())))
return false; //An error occured (3) return false; //An error occured (3)
//Success //Success

View File

@ -0,0 +1,23 @@
<?php
/**
* New conversation message object
*
* @author Pierre HUBERT
*/
class NewConversationMessage extends ConversationMessage {
//Private fields
private $conversationID;
//Get and set conversation ID
public function set_conversationID(int $conversationID){
$this->conversationID = $conversationID;
}
public function get_conversationID() : int {
return $this->conversationID;
}
}