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");
}
/**
* 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
*

View File

@ -113,6 +113,10 @@ class friendsController{
//Get target ID
$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
if(CS::get()->components->friends->are_friend(userID, $friendID))
Rest_fatal_error(401, "The two personns are already friend !");

View File

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

View File

@ -12,18 +12,6 @@ class AccountComponent {
*/
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
*
@ -61,10 +49,10 @@ class AccountComponent {
$token2 = random_str(75);
//Insert token in the database
$tableName = $this->userLoginAPItable;
$tableName = APIClients::USERS_TOKENS_TABLE;
$insertValues = array(
"ID_utilisateurs" => $userID,
"ID_".CS::get()->config->get("dbprefix")."API_ServicesToken" => $serviceID,
"user_id" => $userID,
"service_id" => $serviceID,
"token1" => $token1,
"token2" => $token2
);
@ -84,12 +72,12 @@ class AccountComponent {
*/
private function getUserLoginTokenByIDs(int $userID, int $serviceID) {
//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(
$userID,
$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)
return false; //There is nobody at this address
@ -111,14 +99,14 @@ class AccountComponent {
public function deleteUserLoginToken(int $userID, string $serviceID) : bool {
//Prepare database request
$condition = "ID_utilisateurs = ? AND ID_".CS::get()->config->get("dbprefix")."API_ServicesToken = ?";
$condition = "user_id = ? AND service_id = ?";
$values = array(
$userID,
$serviceID
);
//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
//Everything is ok
@ -135,13 +123,13 @@ class AccountComponent {
public function deleteAllUserLoginTokens(int $userID) : bool {
//Prepare database request
$condition = "ID_utilisateurs = ?";
$condition = "user_id = ?";
$values = array(
$userID
);
//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
//Everything is ok
@ -162,8 +150,8 @@ class AccountComponent {
return 0;
//Prepare database request
$tablesName = $this->userLoginAPItable;
$conditions = "WHERE ".$this->userLoginAPItable.".ID_".CS::get()->config->get("dbprefix")."API_ServicesToken = ? AND ".$this->userLoginAPItable.".token1 = ? AND ".$this->userLoginAPItable.".token2 = ?";
$tablesName = APIClients::USERS_TOKENS_TABLE;
$conditions = "WHERE ".APIClients::USERS_TOKENS_TABLE.".service_id = ? AND ".APIClients::USERS_TOKENS_TABLE.".token1 = ? AND ".APIClients::USERS_TOKENS_TABLE.".token2 = ?";
$conditionsValues = array(
$serviceID,
$tokens[0],
@ -178,7 +166,7 @@ class AccountComponent {
return 0; //No result
//Return ID
return $userInfos[0]["ID_utilisateurs"];
return $userInfos[0]["user_id"];
}
/**

View File

@ -8,29 +8,11 @@
class Conversations {
/**
* @var String $conversationsListTable Name of the conversation list table
* Tables name definition
*/
private $conversationsListTable;
/**
* @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";
}
const LIST_TABLE = DBprefix."conversations_list";
const USERS_TABLE = DBprefix."conversations_users";
const MESSAGES_TABLE = DBprefix."conversations_messages";
/**
* Get the conversations list of a specified user
@ -43,19 +25,19 @@ class Conversations {
public function getList(int $userID, int $conversationID = 0){
//Prepare database request
$tablesName = $this->conversationsListTable.", ".$this->conversationsUsersTable;
$tablesName = self::LIST_TABLE.", ".self::USERS_TABLE;
//Prepare conditions
$tableJoinCondition = $this->conversationsListTable.".ID = ".$this->conversationsUsersTable.".ID_".$this->conversationsListTable."";
$userCondition = $this->conversationsUsersTable.".ID_utilisateurs = ?";
$orderResults = "ORDER BY ".$this->conversationsListTable.".last_active DESC";
$tableJoinCondition = self::LIST_TABLE.".id = ".self::USERS_TABLE.".conv_id";
$userCondition = self::USERS_TABLE.".user_id = ?";
$orderResults = "ORDER BY ".self::LIST_TABLE.".last_active DESC";
//Specify conditions values
$conditionsValues = array($userID);
//Check if we have to get informations about just one conversation
if($conversationID != 0){
$specificConditions = "AND ".$this->conversationsListTable.".ID = ?";
$specificConditions = "AND ".self::LIST_TABLE.".id = ?";
$conditionsValues[] = $conversationID;
}
else
@ -66,12 +48,12 @@ class Conversations {
//Fields list
$requiredFields = array(
$this->conversationsListTable.".ID",
$this->conversationsListTable.".last_active",
$this->conversationsListTable.".name",
$this->conversationsListTable.".ID_utilisateurs AS ID_owner",
$this->conversationsUsersTable.".following",
$this->conversationsUsersTable.".saw_last_message",
self::LIST_TABLE.".id",
self::LIST_TABLE.".last_active",
self::LIST_TABLE.".name",
self::LIST_TABLE.".user_id AS owner_id",
self::USERS_TABLE.".following",
self::USERS_TABLE.".saw_last_message",
);
//Perform database request
@ -101,10 +83,10 @@ class Conversations {
public function getConversationMembers(int $conversationID) : array {
//Perform a request on the database
$tableName = $this->conversationsUsersTable;
$conditions = "WHERE ID_".$this->conversationsListTable." = ?";
$tableName = self::USERS_TABLE;
$conditions = "WHERE conv_id = ?";
$conditionsValues = array($conversationID*1);
$getFields = array("ID_utilisateurs as userID");
$getFields = array("user_id");
//Perform the request
$results = CS::get()->db->select($tableName, $conditions, $conditionsValues, $getFields);
@ -116,7 +98,7 @@ class Conversations {
$membersList = array();
foreach($results as $processUser)
$membersList[] = $processUser["userID"];
$membersList[] = $processUser["user_id"];
//Return result
return $membersList;
@ -131,18 +113,18 @@ class Conversations {
public function create(ConversationInfo $conv) : int{
$mainInformations = array(
"ID_utilisateurs" => $conv->get_id_owner(),
"user_id" => $conv->get_id_owner(),
"name" => ($conv->has_name() ? $conv->get_name() : ""),
"last_active" => time(),
"creation_time" => time()
);
//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
//Get the last inserted ID
$conversationID = CS::get()->db->getLastInsertedID();
$conversationID = db()->getLastInsertedID();
//Check for errors
if($conversationID == 0)
@ -175,8 +157,8 @@ class Conversations {
public function userBelongsTo(int $userID, int $conversationID) : bool {
//Prepare a request on the database
$tableName = $this->conversationsUsersTable;
$conditions = "WHERE ID_".$this->conversationsListTable." = ? AND ID_utilisateurs = ?";
$tableName = self::USERS_TABLE;
$conditions = "WHERE conv_id = ? AND user_id = ?";
$values = array(
$conversationID,
$userID
@ -204,8 +186,8 @@ class Conversations {
public function changeFollowState(int $userID, int $conversationID, bool $follow) : bool{
//Prepare the request on the database
$tableName = $this->conversationsUsersTable;
$conditions = "ID_".$this->conversationsListTable." = ? AND ID_utilisateurs = ?";
$tableName = self::USERS_TABLE;
$conditions = "conv_id = ? AND user_id = ?";
$condVals = array(
$conversationID,
$userID
@ -233,8 +215,8 @@ class Conversations {
*/
public function changeName(int $conversationID, string $conversationName) : bool{
//Prepare database request
$tableName = $this->conversationsListTable;
$conditions = "ID = ?";
$tableName = self::LIST_TABLE;
$conditions = "id = ?";
$condVals = array($conversationID);
//Changes
@ -295,10 +277,10 @@ class Conversations {
private function addMember(int $conversationID, int $userID, bool $follow = false) : bool {
//Prepare database request
$tableName = $this->conversationsUsersTable;
$tableName = self::USERS_TABLE;
$values = array(
"ID_".$this->conversationsListTable => $conversationID,
"ID_utilisateurs" => $userID,
"conv_id" => $conversationID,
"user_id" => $userID,
"time_add" => time(),
"following" => $follow ? 1 : 0,
"saw_last_message" => 1
@ -317,8 +299,8 @@ class Conversations {
*/
private function removeMember(int $conversationID, int $userID) : bool {
//Prepare database request
$tableName = $this->conversationsUsersTable;
$conditions = "ID_".$this->conversationsListTable." = ? AND ID_utilisateurs = ?";
$tableName = self::USERS_TABLE;
$conditions = "conv_id = ? AND user_id = ?";
$values = array(
$conversationID,
$userID
@ -337,11 +319,11 @@ class Conversations {
*/
public function userIsModerator(int $userID, int $conversationID) : bool {
//Prepare database request
$tableName = $this->conversationsListTable;
$conditions = "WHERE ID = ?";
$tableName = self::LIST_TABLE;
$conditions = "WHERE id = ?";
$values = array($conversationID);
$requiredFields = array(
"ID_utilisateurs"
"user_id"
);
//Peform a request on the database
@ -356,7 +338,22 @@ class Conversations {
return false;
//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{
//Prepare database request
$tableName = $this->conversationsUsersTable." AS table1 JOIN ".
$this->conversationsUsersTable." AS table2 JOIN ".
$this->conversationsUsersTable." AS table3";
$tableName = self::USERS_TABLE." AS table1 JOIN ".
self::USERS_TABLE." AS table2 JOIN ".
self::USERS_TABLE." AS table3";
//Prepare conditions
$joinCondition = "(table1.ID_".$this->conversationsListTable." = table2.ID_".$this->conversationsListTable.")".
"AND (table1.ID_".$this->conversationsListTable." = table3.ID_".$this->conversationsListTable.")";
$whereConditions = "table1.ID_utilisateurs = ? AND table2.ID_utilisateurs = ?";
$groupCondition = "table1.ID_".$this->conversationsListTable." having count(*) = 2";
$joinCondition = "(table1.conv_id = table2.conv_id)".
"AND (table1.conv_id = table3.conv_id)";
$whereConditions = "table1.user_id = ? AND table2.user_id = ?";
$groupCondition = "table1.conv_id having count(*) = 2";
//Conditions values
$condValues = array($user1, $user2);
//Required fields
$requiredFields = array(
"table1.ID_".$this->conversationsListTable." as conversationID",
"table1.conv_id as conversationID",
);
//Build conditions
@ -415,11 +412,11 @@ class Conversations {
private function insertMessage(NewConversationMessage $message) : bool {
//Prepare values
$tableName = $this->conversationsMessagesTable;
$tableName = self::MESSAGES_TABLE;
$values = array(
"ID_".$this->conversationsListTable => $message->get_conversationID(),
"ID_utilisateurs" => $message->get_userID(),
"time_insert" => time(),
"conv_id" => $message->get_conversationID(),
"user_id" => $message->get_userID(),
"time_insert" => $message->get_time_sent(),
"message" => $message->has_message() ? $message->get_message() : ""
);
@ -445,8 +442,8 @@ class Conversations {
private function updateLastActive(int $conversationID, int $time) : bool{
//Perform a request on the database
$tableName = $this->conversationsListTable;
$conditions = "ID = ?";
$tableName = self::LIST_TABLE;
$conditions = "id = ?";
$condVals = array($conversationID);
//Set new values
@ -472,13 +469,13 @@ class Conversations {
private function allUsersAsUnread(int $conversationID, array $exceptions) : bool{
//Prepare request
$tableName = $this->conversationsUsersTable;
$conditions = "ID_".$this->conversationsListTable." = ?";
$tableName = self::USERS_TABLE;
$conditions = "conv_id = ?";
$condVals = array($conversationID);
//Remove users exceptions
foreach($exceptions as $userID){
$conditions.= " AND ID_utilisateurs != ?";
$conditions.= " AND user_id != ?";
$condVals[] = $userID;
}
@ -505,8 +502,8 @@ class Conversations {
public function markUserAsRead(int $userID, int $conversationID) : bool {
//Prepare database request
$tableName = $this->conversationsUsersTable;
$conditions = "ID_".$this->conversationsListTable." = ? AND ID_utilisateurs = ?";
$tableName = self::USERS_TABLE;
$conditions = "conv_id = ? AND user_id = ?";
$condVals = array(
$conversationID,
$userID
@ -536,12 +533,15 @@ class Conversations {
//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; ...)
//Set unique message time
$message->set_time_sent(time());
//First, try to insert the message
if(!$this->insertMessage($message))
return false; //An error occured
//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)
//Then, set all the users of the conversation as unread
@ -552,6 +552,24 @@ class Conversations {
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
*
@ -562,7 +580,7 @@ class Conversations {
public function getLastMessages(int $conversationID, int $numberOfMessages) : array {
//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(
$conversationID
);
@ -587,7 +605,7 @@ class Conversations {
public function getNewMessages(int $conversationID, int $lastMessageID) : array {
//Define conditions
$conditions = "WHERE ID_".$this->conversationsListTable." = ? AND ID > ? ORDER BY ID";
$conditions = "WHERE conv_id = ? AND ID > ? ORDER BY id";
$condVals = array(
$conversationID,
$lastMessageID
@ -611,7 +629,7 @@ class Conversations {
public function getOlderMessages(int $conversationID, int $startID, int $limit) : array {
//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(
$conversationID,
$startID + 1
@ -637,7 +655,7 @@ class Conversations {
public function getAllMessages(int $conversationID) : array {
//Define conditions
$conditions = "WHERE ID_".$this->conversationsListTable." = ? ORDER BY ID";
$conditions = "WHERE conv_id = ? ORDER BY id";
$condVals = array(
$conversationID
);
@ -658,9 +676,9 @@ class Conversations {
public function exist(int $convID) : bool {
//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 {
//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
foreach($messages as $message){
@ -733,7 +751,7 @@ class Conversations {
//Get all the messages of member the conversation
$messages = $this->getMessages(
"WHERE ID_".$this->conversationsListTable." = ? AND ID_utilisateurs = ?",
"WHERE conv_id = ? AND user_id = ?",
array($convID, $memberID));
//Delete each message
@ -750,6 +768,22 @@ class Conversations {
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
@ -774,7 +808,7 @@ class Conversations {
//Delete message from the database
$conditions = "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 {
//Prepare request on the database
$conditions = "ID_".$this->conversationsListTable." = ?";
$conditions = "conv_id = ?";
$values = array(
$convID
);
//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
*/
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 {
//Prepare database request
$tableName = $this->conversationsUsersTable;
$conditions = "WHERE ID_utilisateurs = ? AND saw_last_message = 0 AND following = 1";
$tableName = self::USERS_TABLE;
$conditions = "WHERE user_id = ? AND saw_last_message = 0 AND following = 1";
$values = array($userID);
//Perform request and return result
@ -834,9 +868,9 @@ class Conversations {
public function get_list_unread(int $userID) : array {
//Perform the request on the server
$tablesName = $this->conversationsUsersTable." as users, ".$this->conversationsListTable." as list, ".$this->conversationsMessagesTable." 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
AND list.ID = messages.ID_comunic_conversations_list AND list.last_active = messages.time_insert";
$tablesName = self::USERS_TABLE." as users, ".self::LIST_TABLE." as list, ".self::MESSAGES_TABLE." as messages";
$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.conv_id AND list.last_active = messages.time_insert";
$values = array($userID);
//Perform the request
@ -864,7 +898,7 @@ class Conversations {
public function getAllUserMessages(int $userID) : array {
//Define conditions
$conditions = "WHERE ID_utilisateurs = ? ";
$conditions = "WHERE user_id = ? ";
$condVals = array(
$userID
);
@ -929,12 +963,12 @@ class Conversations {
private function getMessages(string $conditions, array $conditionsValues = array()) : array{
//Prepare database request
$tableName = $this->conversationsMessagesTable;
$tableName = self::MESSAGES_TABLE;
//Define required fields
$requiredFields = array(
"ID",
"ID_utilisateurs AS ID_user",
"id",
"user_id",
"image_path",
"message",
"time_insert"
@ -961,14 +995,14 @@ class Conversations {
$conv = new ConversationInfo();
$conv->set_id($entry["ID"]);
$conv->set_id_owner($entry["ID_owner"]);
$conv->set_id($entry["id"]);
$conv->set_id_owner($entry["owner_id"]);
$conv->set_last_active($entry["last_active"]);
if($entry["name"] != null)
$conv->set_name($entry["name"]);
$conv->set_following($entry["following"] == 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;
@ -984,8 +1018,8 @@ class Conversations {
$message = new ConversationMessage();
$message->set_id($entry["ID"]);
$message->set_userID($entry["ID_user"]);
$message->set_id($entry["id"]);
$message->set_userID($entry["user_id"]);
$message->set_time_sent($entry["time_insert"]);
if($entry["image_path"] != null)
$message->set_image_path($entry["image_path"]);
@ -1006,11 +1040,11 @@ class Conversations {
$conversation = new UnreadConversation();
$conversation->set_id($entry["ID_comunic_conversations_list"]);
$conversation->set_id($entry["conv_id"]);
if($entry["name"] != null)
$conversation->set_conv_name($entry["name"]);
$conversation->set_last_active($entry["last_active"]);
$conversation->set_userID($entry["ID_utilisateurs"]);
$conversation->set_userID($entry["user_id"]);
if($entry["message"] != null)
$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
*

View File

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