From 6c100fecce7a1e1593e074b0902facf613f3119b Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 19 Jul 2018 14:43:29 +0200 Subject: [PATCH] Can update following status of a group membership. --- RestControllers/GroupsController.php | 22 ++++++++++++++++++++++ classes/components/GroupsComponent.php | 16 ++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/RestControllers/GroupsController.php b/RestControllers/GroupsController.php index 01662ac..dfb6a62 100644 --- a/RestControllers/GroupsController.php +++ b/RestControllers/GroupsController.php @@ -626,6 +626,28 @@ class GroupsController { return array("success" => "Your membership has been successfully deleted!"); } + /** + * Set whether a user is following a group or not + * + * @url POST groups/set_following + */ + public function setFollowing(){ + user_login_required(); + + //Get the group + $groupID = getPostGroupIdWithAccess("groupID", GroupInfo::MEMBER_ACCESS); + + //Get following status + $following = postBool("follow"); + + //Save the new value + if(!components()->groups->setFollowing($groupID, userID, $following)) + Rest_fatal_error(500, "Could not update following status!"); + + //Success + return array("success" => "Follow status has been successfully updated!"); + } + /** * Parse a GroupInfo object into an array for the API * diff --git a/classes/components/GroupsComponent.php b/classes/components/GroupsComponent.php index 622a090..cb364f4 100644 --- a/classes/components/GroupsComponent.php +++ b/classes/components/GroupsComponent.php @@ -622,6 +622,22 @@ class GroupsComponent { return $groupID == $currID; } + /** + * Set (update) user following status + * + * @param int $groupID Target group ID + * @param int $userID Target user ID + * @param bool $following New following status + * @return bool TRUE to follow / FALSE else + */ + public function setFollowing(int $groupID, int $userID, bool $following) : bool { + return db()->updateDB( + self::GROUPS_MEMBERS_TABLE, + "groups_id = ? AND user_id = ?", + array("following" => $following ? 1 : 0), + array($groupID, $userID)); + } + /** * Turn a database entry into a GroupInfo object *