From 4ca33142363cac7209e6499ba3f60b419a74c971 Mon Sep 17 00:00:00 2001 From: Pierre Date: Mon, 23 Apr 2018 18:59:07 +0200 Subject: [PATCH] Implement ConversationInfo object parsing --- RestControllers/conversationsController.php | 31 +++++++++++++++-- classes/components/conversations.php | 37 ++++++++++++++------- 2 files changed, 54 insertions(+), 14 deletions(-) diff --git a/RestControllers/conversationsController.php b/RestControllers/conversationsController.php index 77bb8fb..43ed30a 100644 --- a/RestControllers/conversationsController.php +++ b/RestControllers/conversationsController.php @@ -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; + + } } \ No newline at end of file diff --git a/classes/components/conversations.php b/classes/components/conversations.php index 6741dda..cd9a4cf 100644 --- a/classes/components/conversations.php +++ b/classes/components/conversations.php @@ -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