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
|
|
|
|
*/
|
2017-06-18 08:07:52 +00:00
|
|
|
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 !");
|
|
|
|
|
|
|
|
//Return results
|
|
|
|
return $conversationsList;
|
|
|
|
}
|
|
|
|
|
2017-06-16 17:08:58 +00:00
|
|
|
/**
|
|
|
|
* Get informationsd about one conversation
|
|
|
|
*
|
|
|
|
* @url POST /conversations/getInfosOne
|
|
|
|
*/
|
2017-06-18 08:07:52 +00:00
|
|
|
public function getOneInformations(){
|
2017-06-16 17:08:58 +00:00
|
|
|
user_login_required();
|
|
|
|
|
2017-12-31 12:49:10 +00:00
|
|
|
//Get conversation ID
|
|
|
|
$conversationID = getPostConversationID("conversationID");
|
2017-06-16 17:08:58 +00:00
|
|
|
|
|
|
|
//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 !");
|
2017-06-16 17:08:58 +00:00
|
|
|
|
|
|
|
//Return conversation informations
|
|
|
|
return $conversationsList[0];
|
|
|
|
}
|
|
|
|
|
2017-06-10 08:07:03 +00:00
|
|
|
/**
|
|
|
|
* Create a new conversation
|
|
|
|
*
|
|
|
|
* @url POST /conversations/create
|
|
|
|
*/
|
2017-06-18 08:07:52 +00:00
|
|
|
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);
|
2017-06-25 16:09:18 +00:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
2017-06-18 08:07:52 +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 !");
|
2017-06-18 08:07:52 +00:00
|
|
|
$conversationID = toInt($_POST["conversationID"]);
|
|
|
|
|
2017-06-23 17:00:40 +00:00
|
|
|
//Check if the user belongs to the conversation
|
2017-06-18 08:07:52 +00:00
|
|
|
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 !");
|
|
|
|
}
|
|
|
|
|
2017-06-18 14:18:09 +00:00
|
|
|
//Update conversation users (if required)
|
|
|
|
if(isset($_POST["members"])){
|
|
|
|
//Get user list
|
2017-06-25 16:09:18 +00:00
|
|
|
$conversationMembers = numbers_list_to_array($_POST['members']);
|
2017-06-18 08:41:43 +00:00
|
|
|
|
2017-06-18 14:18:09 +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
|
|
|
|
|
|
|
}
|
2017-06-18 08:07:52 +00:00
|
|
|
|
|
|
|
//Success
|
2017-06-18 08:41:43 +00:00
|
|
|
return array("success" => "Conversation informations were successfully updated !");
|
2017-06-18 08:07:52 +00:00
|
|
|
}
|
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();
|
2017-06-19 12:53:30 +00:00
|
|
|
|
|
|
|
//Extract parametres
|
2017-12-25 08:20:22 +00:00
|
|
|
$otherUser = getPostUserID('otherUser');
|
|
|
|
|
|
|
|
//Check if we are allowed to create a conversation or not
|
2017-06-19 12:53:30 +00:00
|
|
|
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
|
|
|
|
2017-06-19 12:53:30 +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 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 !");
|
|
|
|
|
2017-06-24 12:30:16 +00:00
|
|
|
//Check for images
|
|
|
|
if($image){
|
|
|
|
//Get image informations
|
|
|
|
$arrayImage = explode(";", $image);
|
|
|
|
if(count($arrayImage) != 2)
|
|
|
|
Rest_fatal_error(400, "Invalid image informations !");
|
|
|
|
|
|
|
|
$image = str_replace("base64,", "", $arrayImage[1]);
|
|
|
|
|
|
|
|
//Try to base64_decode image
|
|
|
|
$decoded_image = base64_decode($image, true);
|
|
|
|
|
|
|
|
//Check for errors
|
|
|
|
if(!$decoded_image)
|
|
|
|
Rest_fatal_error(500, "Couldn't extract image !");
|
|
|
|
|
|
|
|
//Get target folder
|
|
|
|
$targetFolder = prepareFileCreation(userID, "conversations");
|
|
|
|
|
|
|
|
//Generate target file name
|
|
|
|
$targetFileName = $targetFolder.generateNewFileName(path_user_data($targetFolder, true), "png");
|
|
|
|
$relativeFileName = path_user_data($targetFileName, true);
|
|
|
|
|
|
|
|
//Try to resize image
|
|
|
|
if(!reduce_image("string", $relativeFileName, 1200, 1200, "image/png", $decoded_image)){
|
|
|
|
//Returns error
|
|
|
|
Rest_fatal_error(500, "Couldn't resize image !");
|
|
|
|
}
|
|
|
|
|
|
|
|
//Save image URI
|
|
|
|
$image = $targetFileName;
|
|
|
|
}
|
2017-06-23 17:00:40 +00:00
|
|
|
|
|
|
|
//Insert the new message
|
2017-06-24 12:30:16 +00:00
|
|
|
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
|
|
|
}
|
2017-06-25 16:09:18 +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
|
2017-06-25 16:09:18 +00:00
|
|
|
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
|
2017-06-25 16:09:18 +00:00
|
|
|
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);
|
2017-06-26 08:38:58 +00:00
|
|
|
|
|
|
|
//Specify that user has seen last messages
|
|
|
|
CS::get()->components->conversations->markUserAsRead(userID, $conversationID);
|
2017-06-25 16:09:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
2017-06-26 08:38:58 +00:00
|
|
|
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"]))
|
2017-06-26 08:38:58 +00:00
|
|
|
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);
|
2017-06-26 08:38:58 +00:00
|
|
|
|
|
|
|
//Specify that user has seen last messages
|
|
|
|
CS::get()->components->conversations->markUserAsRead(userID, $conversationID);
|
2017-06-26 08:27:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-25 16:09:18 +00:00
|
|
|
//Return result
|
|
|
|
return $conversationsMessages;
|
|
|
|
}
|
2017-12-16 13:02:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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");
|
2017-12-16 13:02:39 +00:00
|
|
|
|
|
|
|
//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);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Return the messges
|
|
|
|
return $messages;
|
|
|
|
|
|
|
|
}
|
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 {
|
2017-12-31 12:49:10 +00:00
|
|
|
//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");
|
|
|
|
}
|
2017-06-10 08:07:03 +00:00
|
|
|
}
|