1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-26 07:19:22 +00:00

Can change admin status of conversation members

This commit is contained in:
Pierre HUBERT 2021-03-06 18:19:53 +01:00
parent 2bcf0706de
commit dcf194aa6f
3 changed files with 33 additions and 3 deletions

View File

@ -143,12 +143,36 @@ pub fn add_member(r: &mut HttpRequestHandler) -> RequestResult {
r.success("The user was added to the conversation!") r.success("The user was added to the conversation!")
} }
/// Update admin status of a user
pub fn set_admin(r: &mut HttpRequestHandler) -> RequestResult {
let conv_membership = r.post_conv("convID")?;
let conv = conversations_helper::get_single(conv_membership.conv_id)?;
let user_to_update = r.post_user_id("userID")?;
let set_admin = r.post_bool("setAdmin")?;
if conv.is_managed() {
r.bad_request("This conversation is managed, you can not manually change its members!".to_string())?;
}
if !conv.can_mark_other_users_admin(r.user_id_ref()?) {
r.forbidden("You are not allowed to make users admin in this conversation!".to_string())?;
}
if conv.get_membership(&user_to_update).is_none() {
r.bad_request("This user is not a member of this conversation!".to_string())?;
}
conversations_helper::set_admin(&conv.id, &user_to_update, set_admin)?;
r.success("The user was added to the conversation!")
}
/// Remove a member from a conversation /// Remove a member from a conversation
pub fn remove_member(r: &mut HttpRequestHandler) -> RequestResult { pub fn remove_member(r: &mut HttpRequestHandler) -> RequestResult {
let conv_membership = r.post_conv("convID")?; let conv_membership = r.post_conv("convID")?;
let conv = conversations_helper::get_single(conv_membership.conv_id)?; let conv = conversations_helper::get_single(conv_membership.conv_id)?;
let user_to_add = r.post_user_id("userID")?; let user_to_remove = r.post_user_id("userID")?;
if conv.is_managed() { if conv.is_managed() {
r.bad_request("This conversation is managed, you can not manually change its members!".to_string())?; r.bad_request("This conversation is managed, you can not manually change its members!".to_string())?;
@ -158,11 +182,11 @@ pub fn remove_member(r: &mut HttpRequestHandler) -> RequestResult {
r.forbidden("You are not allowed to remove members from this conversation!".to_string())?; r.forbidden("You are not allowed to remove members from this conversation!".to_string())?;
} }
if conv.get_membership(&user_to_add).is_none() { if conv.get_membership(&user_to_remove).is_none() {
r.bad_request("This user is not a member of this conversation!".to_string())?; r.bad_request("This user is not a member of this conversation!".to_string())?;
} }
conversations_helper::remove_member(&user_to_add, conv.id, r.user_id_ref()?)?; conversations_helper::remove_member(&user_to_remove, conv.id, r.user_id_ref()?)?;
r.ok() r.ok()
} }

View File

@ -75,6 +75,11 @@ impl Conversation {
!self.is_managed() && (self.is_admin(user_id) || self.can_everyone_add_members) !self.is_managed() && (self.is_admin(user_id) || self.can_everyone_add_members)
} }
/// Check out whether a user can mark other admin or not
pub fn can_mark_other_users_admin(&self, user_id: &UserID) -> bool {
!self.is_managed() && self.is_admin(user_id)
}
/// Check out whether a user can remove members from a conversation or not /// Check out whether a user can remove members from a conversation or not
pub fn can_user_remove_members(&self, user_id: &UserID) -> bool { pub fn can_user_remove_members(&self, user_id: &UserID) -> bool {
!self.is_managed() && self.is_admin(user_id) !self.is_managed() && self.is_admin(user_id)

View File

@ -195,6 +195,7 @@ pub fn get_routes() -> Vec<Route> {
Route::post("/conversations/get_single", Box::new(conversations_controller::get_single)), Route::post("/conversations/get_single", Box::new(conversations_controller::get_single)),
Route::post("/conversations/updateSettings", Box::new(conversations_controller::update_settings)), Route::post("/conversations/updateSettings", Box::new(conversations_controller::update_settings)),
Route::post("/conversations/addMember", Box::new(conversations_controller::add_member)), Route::post("/conversations/addMember", Box::new(conversations_controller::add_member)),
Route::post("/conversations/setAdmin", Box::new(conversations_controller::set_admin)),
Route::post("/conversations/removeMember", Box::new(conversations_controller::remove_member)), Route::post("/conversations/removeMember", Box::new(conversations_controller::remove_member)),
Route::post("/conversations/getPrivate", Box::new(conversations_controller::find_private)), Route::post("/conversations/getPrivate", Box::new(conversations_controller::find_private)),
Route::post("/conversations/refresh_single", Box::new(conversations_controller::refresh_single)), Route::post("/conversations/refresh_single", Box::new(conversations_controller::refresh_single)),