From 205541f6d80a9a769ade0f88ef35e29e85915374 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 27 Jun 2020 14:37:49 +0200 Subject: [PATCH] Can request to join a group --- src/controllers/groups_controller.rs | 36 +++++++++++++++++++++++++++- src/controllers/routes.rs | 2 ++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/controllers/groups_controller.rs b/src/controllers/groups_controller.rs index f32949e..82e9aaa 100644 --- a/src/controllers/groups_controller.rs +++ b/src/controllers/groups_controller.rs @@ -13,11 +13,12 @@ use crate::constants::{DEFAULT_GROUP_LOGO, PATH_GROUPS_LOGOS}; use crate::controllers::routes::RequestResult; use crate::data::group::{Group, GroupAccessLevel, GroupPostsCreationLevel, GroupRegistrationLevel, GroupVisibilityLevel}; use crate::data::group_id::GroupID; -use crate::data::group_member::GroupMembershipLevel; +use crate::data::group_member::{GroupMember, GroupMembershipLevel}; use crate::data::http_request_handler::HttpRequestHandler; use crate::data::new_group::NewGroup; use crate::helpers::{groups_helper, virtual_directory_helper}; use crate::helpers::virtual_directory_helper::VirtualDirType; +use crate::utils::date_utils::time; /// Create a new group pub fn create(r: &mut HttpRequestHandler) -> RequestResult { @@ -192,4 +193,37 @@ pub fn respond_invitation(r: &mut HttpRequestHandler) -> RequestResult { // TODO : Create a notification r.success("Response to the invitation was successfully saved!") +} + +/// Send a request to join a group +pub fn send_request(r: &mut HttpRequestHandler) -> RequestResult { + let group_id = r.post_group_id_with_access("id", GroupAccessLevel::LIMITED_ACCESS)?; + + if groups_helper::get_membership_level(&group_id, r.user_id_opt())? != GroupMembershipLevel::VISITOR { + r.forbidden("You are not currently a visitor of the group!".to_string())?; + } + + let group = groups_helper::get_info(&group_id)?; + + let level = match group.registration_level { + GroupRegistrationLevel::OPEN_REGISTRATION => GroupMembershipLevel::MEMBER, + GroupRegistrationLevel::MODERATED_REGISTRATION => GroupMembershipLevel::PENDING, + GroupRegistrationLevel::CLOSED_REGISTRATION => { + r.forbidden("You are not authorized to send a registration request for this group!".to_string())?; + unreachable!(); + } + }; + + groups_helper::insert_member(&GroupMember { + id: 0, + user_id: r.user_id()?, + group_id, + time_create: time(), + level, + following: true, + })?; + + // TODO : Send a notification, if required + + r.success("The membership has been successfully saved!") } \ No newline at end of file diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index 62b2640..365f9f9 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -154,6 +154,8 @@ pub fn get_routes() -> Vec { Route::post("/groups/respond_invitation", Box::new(groups_controller::respond_invitation)), + Route::post("/groups/send_request", Box::new(groups_controller::send_request)), + // Virtual directory controller Route::post("/user/findbyfolder", Box::new(virtual_directory_controller::find_user)),