From 2d796d11bd1e24b3e933cf825b448f19fdd000b7 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 29 Jun 2020 08:31:07 +0200 Subject: [PATCH] Can respond to a group membership request --- src/controllers/groups_controller.rs | 17 +++++++++++++++++ src/controllers/routes.rs | 2 ++ src/helpers/groups_helper.rs | 8 ++++++++ 3 files changed, 27 insertions(+) diff --git a/src/controllers/groups_controller.rs b/src/controllers/groups_controller.rs index c44ad3b..e91c011 100644 --- a/src/controllers/groups_controller.rs +++ b/src/controllers/groups_controller.rs @@ -294,4 +294,21 @@ pub fn update_membership(r: &mut HttpRequestHandler) -> RequestResult { groups_helper::update_membership_level(&group_id, &user_id, new_level)?; r.success("User membership has been successfully updated!") +} + +/// Respond to a group membership request +pub fn respond_request(r: &mut HttpRequestHandler) -> RequestResult { + let group_id = r.post_group_id_with_access("groupID", GroupAccessLevel::MODERATOR_ACCESS)?; + let user_id = r.post_user_id("userID")?; + let accept = r.post_bool("accept")?; + + if groups_helper::get_membership_level(&group_id, Some(user_id.clone()))? != GroupMembershipLevel::PENDING { + r.forbidden("This user has not requested a membership for this group!".to_string())?; + } + + groups_helper::respond_request(&group_id, &user_id, accept)?; + + // TODO : Create a notification + + r.success("The response to the request has been successfully saved!") } \ No newline at end of file diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index be41116..4e94e78 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -162,6 +162,8 @@ pub fn get_routes() -> Vec { Route::post("/groups/update_membership_level", Box::new(groups_controller::update_membership)), + Route::post("/groups/respond_request", Box::new(groups_controller::respond_request)), + // Virtual directory controller Route::post("/user/findbyfolder", Box::new(virtual_directory_controller::find_user)), diff --git a/src/helpers/groups_helper.rs b/src/helpers/groups_helper.rs index 00105e6..352e195 100644 --- a/src/helpers/groups_helper.rs +++ b/src/helpers/groups_helper.rs @@ -401,6 +401,14 @@ pub fn respond_invitation(g: &GroupID, u: &UserID, accept: bool) -> ResultBoxErr } } +/// Respond to a group membership request +pub fn respond_request(group_id: &GroupID, user_id: &UserID, accept: bool) -> ResultBoxError { + match accept { + true => update_membership_level(&group_id, &user_id, GroupMembershipLevel::MEMBER), + false => delete_member(&group_id, &user_id), + } +} + /// Turn a database entry into a group struct fn db_to_group(row: &database::RowResult) -> ResultBoxError { let group_id = row.get_group_id("id")?;