ComunicAPI/RestControllers/conversationsController.php

457 lines
14 KiB
PHP
Raw Normal View History

2017-06-10 08:07:03 +00:00
<?php
/**
* Conversations controller
*
* @author Pierre HUBERT
*/
class conversationsController{
2017-06-11 13:09:54 +00:00
/**
* Get the conversations list
*
* @url POST /conversations/getList
*/
public function getList(){
2017-06-11 13:09:54 +00:00
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 !");
//Process the list of conversation
foreach($conversationsList as $num => $conv)
$conversationsList[$num] = self::ConvInfoToAPI($conv);
2017-06-11 13:09:54 +00:00
//Return results
return $conversationsList;
}
/**
* Get informationsd about one conversation
*
* @url POST /conversations/getInfosOne
* @url POST /conversations/getInfoOne
*/
public function getOneInformation(){
user_login_required();
//Get conversation ID
$conversationID = getPostConversationID("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,".
2017-12-29 09:23:59 +00:00
" or the conversation doesn't exists !");
//Return conversation informations
return self::ConvInfoToAPI($conversationsList[0]);
}
2017-06-10 08:07:03 +00:00
/**
* Create a new conversation
*
* @url POST /conversations/create
*/
public function create(){
2017-06-10 08:07:03 +00:00
user_login_required();
//Check for parametres
if(!check_post_parametres(array("name", "follow", "users")))
2017-06-19 08:36:39 +00:00
Rest_fatal_error(400, "Please check parametres passed with the request !");
2017-06-10 08:07:03 +00:00
//Extract parametres
$conversationName = ($_POST["name"] == "false" ? false : $_POST['name']);
$followConversation = ($_POST['follow'] == "true" ? true : false);
$usersList = numbers_list_to_array($_POST['users']);
2017-06-10 13:04:45 +00:00
//Add current user (if not present)
if(!isset($usersList[userID]))
$usersList[userID] = userID;
2017-06-10 08:07:03 +00:00
//Check users
2017-06-10 13:04:45 +00:00
if(count($usersList) < 1)
Rest_fatal_error(501, "Please select at least one user !");
2017-06-10 08:07:03 +00:00
2017-06-10 13:04:45 +00:00
//Try to create the conversation
$conversationID = CS::get()->components->conversations->create(userID, $followConversation, $usersList, $conversationName);
2017-06-10 08:07:03 +00:00
2017-06-10 13:04:45 +00:00
//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 !"
);
2017-06-10 08:07:03 +00:00
}
/**
* Update a conversation settings
*
* @url POST /conversations/updateSettings
*/
public function updateSettings(){
user_login_required();
//Check conversation ID was specified
if(!isset($_POST["conversationID"]))
2017-06-19 08:36:39 +00:00
Rest_fatal_error(400, "Please specify a conversation ID !");
$conversationID = toInt($_POST["conversationID"]);
2017-06-23 17:00:40 +00:00
//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 user want to update its follow state
if(isset($_POST['following'])){
$follow = $_POST["following"] === "true" ? true : false;
//Try to update follow state
if(!CS::get()->components->conversations->changeFollowState(userID, $conversationID, $follow))
Rest_fatal_error(500, "Couldn't update user follow state !");
}
2017-06-18 08:41:43 +00:00
//Check if user asked to change moderation settings
if(isset($_POST['members']) OR isset($_POST['name'])){
//Check if user is allowed to change such settings
if(!CS::get()->components->conversations->userIsModerator(userID, $conversationID))
Rest_fatal_error(401, "The user isn't a moderator, he can't updates such settings !");
//Update conversation name (if required)
if(isset($_POST["name"])){
$conversationName = $_POST['name'] == "false" ? "" : $_POST['name'];
//Update conversation name
if(!CS::get()->components->conversations->changeName($conversationID, $conversationName))
Rest_fatal_error(500, "Couldn't update conversation name !");
}
//Update conversation users (if required)
if(isset($_POST["members"])){
//Get user list
$conversationMembers = numbers_list_to_array($_POST['members']);
2017-06-18 08:41:43 +00:00
//Make sure current user is in the list
$conversationMembers[userID] = userID;
//Try to update conversation members
if(!CS::get()->components->conversations->updateMembers($conversationID, $conversationMembers))
Rest_fatal_error(500, "Couldn't update conversation members list !");
}
2017-06-18 08:41:43 +00:00
}
//Success
2017-06-18 08:41:43 +00:00
return array("success" => "Conversation informations were successfully updated !");
}
2017-06-19 08:36:39 +00:00
/**
* Find (create ?) and return private conversation ID
*
* @url POST /conversations/getPrivate
*/
public function findPrivate(){
user_login_required();
//Extract parametres
$otherUser = getPostUserID('otherUser');
//Check if we are allowed to create a conversation or not
if(isset($_POST["allowCreate"]))
$allowCreate = $_POST["allowCreate"] == "true" ? true : false;
else
$allowCreate = false;
//Search the database
$results = CS::get()->components->conversations->findPrivate(userID, $otherUser);
//Count results number
if(count($results) === 0) {
//We check if we are not allowed to create a conversation
if(!$allowCreate)
Rest_fatal_error(404, "Not any private conversation were found. The server wasn't allowed to create a new one...");
//Now we can try to create the conversation
$ID_owner = userID;
$follow = false; //Not following by default
$conversationMembers = array(userID, $otherUser);
//Try to create the conversation
$conversationID = CS::get()->components->conversations->create($ID_owner, $follow, $conversationMembers);
//Check for errors
if($conversationID == 0)
Rest_fatal_error(500, "Couldn't create the conversation !");
//Save result
$results = array($conversationID);
}
2017-06-19 08:36:39 +00:00
//Success
return array("conversationsID" => $results);
2017-06-19 08:36:39 +00:00
}
2017-06-23 17:00:40 +00:00
/**
* 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"]))
2017-12-25 12:03:50 +00:00
Rest_fatal_errror(400, "Please specify a conversation ID !");
2017-06-23 17:00:40 +00:00
//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 information were specified about the new message or not
if(!isset($_POST['message']))
Rest_fatal_error(401, "New conversation messages must contain a message (can be empty if there is an image) !");
2017-06-23 17:00:40 +00:00
//Else extract informations
$message = (string) (isset($_POST['message']) ? $_POST['message'] : "");
2017-06-23 17:00:40 +00:00
//Check for image
$image = "";
if(check_post_file("image")){
//Save image and retrieve its URI
$image = save_post_image("image", userID, "conversations", 1200, 1200);
}
2017-06-23 17:00:40 +00:00
//Check message validity
if(!check_string_before_insert($message) && $image == "")
Rest_fatal_error(401, "Invalid message creation request !");
2017-06-23 17:00:40 +00:00
//Insert the new message
if(!CS::get()->components->conversations->sendMessage(userID, $conversationID, $message, $image))
2017-06-23 17:00:40 +00:00
Rest_fatal_error(500, "Couldn't send the message !");
2017-06-24 12:32:01 +00:00
//Success
return array("success" => "Conversation message with successfully added !");
2017-06-23 17:00:40 +00:00
}
/**
* Refresh conversations
*
* @url POST /conversations/refresh
*/
public function refreshConversations(){
user_login_required();
//Prepare return
$conversationsMessages = array();
2017-06-26 08:27:38 +00:00
//Check if we have to give the latest messages of some conversations
if(isset($_POST['newConversations'])){
//Get conversations ID
$newConversations = numbers_list_to_array($_POST['newConversations']);
foreach($newConversations as $conversationID){
2017-06-26 08:27:38 +00:00
//First, check 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 number ".$conversationID." !");
2017-06-26 08:27:38 +00:00
//Then we can get the ten last messages of the conversation system
2017-06-25 17:11:00 +00:00
$conversationsMessages["conversation-".$conversationID] = CS::get()->components->conversations->getLastMessages($conversationID, 10);
//Specify that user has seen last messages
CS::get()->components->conversations->markUserAsRead(userID, $conversationID);
}
}
2017-06-26 08:27:38 +00:00
//Check if we have to refresh some conversations
if(isset($_POST['toRefresh'])){
//Try to decode informations about the conversations
$toRefresh = json_decode($_POST['toRefresh'], true);
if($toRefresh === false)
Rest_fatal_error(400, "Couldn't get refresh conversations informations !");
//Process each conversation
foreach($toRefresh as $conversationID=>$informations){
//Get conversation ID
$conversationID = toInt(str_replace("conversation-", "", $conversationID));
//Check if the conversation is not a new conversation too
if(isset($conversationsMessages["conversation-".$conversationID]))
Rest_fatal_error(401, "Conversation marked as new can't be refreshed !");
//Check if conversation number is valid
if($conversationID < 1)
Rest_fatal_error(401, "An error occured while trying to extract given conversation ID to refresh: ".$conversationID);
2017-06-26 08:27:38 +00:00
//Check if informations where given about the limit of the informations to get
if(!isset($informations["last_message_id"]))
Rest_fatal_error(401, "Conversation ".$conversationID." couldn't be refreshed: not enough informations !");
2017-06-26 08:27:38 +00:00
$last_message_id = toInt($informations["last_message_id"]);
//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 number ".$conversationID." !");
//Then we can get informations about the conversation
$conversationsMessages["conversation-".$conversationID] = CS::get()->components->conversations->getNewMessages($conversationID, $last_message_id);
//Specify that user has seen last messages
CS::get()->components->conversations->markUserAsRead(userID, $conversationID);
2017-06-26 08:27:38 +00:00
}
}
//Return result
return $conversationsMessages;
}
/**
* Refresh the messages of a specific conversation
*
* @url POST /conversations/refresh_single
*/
public function refresh_single(){
user_login_required();
//Get the ID of the conversation to refresh
2017-12-31 11:56:08 +00:00
$conversationID = getPostConversationID("conversationID");
//Get the last message ID downloaded by the client
if(!isset($_POST['last_message_id']))
Rest_fatal_error(400, "Please specify the ID of the last message you've got!");
$conversationID = toInt($_POST['conversationID']);
$last_message_id = toInt($_POST['last_message_id']);
//Check if the current user can access the conversation
if(!CS::get()->components->conversations->userBelongsTo(userID, $conversationID))
Rest_fatal_error(401, "Specified user doesn't belongs to the conversation number ".$conversationID." !");
//Check if user has already some of the messages of the conversations, or
//If we have to return the list of the last ten messages
if($last_message_id == 0){
$messages =
CS::get()->components->conversations->getLastMessages($conversationID, 10);
}
else {
$messages =
CS::get()->components->conversations->getNewMessages($conversationID, $last_message_id);
}
2017-12-31 13:08:27 +00:00
//Specify that user has seen last messages
CS::get()->components->conversations->markUserAsRead(userID, $conversationID);
//Return the messges
return $messages;
}
2017-12-31 11:56:08 +00:00
/**
* Get the number of unread conversations of the user
*
* @url POST conversations/get_number_unread
*/
public function get_number_unread(){
user_login_required();
//Get the number of unread conversations of the user
$number_unread_conversations = components()->conversations->number_user_unread(userID);
//Return result
return array("nb_unread" => $number_unread_conversations);
}
2018-02-23 10:32:17 +00:00
/**
* Get the list of unread notifications
*
* @url POST /conversations/get_list_unread
*/
public function get_list_unread(){
user_login_required();
//Get the list of unread conversations of the user
$list_unread_conversations = components()->conversations->get_list_unread(userID);
//Return result
return $list_unread_conversations;
}
2017-12-31 11:56:08 +00:00
/**
* Delete a conversation
*
* @url POST /conversations/delete
*/
public function delete(){
user_login_required();
//Get conversation ID
$conversationID = getPostConversationID("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 number ".$conversationID." !");
//Check if user is the owner of the conversation or not
$owner = CS::get()->components->conversations->userIsModerator(userID, $conversationID);
if($owner){
//Delete the conversation
if(!CS::get()->components->conversations->delete_conversation($conversationID))
Rest_fatal_error(500, "Couldn't delete the conversation from the server !");
}
else {
//Delete the membership to the conversation
if(!CS::get()->components->conversations->delete_member($conversationID, userID))
Rest_fatal_error(500, "Couldn't delete conversation membership !");
2017-12-31 11:56:08 +00:00
}
//The operation is a success
return array("success" => "The conversation has been deleted");
}
/**
* Turn a ConversationInfo object into a valid API entry
*
* @param ConversationInfo $conv Information about the conversation
* @return array Generated conversation object
*/
private static function ConvInfoToAPI(ConversationInfo $conv) : array {
$data = array();
$data["ID"] = $conv->get_id();
$data["ID_owner"] = $conv->get_id_owner();
$data["last_active"] = $conv->get_last_active();
$data["name"] = $conv->has_name() ? $conv->get_name() : false;
$data["following"] = $conv->is_following() ? 1 : 0;
$data["saw_last_message"] = $conv->is_saw_last_message() ? 1 : 0;
$data["members"] = $conv->get_members();
return $data;
}
2017-06-10 08:07:03 +00:00
}