From 50875adc3be8c0b84dfc291ef15b4d9a7cea0bcc Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 11 May 2019 17:48:03 +0200 Subject: [PATCH] Can get all the memberships of a user at once --- RestControllers/WebAppController.php | 81 ++++++++++++++++++++++++++ classes/components/GroupsComponent.php | 19 ++++++ 2 files changed, 100 insertions(+) create mode 100644 RestControllers/WebAppController.php diff --git a/RestControllers/WebAppController.php b/RestControllers/WebAppController.php new file mode 100644 index 0000000..50a38c5 --- /dev/null +++ b/RestControllers/WebAppController.php @@ -0,0 +1,81 @@ +friends->getList(userID); + + // Get the list of groups of the user + $groups = components()->groups->getListUser(userID); + + // Get last activities of groups + $groups_activity = array(); + foreach($groups as $group) + $groups_activity[components()->groups->getLastActivity($group)] = $group; + krsort($groups_activity); + $groups = array(); + foreach($groups_activity as $activity => $id) + $groups[] = array("id" => $id, "activity" => $activity); + + $out = array(); + while(count($friends) != 0 || count($groups) != 0) { + + if(count($friends) == 0) + $type = self::MEMBERSHIP_GROUP; + + else if(count($groups) == 0) + $type = self::MEMBERSHIP_FRIEND; + + else if($friends[0]->getLastActivityTime() > $groups[0]["activity"]) + $type = self::MEMBERSHIP_FRIEND; + + else + $type = self::MEMBERSHIP_GROUP; + + // In case of friend + if($type == self::MEMBERSHIP_FRIEND){ + $out[] = array( + "type" => $type, + "friend" => friendsController::parseFriendAPI(array_shift($friends)) + ); + } + + // In case of group + else { + $info = array_shift($groups); + $out[] = array( + "type" => $type, + "id" => $info["id"], + "last_activity" => $info["activity"] + ); + } + + } + + + return $out; + } + +} \ No newline at end of file diff --git a/classes/components/GroupsComponent.php b/classes/components/GroupsComponent.php index 513c705..678e603 100644 --- a/classes/components/GroupsComponent.php +++ b/classes/components/GroupsComponent.php @@ -172,6 +172,25 @@ class GroupsComponent { return $this->dbToAdvancedGroupInfo($info[0], null, TRUE); } + /** + * Get the timestamp of the estimated last activity on the group + * + * @param int $id The ID of the target group + * @return int The time of last activity on the group + */ + public function getLastActivity(int $id) : int { + + // Query the database + $posts = components()->posts->getGroupPosts($id, true, 0, 1); + + if(count($posts) == 0) + return 0; + + else + return $posts[0]->get_time_sent(); + + } + /** * Get a group settings *