mirror of
				https://github.com/pierre42100/ComunicAPI
				synced 2025-10-30 17:54:59 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			99 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			99 lines
		
	
	
		
			2.6 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| /**
 | |
|  * Conversations controller
 | |
|  *
 | |
|  * @author Pierre HUBERT
 | |
|  */
 | |
| 
 | |
| class conversationsController{
 | |
| 
 | |
| 	/**
 | |
| 	 * Get the conversations list
 | |
| 	 *
 | |
| 	 * @url POST /conversations/getList
 | |
| 	 */
 | |
| 	public function getConversationsList(){
 | |
| 		user_login_required();
 | |
| 
 | |
| 		//Try to get the list
 | |
| 		$conversationsList = CS::get()->components->conversations->getList(userID);
 | |
| 
 | |
| 		//Check for errors
 | |
| 		if($conversationsList === false)
 | |
| 			Rest_fatal_error(500, "Couldn't get conversations list !");
 | |
| 		
 | |
| 		//Return results
 | |
| 		return $conversationsList;
 | |
| 	}
 | |
| 
 | |
| 	/**
 | |
| 	 * Get informationsd about one conversation
 | |
| 	 *
 | |
| 	 * @url POST /conversations/getInfosOne
 | |
| 	 */
 | |
| 	public function getOneConversationInformations(){
 | |
| 		user_login_required();
 | |
| 
 | |
| 		//First, check the parametres
 | |
| 		if(!isset($_POST['conversationID']))
 | |
| 			Rest_fatal_error(501, "No conversation ID specified with the request");
 | |
| 		
 | |
| 		//Extract data
 | |
| 		$conversationID = toInt($_POST['conversationID']);
 | |
| 		
 | |
| 		//Try to get informations about the conversation
 | |
| 		$conversationsList = CS::get()->components->conversations->getList(userID, $conversationID);
 | |
| 
 | |
| 		//Check for errors
 | |
| 		if($conversationsList === false)
 | |
| 			Rest_fatal_error(500, "An internal error occured");
 | |
| 
 | |
| 		//Check if a conversation was found
 | |
| 		if(count($conversationsList) < 1)
 | |
| 			Rest_fatal_error(401, "Users doesn't belong to the specified conversation,".
 | |
| 			"or the conversation doesn't exists !");
 | |
| 
 | |
| 		//Return conversation informations
 | |
| 		return $conversationsList[0];
 | |
| 	}
 | |
| 
 | |
| 	/**
 | |
| 	 * Create a new conversation
 | |
| 	 *
 | |
| 	 * @url POST /conversations/create
 | |
| 	 */
 | |
| 	public function createConversation(){
 | |
| 		user_login_required();
 | |
| 
 | |
| 		//Check for parametres
 | |
| 		if(!check_post_parametres(array("name", "follow", "users")))
 | |
| 			Rest_fatal_error(501, "Please check parametres passed with the request !");
 | |
| 		
 | |
| 		//Extract parametres
 | |
| 		$conversationName = ($_POST["name"] == "false" ? false : $_POST['name']);
 | |
| 		$followConversation = ($_POST['follow'] == "true" ? true : false);
 | |
| 		$usersList = users_list_to_array($_POST['users']);
 | |
| 
 | |
| 		//Add current user (if not present)
 | |
| 		if(!isset($usersList[userID]))
 | |
| 			$usersList[userID] = userID;
 | |
| 
 | |
| 		//Check users
 | |
| 		if(count($usersList) < 1)
 | |
| 			Rest_fatal_error(501, "Please select at least one user !");
 | |
| 		
 | |
| 		//Try to create the conversation
 | |
| 		$conversationID = CS::get()->components->conversations->create(userID, $followConversation, $usersList, $conversationName);
 | |
| 		
 | |
| 		//Check for errors
 | |
| 		if($conversationID == 0)
 | |
| 			Rest_fatal_error(500, "Couldn't create the conversation !");
 | |
| 
 | |
| 		//Success
 | |
| 		return array(
 | |
| 			"conversationID" => $conversationID,
 | |
| 			"success" => "The conversation was successfully created !"
 | |
| 		);
 | |
| 	}
 | |
| 
 | |
| } | 
