Can cancel a membership request.

This commit is contained in:
Pierre HUBERT 2018-07-06 18:36:19 +02:00
parent c3bdbedb30
commit cd8fc40810
2 changed files with 51 additions and 5 deletions

View File

@ -260,6 +260,29 @@ class GroupsController {
return array("success" => "The response to the invitation was saved!"); 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 * Parse a GroupInfo object into an array for the API
* *

View File

@ -214,6 +214,22 @@ class GroupsComponent {
array($groupID, $userID)) > 0; 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 * Check whether a user received an invitation or not
* *
@ -255,11 +271,18 @@ class GroupsComponent {
* @return bool TRUE for a success / FALSE else * @return bool TRUE for a success / FALSE else
*/ */
public function deleteInvitation(int $userID, int $groupID) : bool { public function deleteInvitation(int $userID, int $groupID) : bool {
return db()->deleteEntry( return $this->deleteMembershipWithStatus($userID, $groupID, GroupMember::INVITED);
self::GROUPS_MEMBERS_TABLE, }
"groups_id = ? AND user_id = ? AND level = ?",
array($groupID, $userID, 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);
} }
/** /**