diff --git a/src/controllers/groups_controller.rs b/src/controllers/groups_controller.rs index 1e1e721..e584dcb 100644 --- a/src/controllers/groups_controller.rs +++ b/src/controllers/groups_controller.rs @@ -23,6 +23,7 @@ use crate::helpers::{conversations_helper, groups_helper, notifications_helper, use crate::helpers::virtual_directory_helper::VirtualDirType; use crate::routes::RequestResult; use crate::utils::date_utils::time; +use crate::data::conversation::Conversation; impl HttpRequestHandler { /// Get membership level for a conversation @@ -35,6 +36,18 @@ impl HttpRequestHandler { Ok(level) } + + /// Get a group conversation referenced in the request, for which current user is an admin + fn post_group_conv_admin(&mut self, name: &str) -> Res { + let conv_membership = self.post_conv_admin(name)?; + let conv = conversations_helper::get_single(conv_membership.conv_id)?; + + if !conv.is_linked_to_group() { + self.bad_request("This conversation is not linked to a group, it can not be deleted this way!".to_string())?; + } + + Ok(conv) + } } /// Create a new group @@ -179,14 +192,19 @@ pub fn create_conversation(r: &mut HttpRequestHandler) -> RequestResult { r.set_response(ResCreateConversationForGroup::new(conv_id)) } +/// Update a group's conversation settings +pub fn update_conversation(r: &mut HttpRequestHandler) -> RequestResult { + let conv = r.post_group_conv_admin("conv_id")?; + let min_level = r.post_group_membership_level_for_conversation("min_membership_level")?; + + conversations_helper::set_min_group_conversation_membership_level(conv.id, min_level)?; + + r.ok() +} + /// Delete a group's conversation pub fn delete_conversation(r: &mut HttpRequestHandler) -> RequestResult { - let conv_membership = r.post_conv_admin("conv_id")?; - let conv = conversations_helper::get_single(conv_membership.conv_id)?; - - if !conv.is_linked_to_group() { - r.bad_request("This conversation is not linked to a group, it can not deleted this way!".to_string())?; - } + let conv = r.post_group_conv_admin("conv_id")?; conversations_helper::delete_conversation(&conv)?; diff --git a/src/helpers/conversations_helper.rs b/src/helpers/conversations_helper.rs index 90ac08e..aecf375 100644 --- a/src/helpers/conversations_helper.rs +++ b/src/helpers/conversations_helper.rs @@ -183,6 +183,16 @@ pub fn set_settings(settings: NewConversationSettings) -> Res { .exec() } +/// Change minimal membership level to join a group conversation +pub fn set_min_group_conversation_membership_level(conv_id: ConvID, level: GroupMembershipLevel) -> Res { + database::UpdateInfo::new(CONV_LIST_TABLE) + .cond_conv_id("id", conv_id) + .set_u32("min_group_membership_level", level.to_db()) + .exec()?; + + update_members_list_for_group_conversation(conv_id) +} + /// Search for private conversation between two users pub fn find_private(user_1: &UserID, user_2: &UserID) -> ResultBoxError> { database::QueryInfo::new(CONV_MEMBERS_TABLE) diff --git a/src/routes.rs b/src/routes.rs index bea696d..21e5c76 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -228,6 +228,7 @@ pub fn get_routes() -> Vec { Route::post("/groups/upload_logo", Box::new(groups_controller::upload_logo)), Route::post("/groups/delete_logo", Box::new(groups_controller::delete_logo)), Route::post("/groups/create_conversation", Box::new(groups_controller::create_conversation)), + Route::post("/groups/update_conversation", Box::new(groups_controller::update_conversation)), Route::post("/groups/delete_conversation", Box::new(groups_controller::delete_conversation)), Route::post("/groups/get_members", Box::new(groups_controller::get_members)), Route::post("/groups/invite", Box::new(groups_controller::invite_user)),