mirror of
				https://github.com/pierre42100/ComunicAPI
				synced 2025-11-04 04:04:20 +00:00 
			
		
		
		
	Ready to implement sendMessage method
This commit is contained in:
		@@ -109,7 +109,7 @@ class conversationsController{
 | 
			
		||||
			Rest_fatal_error(400, "Please specify a conversation ID !");
 | 
			
		||||
		$conversationID = toInt($_POST["conversationID"]);
 | 
			
		||||
 | 
			
		||||
		//Check if the user is a conversation moderator or not
 | 
			
		||||
		//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 !");
 | 
			
		||||
 | 
			
		||||
@@ -210,4 +210,44 @@ class conversationsController{
 | 
			
		||||
		//Success
 | 
			
		||||
		return array("conversationsID" => $results); 
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Send a new message
 | 
			
		||||
	 *
 | 
			
		||||
	 * @url POST /conversations/sendMessage
 | 
			
		||||
	 */
 | 
			
		||||
	public function sendMessage(){
 | 
			
		||||
		user_login_required();
 | 
			
		||||
 | 
			
		||||
		//First, check a conversation ID was specified
 | 
			
		||||
		if(!isset($_POST["conversationID"]))
 | 
			
		||||
			Rest_fatal_errror(400, "Please speicify a conversation ID !");
 | 
			
		||||
 | 
			
		||||
		//Extract conversation ID
 | 
			
		||||
		$conversationID = toInt($_POST["conversationID"]);
 | 
			
		||||
 | 
			
		||||
		//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 !");
 | 
			
		||||
 | 
			
		||||
		//Check if informations were specified about the new message or not
 | 
			
		||||
		if(!isset($_POST['message']) AND !isset($_POST['image']))
 | 
			
		||||
			Rest_fatal_error(401, "Nothing to be sent with the new message !");
 | 
			
		||||
		
 | 
			
		||||
		//Else extract informations
 | 
			
		||||
		$message = (isset($_POST['message']) ? $_POST['message'] : "");
 | 
			
		||||
		$image = (isset($_POST['image']) ? $_POST['image'] : false);
 | 
			
		||||
 | 
			
		||||
		//Check message validity
 | 
			
		||||
		if(!check_string_before_insert($message) && !$image)
 | 
			
		||||
			Rest_fatal_error(401, "Invalid message sending request !");
 | 
			
		||||
 | 
			
		||||
		//Process images NOT IMPLEMENTED YET
 | 
			
		||||
 | 
			
		||||
		//Insert the new message
 | 
			
		||||
		if(!CS::get()->components->conversations->sendMessage(userID, $conversationID, $message))
 | 
			
		||||
			Rest_fatal_error(500, "Couldn't send the message !");
 | 
			
		||||
		
 | 
			
		||||
		Rest_fatal_error("200", "All right now");
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@@ -411,6 +411,23 @@ class conversations {
 | 
			
		||||
		return $conversationsID;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Send a new message
 | 
			
		||||
	 *
 | 
			
		||||
	 * @param Integer $userID The ID of the user sending the message
 | 
			
		||||
	 * @param Integer $conversationID The ID of the target conversation
 | 
			
		||||
	 * @param String $message The message
 | 
			
		||||
	 * @return Boolean True for a success
 | 
			
		||||
	 */
 | 
			
		||||
	 public function sendMessage($userID, $conversationID, $message){
 | 
			
		||||
 | 
			
		||||
		//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; ...)
 | 
			
		||||
 | 
			
		||||
		 //Success
 | 
			
		||||
		 return true;
 | 
			
		||||
	 }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
//Register component
 | 
			
		||||
 
 | 
			
		||||
@@ -64,4 +64,42 @@ function users_list_to_array($list) : array{
 | 
			
		||||
 */
 | 
			
		||||
function toInt($input){
 | 
			
		||||
	return floor($input*1);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Remove HTML markup codes (<, >)
 | 
			
		||||
 *
 | 
			
		||||
 * @param String $input The string to change
 | 
			
		||||
 * @return String The updated string
 | 
			
		||||
 */
 | 
			
		||||
function removeHTMLnodes($input){
 | 
			
		||||
	$output = str_replace("<", "<", $input);
 | 
			
		||||
	return str_replace(">", ">", $output);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Check a string before inserting it
 | 
			
		||||
 *
 | 
			
		||||
 * @param String $string The string to check
 | 
			
		||||
 * @return Boolean True if the string is valid / false else
 | 
			
		||||
 */
 | 
			
		||||
function check_string_before_insert($string){
 | 
			
		||||
 | 
			
		||||
	//First, empty string are invalid
 | 
			
		||||
	if($string == "")
 | 
			
		||||
		return false;
 | 
			
		||||
	
 | 
			
		||||
	//Remove HTML tags before continuing
 | 
			
		||||
	$string = str_replace(array("<", ">"), "", $string);
 | 
			
		||||
 | 
			
		||||
	//Check string size
 | 
			
		||||
	if(strlen($string)<5)
 | 
			
		||||
		return false;
 | 
			
		||||
 | 
			
		||||
	//Check if the string has at least three different characters
 | 
			
		||||
	if(strlen(count_chars($string,3)) < 3)
 | 
			
		||||
		return false; 
 | 
			
		||||
	
 | 
			
		||||
	//Success
 | 
			
		||||
	return true;
 | 
			
		||||
}
 | 
			
		||||
@@ -1,6 +1,6 @@
 | 
			
		||||
<?php
 | 
			
		||||
/**
 | 
			
		||||
 * API specific methods
 | 
			
		||||
 * Strings methods
 | 
			
		||||
 *
 | 
			
		||||
 * @author Pierre HUBERT
 | 
			
		||||
 */
 | 
			
		||||
@@ -22,4 +22,4 @@ function random_str($length, $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzAB
 | 
			
		||||
        $str .= $keyspace[random_int(0, $max)];
 | 
			
		||||
    }
 | 
			
		||||
    return $str;
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user