diff --git a/RestControllers/GroupsController.php b/RestControllers/GroupsController.php index f97c509..eb703f3 100644 --- a/RestControllers/GroupsController.php +++ b/RestControllers/GroupsController.php @@ -382,6 +382,36 @@ class GroupsController { return array("success" => "The membership has been successfully deleted!"); } + /** + * Respond to a membership request + * + * @url POST /groups/respond_request + */ + public function respondRequest() : array { + + user_login_required(); + + //Get the ID of the target group + $groupID = getPostGroupIdWithAccess("groupID", GroupInfo::MODERATOR_ACCESS); + + //Get user ID + $userID = getPostUserID("userID"); + + //Get the response + $accept = postBool("accept"); + + //Check if the user membership is really pending or not + if(components()->groups->getMembershipLevel($userID, $groupID) != GroupMember::PENDING) + Rest_fatal_error(401, "This user has not requested a membership in this group!"); + + //Respond to the request + if(!components()->groups->respondRequest($userID, $groupID, $accept)) + Rest_fatal_error(500, "Could not respond to the membership request!"); + + //Success + return array("success" => "The response to the request has been successfully saved!"); + } + /** * Parse a GroupInfo object into an array for the API * diff --git a/classes/components/GroupsComponent.php b/classes/components/GroupsComponent.php index cc88a84..1c4a717 100644 --- a/classes/components/GroupsComponent.php +++ b/classes/components/GroupsComponent.php @@ -296,6 +296,24 @@ class GroupsComponent { return $this->updateMembershipLevel($userID, $groupID, GroupMember::MEMBER); } + /** + * Respond to a membership request + * + * @param int $userID The ID of the target user + * @param int $groupID The ID of the related group + * @param bool $accept Set whether the request was accepted or not + * @return bool TRUE for a success / FALSE else + */ + public function respondRequest(int $userID, int $groupID, bool $accept) : bool { + + //If the user reject the invitation, delete it + if(!$accept) + return $this->deleteRequest($userID, $groupID); + + //Upgrade the user as member + return $this->updateMembershipLevel($userID, $groupID, GroupMember::MEMBER); + } + /** * Delete a membership invitation * @@ -307,6 +325,17 @@ class GroupsComponent { return $this->deleteMembershipWithStatus($userID, $groupID, GroupMember::INVITED); } + /** + * Delete a membership request + * + * @param int $userID The ID of the target user + * @param int $groupID The ID of the related group + * @return bool TRUE for a success / FALSE else + */ + public function deleteRequest(int $userID, int $groupID) : bool { + return $this->deleteMembershipWithStatus($userID, $groupID, GroupMember::PENDING); + } + /** * Cancel a membership request *