ComunicAPI/RestControllers/conversationsController.php

174 lines
5.0 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 !");
//Return results
return $conversationsList;
}
/**
* Get informationsd about one conversation
*
* @url POST /conversations/getInfosOne
*/
public function getOneInformations(){
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];
}
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);
2017-06-10 13:04:45 +00:00
$usersList = users_list_to_array($_POST['users']);
//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"]);
//Check if the user is a conversation moderator or not
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 = users_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();
//Check for parametres
if(!isset($_POST['user1']) OR !isset($_POST['user2']))
Rest_fatal_error(400, "Please check your parametres")
}
2017-06-10 08:07:03 +00:00
}