9 Commits

8 changed files with 257 additions and 157 deletions

View File

@ -480,6 +480,55 @@ class ConversationsController {
return array("success" => "The conversation has been deleted"); return array("success" => "The conversation has been deleted");
} }
/**
* Update the content of a conversation message
*
* @url POST /conversations/updateMessage
*/
public function updateMessage(){
user_login_required();
$messageID = postInt("messageID");
$newContent = postString("content");
if(!check_string_before_insert($newContent))
Rest_fatal_error(401, "Invalid new message content!");
//Check whether the user own or not conversation message
if(!components()->conversations->isOwnerMessage(userID, $messageID))
Rest_fatal_error(401, "You do not own this conversation message!");
//Update the message
$message = new ConversationMessage();
$message->set_id($messageID);
$message->set_message($newContent);
if(!components()->conversations->updateMessage($message))
Rest_fatal_error(500, "Could not update the content of the message!");
return array("success" => "The conversation message has been successfully updated!");
}
/**
* Delete a conversation message
*
* @url POST /conversations/deleteMessage
*/
public function deleteMessage(){
user_login_required();
$messageID = postInt("messageID");
//Check whether the user own or not conversation message
if(!components()->conversations->isOwnerMessage(userID, $messageID))
Rest_fatal_error(401, "You do not own this conversation message!");
if(!components()->conversations->deleteConversationMessage($messageID))
Rest_fatal_error(500, "Could not delete conversation message!");
return array("success" => "Conversation message has been successfully deleted!");
}
/** /**
* Get and return safely a conversation ID specified in a $_POST Request * Get and return safely a conversation ID specified in a $_POST Request
* *

View File

@ -113,6 +113,10 @@ class friendsController{
//Get target ID //Get target ID
$friendID = getPostUserID('friendID'); $friendID = getPostUserID('friendID');
//Check if the current user is requesting himself as friend
if($friendID == userID)
Rest_fatal_error(401, "You can not become a friend to yourself!");
//Check if the two persons are already friend //Check if the two persons are already friend
if(CS::get()->components->friends->are_friend(userID, $friendID)) if(CS::get()->components->friends->are_friend(userID, $friendID))
Rest_fatal_error(401, "The two personns are already friend !"); Rest_fatal_error(401, "The two personns are already friend !");

View File

@ -40,11 +40,17 @@ class notificationsController {
user_login_required(); user_login_required();
//Get and return the data //Get and return the data
return array( $data = array(
"notifications" => components()->notifications->count_unread(userID), "notifications" => components()->notifications->count_unread(userID),
"conversations" => components()->conversations->number_user_unread(userID) "conversations" => components()->conversations->number_user_unread(userID)
); );
//Include friendship requests if required
if(isset($_POST["friends_request"]))
if(postBool("friends_request"))
$data["friends_request"] = components()->friends->count_requests(userID);
return $data;
} }

View File

@ -7,6 +7,12 @@
class APIClients { class APIClients {
/**
* Tables name
*/
const SERVICES_TOKENS_TABLE = DBprefix."api_services_tokens";
const USERS_TOKENS_TABLE = DBprefix."api_users_tokens";
/** /**
* Check request client tokens * Check request client tokens
* *
@ -21,7 +27,7 @@ class APIClients {
return false; return false;
//Save service ID in a constant //Save service ID in a constant
define("APIServiceID", $serviceInfos["ID"]); define("APIServiceID", $serviceInfos["id"]);
//Save service domain in a constant (if any) //Save service domain in a constant (if any)
if($serviceInfos["clientDomain"] != "") if($serviceInfos["clientDomain"] != "")
@ -40,8 +46,8 @@ class APIClients {
*/ */
private function validateClientTokens(string $serviceName, string $token) { private function validateClientTokens(string $serviceName, string $token) {
//Prepare DataBase request //Prepare DataBase request
$tableName = CS::get()->config->get("dbprefix")."API_ServicesToken"; $tableName = self::SERVICES_TOKENS_TABLE;
$conditions = "WHERE serviceName = ? AND token = ?"; $conditions = "WHERE service_name = ? AND token = ?";
$values = array( $values = array(
$serviceName, $serviceName,
$token $token
@ -58,7 +64,7 @@ class APIClients {
//The API is correctly identified //The API is correctly identified
//Generate client informations //Generate client informations
$clientInformations = array( $clientInformations = array(
"ID" => $requestResult[0]['ID'], "id" => $requestResult[0]['id'],
"clientDomain" => ($requestResult[0]["client_domain"] == "" ? false : $requestResult[0]["client_domain"]) "clientDomain" => ($requestResult[0]["client_domain"] == "" ? false : $requestResult[0]["client_domain"])
); );
@ -80,7 +86,7 @@ class APIClients {
$entry = self::APIClientsToDb($client); $entry = self::APIClientsToDb($client);
//Insert the entry in the database //Insert the entry in the database
$tableName = CS::get()->config->get("dbprefix")."API_ServicesToken"; $tableName = self::SERVICES_TOKENS_TABLE;
return CS::get()->db->addLine($tableName, $entry); return CS::get()->db->addLine($tableName, $entry);
} }
@ -95,7 +101,7 @@ class APIClients {
$data = array(); $data = array();
$data["time_insert"] = $client->get_time_insert(); $data["time_insert"] = $client->get_time_insert();
$data["serviceName"] = $client->get_name(); $data["service_name"] = $client->get_name();
$data["token"] = $client->get_token(); $data["token"] = $client->get_token();
if($client->has_client_domain()) if($client->has_client_domain())
$data["client_domain"] = $client->get_client_domain(); $data["client_domain"] = $client->get_client_domain();

View File

@ -12,18 +12,6 @@ class AccountComponent {
*/ */
const USER_TABLE = "utilisateurs"; const USER_TABLE = "utilisateurs";
/**
* @var String $userLoginAPItable The name of the table that contains logins performed on the API
*/
private $userLoginAPItable = "";
/**
* Public constructor
*/
public function __construct(){
$this->userLoginAPItable = CS::get()->config->get("dbprefix")."API_userLoginToken";
}
/** /**
* Try to login user with returning a service token * Try to login user with returning a service token
* *
@ -61,10 +49,10 @@ class AccountComponent {
$token2 = random_str(75); $token2 = random_str(75);
//Insert token in the database //Insert token in the database
$tableName = $this->userLoginAPItable; $tableName = APIClients::USERS_TOKENS_TABLE;
$insertValues = array( $insertValues = array(
"ID_utilisateurs" => $userID, "user_id" => $userID,
"ID_".CS::get()->config->get("dbprefix")."API_ServicesToken" => $serviceID, "service_id" => $serviceID,
"token1" => $token1, "token1" => $token1,
"token2" => $token2 "token2" => $token2
); );
@ -84,12 +72,12 @@ class AccountComponent {
*/ */
private function getUserLoginTokenByIDs(int $userID, int $serviceID) { private function getUserLoginTokenByIDs(int $userID, int $serviceID) {
//Prepare database request //Prepare database request
$conditions = "WHERE ID_utilisateurs = ? AND ID_".CS::get()->config->get("dbprefix")."API_ServicesToken = ?"; $conditions = "WHERE user_id = ? AND service_id = ?";
$values = array( $values = array(
$userID, $userID,
$serviceID $serviceID
); );
$tokenInfos = CS::get()->db->select($this->userLoginAPItable, $conditions, $values); $tokenInfos = CS::get()->db->select(APIClients::USERS_TOKENS_TABLE, $conditions, $values);
if(count($tokenInfos) == 0) if(count($tokenInfos) == 0)
return false; //There is nobody at this address return false; //There is nobody at this address
@ -111,14 +99,14 @@ class AccountComponent {
public function deleteUserLoginToken(int $userID, string $serviceID) : bool { public function deleteUserLoginToken(int $userID, string $serviceID) : bool {
//Prepare database request //Prepare database request
$condition = "ID_utilisateurs = ? AND ID_".CS::get()->config->get("dbprefix")."API_ServicesToken = ?"; $condition = "user_id = ? AND service_id = ?";
$values = array( $values = array(
$userID, $userID,
$serviceID $serviceID
); );
//Try to perform request //Try to perform request
if(!CS::get()->db->deleteEntry($this->userLoginAPItable, $condition, $values)) if(!CS::get()->db->deleteEntry(APIClients::USERS_TOKENS_TABLE, $condition, $values))
return false; //Something went wrong during the request return false; //Something went wrong during the request
//Everything is ok //Everything is ok
@ -135,13 +123,13 @@ class AccountComponent {
public function deleteAllUserLoginTokens(int $userID) : bool { public function deleteAllUserLoginTokens(int $userID) : bool {
//Prepare database request //Prepare database request
$condition = "ID_utilisateurs = ?"; $condition = "user_id = ?";
$values = array( $values = array(
$userID $userID
); );
//Try to perform request //Try to perform request
if(!CS::get()->db->deleteEntry($this->userLoginAPItable, $condition, $values)) if(!CS::get()->db->deleteEntry(APIClients::USERS_TOKENS_TABLE, $condition, $values))
return false; //Something went wrong during the request return false; //Something went wrong during the request
//Everything is ok //Everything is ok
@ -162,8 +150,8 @@ class AccountComponent {
return 0; return 0;
//Prepare database request //Prepare database request
$tablesName = $this->userLoginAPItable; $tablesName = APIClients::USERS_TOKENS_TABLE;
$conditions = "WHERE ".$this->userLoginAPItable.".ID_".CS::get()->config->get("dbprefix")."API_ServicesToken = ? AND ".$this->userLoginAPItable.".token1 = ? AND ".$this->userLoginAPItable.".token2 = ?"; $conditions = "WHERE ".APIClients::USERS_TOKENS_TABLE.".service_id = ? AND ".APIClients::USERS_TOKENS_TABLE.".token1 = ? AND ".APIClients::USERS_TOKENS_TABLE.".token2 = ?";
$conditionsValues = array( $conditionsValues = array(
$serviceID, $serviceID,
$tokens[0], $tokens[0],
@ -178,7 +166,7 @@ class AccountComponent {
return 0; //No result return 0; //No result
//Return ID //Return ID
return $userInfos[0]["ID_utilisateurs"]; return $userInfos[0]["user_id"];
} }
/** /**

View File

@ -8,29 +8,11 @@
class Conversations { class Conversations {
/** /**
* @var String $conversationsListTable Name of the conversation list table * Tables name definition
*/ */
private $conversationsListTable; const LIST_TABLE = DBprefix."conversations_list";
const USERS_TABLE = DBprefix."conversations_users";
/** const MESSAGES_TABLE = DBprefix."conversations_messages";
* @var String $conversationsUsersTable Name of the conversation users table
*/
private $conversationsUsersTable;
/**
* @var String $conversationMessagesTabel Name of the conversation messages table
*/
private $conversationsMessagesTable;
/**
* Public constructor
*/
public function __construct(){
$this->conversationsListTable = CS::get()->config->get("dbprefix")."conversations_list";
$this->conversationsUsersTable = CS::get()->config->get("dbprefix")."conversations_users";
$this->conversationsMessagesTable = CS::get()->config->get("dbprefix")."conversations_messages";
}
/** /**
* Get the conversations list of a specified user * Get the conversations list of a specified user
@ -43,19 +25,19 @@ class Conversations {
public function getList(int $userID, int $conversationID = 0){ public function getList(int $userID, int $conversationID = 0){
//Prepare database request //Prepare database request
$tablesName = $this->conversationsListTable.", ".$this->conversationsUsersTable; $tablesName = self::LIST_TABLE.", ".self::USERS_TABLE;
//Prepare conditions //Prepare conditions
$tableJoinCondition = $this->conversationsListTable.".ID = ".$this->conversationsUsersTable.".ID_".$this->conversationsListTable.""; $tableJoinCondition = self::LIST_TABLE.".id = ".self::USERS_TABLE.".conv_id";
$userCondition = $this->conversationsUsersTable.".ID_utilisateurs = ?"; $userCondition = self::USERS_TABLE.".user_id = ?";
$orderResults = "ORDER BY ".$this->conversationsListTable.".last_active DESC"; $orderResults = "ORDER BY ".self::LIST_TABLE.".last_active DESC";
//Specify conditions values //Specify conditions values
$conditionsValues = array($userID); $conditionsValues = array($userID);
//Check if we have to get informations about just one conversation //Check if we have to get informations about just one conversation
if($conversationID != 0){ if($conversationID != 0){
$specificConditions = "AND ".$this->conversationsListTable.".ID = ?"; $specificConditions = "AND ".self::LIST_TABLE.".id = ?";
$conditionsValues[] = $conversationID; $conditionsValues[] = $conversationID;
} }
else else
@ -66,12 +48,12 @@ class Conversations {
//Fields list //Fields list
$requiredFields = array( $requiredFields = array(
$this->conversationsListTable.".ID", self::LIST_TABLE.".id",
$this->conversationsListTable.".last_active", self::LIST_TABLE.".last_active",
$this->conversationsListTable.".name", self::LIST_TABLE.".name",
$this->conversationsListTable.".ID_utilisateurs AS ID_owner", self::LIST_TABLE.".user_id AS owner_id",
$this->conversationsUsersTable.".following", self::USERS_TABLE.".following",
$this->conversationsUsersTable.".saw_last_message", self::USERS_TABLE.".saw_last_message",
); );
//Perform database request //Perform database request
@ -101,10 +83,10 @@ class Conversations {
public function getConversationMembers(int $conversationID) : array { public function getConversationMembers(int $conversationID) : array {
//Perform a request on the database //Perform a request on the database
$tableName = $this->conversationsUsersTable; $tableName = self::USERS_TABLE;
$conditions = "WHERE ID_".$this->conversationsListTable." = ?"; $conditions = "WHERE conv_id = ?";
$conditionsValues = array($conversationID*1); $conditionsValues = array($conversationID*1);
$getFields = array("ID_utilisateurs as userID"); $getFields = array("user_id");
//Perform the request //Perform the request
$results = CS::get()->db->select($tableName, $conditions, $conditionsValues, $getFields); $results = CS::get()->db->select($tableName, $conditions, $conditionsValues, $getFields);
@ -116,7 +98,7 @@ class Conversations {
$membersList = array(); $membersList = array();
foreach($results as $processUser) foreach($results as $processUser)
$membersList[] = $processUser["userID"]; $membersList[] = $processUser["user_id"];
//Return result //Return result
return $membersList; return $membersList;
@ -131,18 +113,18 @@ class Conversations {
public function create(ConversationInfo $conv) : int{ public function create(ConversationInfo $conv) : int{
$mainInformations = array( $mainInformations = array(
"ID_utilisateurs" => $conv->get_id_owner(), "user_id" => $conv->get_id_owner(),
"name" => ($conv->has_name() ? $conv->get_name() : ""), "name" => ($conv->has_name() ? $conv->get_name() : ""),
"last_active" => time(), "last_active" => time(),
"creation_time" => time() "creation_time" => time()
); );
//First, insert the conversation in the main table //First, insert the conversation in the main table
if(!CS::get()->db->addLine($this->conversationsListTable, $mainInformations)) if(!CS::get()->db->addLine(self::LIST_TABLE, $mainInformations))
return 0; //An error occured return 0; //An error occured
//Get the last inserted ID //Get the last inserted ID
$conversationID = CS::get()->db->getLastInsertedID(); $conversationID = db()->getLastInsertedID();
//Check for errors //Check for errors
if($conversationID == 0) if($conversationID == 0)
@ -175,8 +157,8 @@ class Conversations {
public function userBelongsTo(int $userID, int $conversationID) : bool { public function userBelongsTo(int $userID, int $conversationID) : bool {
//Prepare a request on the database //Prepare a request on the database
$tableName = $this->conversationsUsersTable; $tableName = self::USERS_TABLE;
$conditions = "WHERE ID_".$this->conversationsListTable." = ? AND ID_utilisateurs = ?"; $conditions = "WHERE conv_id = ? AND user_id = ?";
$values = array( $values = array(
$conversationID, $conversationID,
$userID $userID
@ -204,8 +186,8 @@ class Conversations {
public function changeFollowState(int $userID, int $conversationID, bool $follow) : bool{ public function changeFollowState(int $userID, int $conversationID, bool $follow) : bool{
//Prepare the request on the database //Prepare the request on the database
$tableName = $this->conversationsUsersTable; $tableName = self::USERS_TABLE;
$conditions = "ID_".$this->conversationsListTable." = ? AND ID_utilisateurs = ?"; $conditions = "conv_id = ? AND user_id = ?";
$condVals = array( $condVals = array(
$conversationID, $conversationID,
$userID $userID
@ -233,8 +215,8 @@ class Conversations {
*/ */
public function changeName(int $conversationID, string $conversationName) : bool{ public function changeName(int $conversationID, string $conversationName) : bool{
//Prepare database request //Prepare database request
$tableName = $this->conversationsListTable; $tableName = self::LIST_TABLE;
$conditions = "ID = ?"; $conditions = "id = ?";
$condVals = array($conversationID); $condVals = array($conversationID);
//Changes //Changes
@ -295,10 +277,10 @@ class Conversations {
private function addMember(int $conversationID, int $userID, bool $follow = false) : bool { private function addMember(int $conversationID, int $userID, bool $follow = false) : bool {
//Prepare database request //Prepare database request
$tableName = $this->conversationsUsersTable; $tableName = self::USERS_TABLE;
$values = array( $values = array(
"ID_".$this->conversationsListTable => $conversationID, "conv_id" => $conversationID,
"ID_utilisateurs" => $userID, "user_id" => $userID,
"time_add" => time(), "time_add" => time(),
"following" => $follow ? 1 : 0, "following" => $follow ? 1 : 0,
"saw_last_message" => 1 "saw_last_message" => 1
@ -317,8 +299,8 @@ class Conversations {
*/ */
private function removeMember(int $conversationID, int $userID) : bool { private function removeMember(int $conversationID, int $userID) : bool {
//Prepare database request //Prepare database request
$tableName = $this->conversationsUsersTable; $tableName = self::USERS_TABLE;
$conditions = "ID_".$this->conversationsListTable." = ? AND ID_utilisateurs = ?"; $conditions = "conv_id = ? AND user_id = ?";
$values = array( $values = array(
$conversationID, $conversationID,
$userID $userID
@ -337,11 +319,11 @@ class Conversations {
*/ */
public function userIsModerator(int $userID, int $conversationID) : bool { public function userIsModerator(int $userID, int $conversationID) : bool {
//Prepare database request //Prepare database request
$tableName = $this->conversationsListTable; $tableName = self::LIST_TABLE;
$conditions = "WHERE ID = ?"; $conditions = "WHERE id = ?";
$values = array($conversationID); $values = array($conversationID);
$requiredFields = array( $requiredFields = array(
"ID_utilisateurs" "user_id"
); );
//Peform a request on the database //Peform a request on the database
@ -356,7 +338,22 @@ class Conversations {
return false; return false;
//Check the first result only //Check the first result only
return $results[0]["ID_utilisateurs"] == $userID; return $results[0]["user_id"] == $userID;
}
/**
* Check whether a user is the owner of a conversation message or not
*
* @param int $userID Target user ID
* @param int $messageID Target message
* @return bool TRUE if the user is the owner of the conversation / FALSE else
*/
public function isOwnerMessage(int $userID, int $messageID) : bool {
return db()->count(
self::MESSAGES_TABLE,
"WHERE id = ? AND user_id = ?",
array($messageID, $userID)
) > 0;
} }
/** /**
@ -369,22 +366,22 @@ class Conversations {
public function findPrivate(int $user1, int $user2) : array{ public function findPrivate(int $user1, int $user2) : array{
//Prepare database request //Prepare database request
$tableName = $this->conversationsUsersTable." AS table1 JOIN ". $tableName = self::USERS_TABLE." AS table1 JOIN ".
$this->conversationsUsersTable." AS table2 JOIN ". self::USERS_TABLE." AS table2 JOIN ".
$this->conversationsUsersTable." AS table3"; self::USERS_TABLE." AS table3";
//Prepare conditions //Prepare conditions
$joinCondition = "(table1.ID_".$this->conversationsListTable." = table2.ID_".$this->conversationsListTable.")". $joinCondition = "(table1.conv_id = table2.conv_id)".
"AND (table1.ID_".$this->conversationsListTable." = table3.ID_".$this->conversationsListTable.")"; "AND (table1.conv_id = table3.conv_id)";
$whereConditions = "table1.ID_utilisateurs = ? AND table2.ID_utilisateurs = ?"; $whereConditions = "table1.user_id = ? AND table2.user_id = ?";
$groupCondition = "table1.ID_".$this->conversationsListTable." having count(*) = 2"; $groupCondition = "table1.conv_id having count(*) = 2";
//Conditions values //Conditions values
$condValues = array($user1, $user2); $condValues = array($user1, $user2);
//Required fields //Required fields
$requiredFields = array( $requiredFields = array(
"table1.ID_".$this->conversationsListTable." as conversationID", "table1.conv_id as conversationID",
); );
//Build conditions //Build conditions
@ -415,11 +412,11 @@ class Conversations {
private function insertMessage(NewConversationMessage $message) : bool { private function insertMessage(NewConversationMessage $message) : bool {
//Prepare values //Prepare values
$tableName = $this->conversationsMessagesTable; $tableName = self::MESSAGES_TABLE;
$values = array( $values = array(
"ID_".$this->conversationsListTable => $message->get_conversationID(), "conv_id" => $message->get_conversationID(),
"ID_utilisateurs" => $message->get_userID(), "user_id" => $message->get_userID(),
"time_insert" => time(), "time_insert" => $message->get_time_sent(),
"message" => $message->has_message() ? $message->get_message() : "" "message" => $message->has_message() ? $message->get_message() : ""
); );
@ -445,8 +442,8 @@ class Conversations {
private function updateLastActive(int $conversationID, int $time) : bool{ private function updateLastActive(int $conversationID, int $time) : bool{
//Perform a request on the database //Perform a request on the database
$tableName = $this->conversationsListTable; $tableName = self::LIST_TABLE;
$conditions = "ID = ?"; $conditions = "id = ?";
$condVals = array($conversationID); $condVals = array($conversationID);
//Set new values //Set new values
@ -472,13 +469,13 @@ class Conversations {
private function allUsersAsUnread(int $conversationID, array $exceptions) : bool{ private function allUsersAsUnread(int $conversationID, array $exceptions) : bool{
//Prepare request //Prepare request
$tableName = $this->conversationsUsersTable; $tableName = self::USERS_TABLE;
$conditions = "ID_".$this->conversationsListTable." = ?"; $conditions = "conv_id = ?";
$condVals = array($conversationID); $condVals = array($conversationID);
//Remove users exceptions //Remove users exceptions
foreach($exceptions as $userID){ foreach($exceptions as $userID){
$conditions.= " AND ID_utilisateurs != ?"; $conditions.= " AND user_id != ?";
$condVals[] = $userID; $condVals[] = $userID;
} }
@ -505,8 +502,8 @@ class Conversations {
public function markUserAsRead(int $userID, int $conversationID) : bool { public function markUserAsRead(int $userID, int $conversationID) : bool {
//Prepare database request //Prepare database request
$tableName = $this->conversationsUsersTable; $tableName = self::USERS_TABLE;
$conditions = "ID_".$this->conversationsListTable." = ? AND ID_utilisateurs = ?"; $conditions = "conv_id = ? AND user_id = ?";
$condVals = array( $condVals = array(
$conversationID, $conversationID,
$userID $userID
@ -536,12 +533,15 @@ class Conversations {
//GUIDE LINE : this method act like a "controller" : it doesn't perform any database operation //GUIDE LINE : this method act like a "controller" : it doesn't perform any database operation
//But it manages all operations (insert message; save image; inform other users; ...) //But it manages all operations (insert message; save image; inform other users; ...)
//Set unique message time
$message->set_time_sent(time());
//First, try to insert the message //First, try to insert the message
if(!$this->insertMessage($message)) if(!$this->insertMessage($message))
return false; //An error occured return false; //An error occured
//Then, update the last activity of the conversation //Then, update the last activity of the conversation
if(!$this->updateLastActive($message->get_conversationID(), time())) if(!$this->updateLastActive($message->get_conversationID(), $message->get_time_sent()))
return false; //An error occured (2) return false; //An error occured (2)
//Then, set all the users of the conversation as unread //Then, set all the users of the conversation as unread
@ -552,6 +552,24 @@ class Conversations {
return true; return true;
} }
/**
* Update a message
*
* @param ConversationMessage $message Information about the message to update
* @return bool TRUE for a success / FALSE else
*/
public function updateMessage(ConversationMessage $message) : bool {
$modifs = array();
//Check if the content of message has to be updated
if($message->has_message())
$modifs["message"] = $message->get_message();
//Peform update
return db()->updateDB(self::MESSAGES_TABLE, "id = ?", $modifs, array($message->get_id()));
}
/** /**
* Get the last messages of a conversation * Get the last messages of a conversation
* *
@ -562,7 +580,7 @@ class Conversations {
public function getLastMessages(int $conversationID, int $numberOfMessages) : array { public function getLastMessages(int $conversationID, int $numberOfMessages) : array {
//Define conditions //Define conditions
$conditions = "WHERE ID_".$this->conversationsListTable." = ? ORDER BY ID DESC LIMIT ".($numberOfMessages*1); $conditions = "WHERE conv_id = ? ORDER BY id DESC LIMIT ".($numberOfMessages*1);
$condVals = array( $condVals = array(
$conversationID $conversationID
); );
@ -587,7 +605,7 @@ class Conversations {
public function getNewMessages(int $conversationID, int $lastMessageID) : array { public function getNewMessages(int $conversationID, int $lastMessageID) : array {
//Define conditions //Define conditions
$conditions = "WHERE ID_".$this->conversationsListTable." = ? AND ID > ? ORDER BY ID"; $conditions = "WHERE conv_id = ? AND ID > ? ORDER BY id";
$condVals = array( $condVals = array(
$conversationID, $conversationID,
$lastMessageID $lastMessageID
@ -611,7 +629,7 @@ class Conversations {
public function getOlderMessages(int $conversationID, int $startID, int $limit) : array { public function getOlderMessages(int $conversationID, int $startID, int $limit) : array {
//Define conditions //Define conditions
$conditions = "WHERE ID_".$this->conversationsListTable." = ? AND ID < ? ORDER BY ID DESC LIMIT ".($limit); $conditions = "WHERE conv_id = ? AND ID < ? ORDER BY id DESC LIMIT ".($limit);
$condVals = array( $condVals = array(
$conversationID, $conversationID,
$startID + 1 $startID + 1
@ -637,7 +655,7 @@ class Conversations {
public function getAllMessages(int $conversationID) : array { public function getAllMessages(int $conversationID) : array {
//Define conditions //Define conditions
$conditions = "WHERE ID_".$this->conversationsListTable." = ? ORDER BY ID"; $conditions = "WHERE conv_id = ? ORDER BY id";
$condVals = array( $condVals = array(
$conversationID $conversationID
); );
@ -658,9 +676,9 @@ class Conversations {
public function exist(int $convID) : bool { public function exist(int $convID) : bool {
//Perform a request on the database //Perform a request on the database
$tableName = $this->conversationsListTable; $tableName = self::LIST_TABLE;
return CS::get()->db->count($tableName, "WHERE ID = ?", array($convID)) > 0; return CS::get()->db->count($tableName, "WHERE id = ?", array($convID)) > 0;
} }
@ -698,7 +716,7 @@ class Conversations {
public function delete_conversation(int $convID) : bool { public function delete_conversation(int $convID) : bool {
//Get all the messages of the conversation //Get all the messages of the conversation
$messages = $this->getMessages("WHERE ID_".$this->conversationsListTable." = ?", array($convID)); $messages = $this->getMessages("WHERE conv_id = ?", array($convID));
//Delete each message //Delete each message
foreach($messages as $message){ foreach($messages as $message){
@ -733,7 +751,7 @@ class Conversations {
//Get all the messages of member the conversation //Get all the messages of member the conversation
$messages = $this->getMessages( $messages = $this->getMessages(
"WHERE ID_".$this->conversationsListTable." = ? AND ID_utilisateurs = ?", "WHERE conv_id = ? AND user_id = ?",
array($convID, $memberID)); array($convID, $memberID));
//Delete each message //Delete each message
@ -750,6 +768,22 @@ class Conversations {
return true; return true;
} }
/**
* Delete a single conversation message
*
* @param int $messageID The ID of the message to delete
* @return bool TRUE for a success / FALSE else
*/
public function deleteConversationMessage(int $messageID) : bool {
//Get information about the message
$messages = $this->getMessages("WHERE id = ?", array($messageID));
if(count($messages) < 1)
return FALSE; //Message not found
return $this->delete_message($messages[0]);
}
/** /**
* Delete a single message of a conversation * Delete a single message of a conversation
@ -774,7 +808,7 @@ class Conversations {
//Delete message from the database //Delete message from the database
$conditions = "ID = ?"; $conditions = "ID = ?";
$condValues = array($message->get_id()); $condValues = array($message->get_id());
return CS::get()->db->deleteEntry($this->conversationsMessagesTable, $conditions, $condValues); return CS::get()->db->deleteEntry(self::MESSAGES_TABLE, $conditions, $condValues);
} }
@ -787,13 +821,13 @@ class Conversations {
private function delete_all_members(int $convID) : bool { private function delete_all_members(int $convID) : bool {
//Prepare request on the database //Prepare request on the database
$conditions = "ID_".$this->conversationsListTable." = ?"; $conditions = "conv_id = ?";
$values = array( $values = array(
$convID $convID
); );
//Try to perform request //Try to perform request
return CS::get()->db->deleteEntry($this->conversationsUsersTable, $conditions, $values); return CS::get()->db->deleteEntry(self::USERS_TABLE, $conditions, $values);
} }
@ -804,7 +838,7 @@ class Conversations {
* @return bool True in case of success / false else * @return bool True in case of success / false else
*/ */
private function delete_conversation_entry(int $convID) : bool { private function delete_conversation_entry(int $convID) : bool {
return CS::get()->db->deleteEntry($this->conversationsListTable, "ID = ?", array($convID)); return CS::get()->db->deleteEntry(self::LIST_TABLE, "id = ?", array($convID));
} }
/** /**
@ -816,8 +850,8 @@ class Conversations {
public function number_user_unread(int $userID) : int { public function number_user_unread(int $userID) : int {
//Prepare database request //Prepare database request
$tableName = $this->conversationsUsersTable; $tableName = self::USERS_TABLE;
$conditions = "WHERE ID_utilisateurs = ? AND saw_last_message = 0 AND following = 1"; $conditions = "WHERE user_id = ? AND saw_last_message = 0 AND following = 1";
$values = array($userID); $values = array($userID);
//Perform request and return result //Perform request and return result
@ -834,9 +868,9 @@ class Conversations {
public function get_list_unread(int $userID) : array { public function get_list_unread(int $userID) : array {
//Perform the request on the server //Perform the request on the server
$tablesName = $this->conversationsUsersTable." as users, ".$this->conversationsListTable." as list, ".$this->conversationsMessagesTable." as messages"; $tablesName = self::USERS_TABLE." as users, ".self::LIST_TABLE." as list, ".self::MESSAGES_TABLE." as messages";
$conditions = "WHERE users.ID_utilisateurs = ? AND users.following = 1 AND users.saw_last_message = 0 AND users.ID_comunic_conversations_list = list.ID $conditions = "WHERE users.user_id = ? AND users.following = 1 AND users.saw_last_message = 0 AND users.conv_id = list.id
AND list.ID = messages.ID_comunic_conversations_list AND list.last_active = messages.time_insert"; AND list.id = messages.conv_id AND list.last_active = messages.time_insert";
$values = array($userID); $values = array($userID);
//Perform the request //Perform the request
@ -864,7 +898,7 @@ class Conversations {
public function getAllUserMessages(int $userID) : array { public function getAllUserMessages(int $userID) : array {
//Define conditions //Define conditions
$conditions = "WHERE ID_utilisateurs = ? "; $conditions = "WHERE user_id = ? ";
$condVals = array( $condVals = array(
$userID $userID
); );
@ -929,12 +963,12 @@ class Conversations {
private function getMessages(string $conditions, array $conditionsValues = array()) : array{ private function getMessages(string $conditions, array $conditionsValues = array()) : array{
//Prepare database request //Prepare database request
$tableName = $this->conversationsMessagesTable; $tableName = self::MESSAGES_TABLE;
//Define required fields //Define required fields
$requiredFields = array( $requiredFields = array(
"ID", "id",
"ID_utilisateurs AS ID_user", "user_id",
"image_path", "image_path",
"message", "message",
"time_insert" "time_insert"
@ -961,14 +995,14 @@ class Conversations {
$conv = new ConversationInfo(); $conv = new ConversationInfo();
$conv->set_id($entry["ID"]); $conv->set_id($entry["id"]);
$conv->set_id_owner($entry["ID_owner"]); $conv->set_id_owner($entry["owner_id"]);
$conv->set_last_active($entry["last_active"]); $conv->set_last_active($entry["last_active"]);
if($entry["name"] != null) if($entry["name"] != null)
$conv->set_name($entry["name"]); $conv->set_name($entry["name"]);
$conv->set_following($entry["following"] == 1); $conv->set_following($entry["following"] == 1);
$conv->set_saw_last_message($entry["saw_last_message"] == 1); $conv->set_saw_last_message($entry["saw_last_message"] == 1);
$conv->set_members($this->getConversationMembers($entry["ID"])); $conv->set_members($this->getConversationMembers($entry["id"]));
return $conv; return $conv;
@ -984,8 +1018,8 @@ class Conversations {
$message = new ConversationMessage(); $message = new ConversationMessage();
$message->set_id($entry["ID"]); $message->set_id($entry["id"]);
$message->set_userID($entry["ID_user"]); $message->set_userID($entry["user_id"]);
$message->set_time_sent($entry["time_insert"]); $message->set_time_sent($entry["time_insert"]);
if($entry["image_path"] != null) if($entry["image_path"] != null)
$message->set_image_path($entry["image_path"]); $message->set_image_path($entry["image_path"]);
@ -1006,11 +1040,11 @@ class Conversations {
$conversation = new UnreadConversation(); $conversation = new UnreadConversation();
$conversation->set_id($entry["ID_comunic_conversations_list"]); $conversation->set_id($entry["conv_id"]);
if($entry["name"] != null) if($entry["name"] != null)
$conversation->set_conv_name($entry["name"]); $conversation->set_conv_name($entry["name"]);
$conversation->set_last_active($entry["last_active"]); $conversation->set_last_active($entry["last_active"]);
$conversation->set_userID($entry["ID_utilisateurs"]); $conversation->set_userID($entry["user_id"]);
if($entry["message"] != null) if($entry["message"] != null)
$conversation->set_message($entry["message"]); $conversation->set_message($entry["message"]);

View File

@ -409,6 +409,19 @@ class friends {
} }
/**
* Count the number of friendship requests a user has received
*
* @param int $userID Target user ID
* @return int The number of friendship request the user received
*/
public function count_requests(int $userID) : int {
return db()->count(
$this->friendsTable,
"WHERE ID_personne = ? AND actif = 0",
array($userID));
}
/** /**
* Parse friend informations from the database * Parse friend informations from the database
* *

View File

@ -81,60 +81,60 @@ CREATE TABLE `comunic_api_limit_count` (
) ENGINE=InnoDB DEFAULT CHARSET=latin1; ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `comunic_API_ServicesToken`; DROP TABLE IF EXISTS `comunic_api_services_tokens`;
CREATE TABLE `comunic_API_ServicesToken` ( CREATE TABLE `comunic_api_services_tokens` (
`ID` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`time_insert` int(11) DEFAULT NULL, `time_insert` int(11) DEFAULT NULL,
`serviceName` varchar(255) NOT NULL, `service_name` varchar(255) NOT NULL,
`token` varchar(255) NOT NULL, `token` varchar(255) NOT NULL,
`client_domain` varchar(45) DEFAULT NULL, `client_domain` varchar(45) DEFAULT NULL,
PRIMARY KEY (`ID`) PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1; ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `comunic_API_userLoginToken`; DROP TABLE IF EXISTS `comunic_api_users_tokens`;
CREATE TABLE `comunic_API_userLoginToken` ( CREATE TABLE `comunic_api_users_tokens` (
`ID` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`ID_utilisateurs` int(11) NOT NULL, `user_id` int(11) NOT NULL,
`ID_comunic_API_ServicesToken` int(11) NOT NULL, `service_id` int(11) NOT NULL,
`token1` varchar(255) NOT NULL, `token1` varchar(255) NOT NULL,
`token2` varchar(255) NOT NULL, `token2` varchar(255) NOT NULL,
PRIMARY KEY (`ID`) PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1; ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `comunic_conversations_list`; DROP TABLE IF EXISTS `comunic_conversations_list`;
CREATE TABLE `comunic_conversations_list` ( CREATE TABLE `comunic_conversations_list` (
`ID` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`ID_utilisateurs` int(11) DEFAULT NULL, `user_id` int(11) DEFAULT NULL,
`name` varchar(50) DEFAULT NULL, `name` varchar(50) DEFAULT NULL,
`last_active` int(11) DEFAULT NULL, `last_active` int(11) DEFAULT NULL,
`creation_time` int(11) DEFAULT NULL, `creation_time` int(11) DEFAULT NULL,
PRIMARY KEY (`ID`) PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1; ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `comunic_conversations_messages`; DROP TABLE IF EXISTS `comunic_conversations_messages`;
CREATE TABLE `comunic_conversations_messages` ( CREATE TABLE `comunic_conversations_messages` (
`ID` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`ID_comunic_conversations_list` int(11) DEFAULT NULL, `conv_id` int(11) DEFAULT NULL,
`ID_utilisateurs` int(11) DEFAULT NULL, `user_id` int(11) DEFAULT NULL,
`time_insert` int(11) DEFAULT NULL, `time_insert` int(11) DEFAULT NULL,
`message` varchar(200) DEFAULT NULL, `message` varchar(200) DEFAULT NULL,
`image_path` varchar(100) DEFAULT NULL, `image_path` varchar(100) DEFAULT NULL,
PRIMARY KEY (`ID`) PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1; ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `comunic_conversations_users`; DROP TABLE IF EXISTS `comunic_conversations_users`;
CREATE TABLE `comunic_conversations_users` ( CREATE TABLE `comunic_conversations_users` (
`ID` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`ID_comunic_conversations_list` int(11) DEFAULT NULL, `conv_id` int(11) DEFAULT NULL,
`ID_utilisateurs` int(11) DEFAULT NULL, `user_id` int(11) DEFAULT NULL,
`time_add` int(11) DEFAULT NULL, `time_add` int(11) DEFAULT NULL,
`following` int(1) DEFAULT '0', `following` int(1) DEFAULT '0',
`saw_last_message` int(1) DEFAULT NULL, `saw_last_message` int(1) DEFAULT NULL,
PRIMARY KEY (`ID`) PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1; ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
DROP TABLE IF EXISTS `comunic_groups`; DROP TABLE IF EXISTS `comunic_groups`;
@ -171,7 +171,7 @@ CREATE TABLE `comunic_mails_queue` (
`id` int(11) NOT NULL AUTO_INCREMENT, `id` int(11) NOT NULL AUTO_INCREMENT,
`priority` int(11) DEFAULT NULL, `priority` int(11) DEFAULT NULL,
`time_insert` int(11) DEFAULT NULL, `time_insert` int(11) DEFAULT NULL,
`userID` int(11) DEFAULT NULL, `user_id` int(11) DEFAULT NULL,
`template` varchar(45) DEFAULT NULL, `template` varchar(45) DEFAULT NULL,
`data` text DEFAULT NULL, `data` text DEFAULT NULL,
PRIMARY KEY (`id`) PRIMARY KEY (`id`)