Implement ConversationInfo object parsing

This commit is contained in:
Pierre 2018-04-23 18:59:07 +02:00
parent 3dd0d3792a
commit 4ca3314236
2 changed files with 54 additions and 14 deletions

View File

@ -22,6 +22,10 @@ class conversationsController{
if($conversationsList === false)
Rest_fatal_error(500, "Couldn't get conversations list !");
//Process the list of conversation
foreach($conversationsList as $num => $conv)
$conversationsList[$num] = self::ConvInfoToAPI($conv);
//Return results
return $conversationsList;
}
@ -30,8 +34,9 @@ class conversationsController{
* Get informationsd about one conversation
*
* @url POST /conversations/getInfosOne
* @url POST /conversations/getInfoOne
*/
public function getOneInformations(){
public function getOneInformation(){
user_login_required();
//Get conversation ID
@ -50,7 +55,7 @@ class conversationsController{
" or the conversation doesn't exists !");
//Return conversation informations
return $conversationsList[0];
return self::ConvInfoToAPI($conversationsList[0]);
}
/**
@ -427,4 +432,26 @@ class conversationsController{
//The operation is a success
return array("success" => "The conversation has been deleted");
}
/**
* Turn a ConversationInfo object into a valid API entry
*
* @param ConversationInfo $conv Information about the conversation
* @return array Generated conversation object
*/
private static function ConvInfoToAPI(ConversationInfo $conv) : array {
$data = array();
$data["ID"] = $conv->get_id();
$data["ID_owner"] = $conv->get_id_owner();
$data["last_active"] = $conv->get_last_active();
$data["name"] = $conv->has_name() ? $conv->get_name() : false;
$data["following"] = $conv->is_following() ? 1 : 0;
$data["saw_last_message"] = $conv->is_saw_last_message() ? 1 : 0;
$data["members"] = $conv->get_members();
return $data;
}
}

View File

@ -83,18 +83,8 @@ class conversations {
//Process results
$conversationsList = array();
foreach($results as $processConversation){
$conversationsList[] = array(
"ID" => $processConversation["ID"],
"ID_owner" => $processConversation["ID_owner"],
"last_active" => $processConversation["last_active"],
"name" => ($processConversation["name"] == "" ? false : $processConversation["name"]),
"following" => $processConversation["following"],
"saw_last_message" => $processConversation["saw_last_message"],
//Get and add conversation members
"members" => $this->getConversationMembers($processConversation["ID"]),
);
foreach($results as $entry){
$conversationsList[] = $this->dbToConvInfo($entry);
}
//Return results
@ -861,6 +851,29 @@ class conversations {
return $messages;
}
/**
* Turn a conversation database entry into a ConversationInfo object
*
* @param array $entry Conversation entry in the database
* @return ConversationInfo Generated conversation information object
*/
private function dbToConvInfo(array $entry) : ConversationInfo {
$conv = new ConversationInfo();
$conv->set_id($entry["ID"]);
$conv->set_id_owner($entry["ID_owner"]);
$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"]));
return $conv;
}
}
//Register component