diff --git a/RestControllers/GroupsController.php b/RestControllers/GroupsController.php index 2602957..f98511d 100644 --- a/RestControllers/GroupsController.php +++ b/RestControllers/GroupsController.php @@ -260,6 +260,29 @@ class GroupsController { return array("success" => "The response to the invitation was saved!"); } + /** + * Cancel a membership request + * + * @url POST /groups/cancel_request + */ + public function cancelRequest(){ + + user_login_required(); + + //Get the ID of the group (with basic access) + $groupID = getPostGroupIdWithAccess("id", GroupInfo::LIMITED_ACCESS); + + //Check if the user has created a membership request + if(components()->groups->getMembershipLevel(userID, $groupID) != GroupMember::PENDING) + Rest_fatal_error(401, "You did not send a membership request to this group!"); + + //Try to cancel membership request + if(!components()->groups->cancelRequest(userID, $groupID)) + Rest_fatal_error(500, "An error occurred while trying to cancel membership request!"); + + return array("success" => "The request has been successfully cancelled!"); + } + /** * Parse a GroupInfo object into an array for the API * diff --git a/classes/components/GroupsComponent.php b/classes/components/GroupsComponent.php index 892f632..e9ff1ea 100644 --- a/classes/components/GroupsComponent.php +++ b/classes/components/GroupsComponent.php @@ -214,6 +214,22 @@ class GroupsComponent { array($groupID, $userID)) > 0; } + /** + * Delete a user membership with a precise status + * + * @param int $userID Target user ID + * @param int $groupID Target group + * @param int $status The status of the membership to delete + * @return bool TRUE for a success / FALSE else + */ + private function deleteMembershipWithStatus(int $userID, int $groupID, int $status) : bool { + return db()->deleteEntry( + self::GROUPS_MEMBERS_TABLE, + "groups_id = ? AND user_id = ? AND level = ?", + array($groupID, $userID, $status) + ); + } + /** * Check whether a user received an invitation or not * @@ -255,11 +271,18 @@ class GroupsComponent { * @return bool TRUE for a success / FALSE else */ public function deleteInvitation(int $userID, int $groupID) : bool { - return db()->deleteEntry( - self::GROUPS_MEMBERS_TABLE, - "groups_id = ? AND user_id = ? AND level = ?", - array($groupID, $userID, GroupMember::INVITED) - ); + return $this->deleteMembershipWithStatus($userID, $groupID, GroupMember::INVITED); + } + + /** + * Cancel 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 cancelRequest(int $userID, int $groupID) : bool { + return $this->deleteMembershipWithStatus($userID, $groupID, GroupMember::PENDING); } /**