1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-22 21:39:21 +00:00

Can remove a member from a conversation

This commit is contained in:
Pierre HUBERT 2021-03-06 18:09:50 +01:00
parent 72a9553f1a
commit 2bcf0706de
4 changed files with 39 additions and 2 deletions

View File

@ -135,7 +135,7 @@ pub fn add_member(r: &mut HttpRequestHandler) -> RequestResult {
} }
if conv.get_membership(&user_to_add).is_some() { if conv.get_membership(&user_to_add).is_some() {
r.bad_request("This user is already a member of this group!".to_string())?; r.bad_request("This user is already a member of this conversation!".to_string())?;
} }
conversations_helper::add_member(conv.id, &user_to_add, true, false, r.user_id_ref()?)?; conversations_helper::add_member(conv.id, &user_to_add, true, false, r.user_id_ref()?)?;
@ -143,6 +143,30 @@ 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!")
} }
/// Remove a member from a conversation
pub fn remove_member(r: &mut HttpRequestHandler) -> RequestResult {
let conv_membership = r.post_conv("convID")?;
let conv = conversations_helper::get_single(conv_membership.conv_id)?;
let user_to_add = r.post_user_id("userID")?;
if conv.is_managed() {
r.bad_request("This conversation is managed, you can not manually change its members!".to_string())?;
}
if !conv.can_user_remove_members(r.user_id_ref()?) {
r.forbidden("You are not allowed to remove members from this conversation!".to_string())?;
}
if conv.get_membership(&user_to_add).is_none() {
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()?)?;
r.ok()
}
/// Find a private conversation /// Find a private conversation
pub fn find_private(r: &mut HttpRequestHandler) -> RequestResult { pub fn find_private(r: &mut HttpRequestHandler) -> RequestResult {
let other_user = r.post_user_id("otherUser")?; let other_user = r.post_user_id("otherUser")?;

View File

@ -5,6 +5,7 @@
use std::collections::HashSet; use std::collections::HashSet;
use std::error::Error; use std::error::Error;
use std::str::from_utf8;
use exif::In; use exif::In;
use image::{GenericImageView, ImageFormat}; use image::{GenericImageView, ImageFormat};
@ -31,7 +32,6 @@ use crate::utils::string_utils::{check_emoji_code, check_html_color, check_strin
use crate::utils::user_data_utils::{generate_new_user_data_file_name, prepare_file_creation, user_data_path}; use crate::utils::user_data_utils::{generate_new_user_data_file_name, prepare_file_creation, user_data_path};
use crate::utils::virtual_directories_utils; use crate::utils::virtual_directories_utils;
use crate::utils::zip_utils::is_valid_zip; use crate::utils::zip_utils::is_valid_zip;
use std::str::from_utf8;
#[derive(Serialize)] #[derive(Serialize)]
struct SuccessMessage { struct SuccessMessage {
@ -80,6 +80,13 @@ pub trait BaseRequestHandler {
}) })
} }
/// Success without message
fn ok(&mut self) -> RequestResult {
self.set_response(SuccessMessage {
success: "OK.".to_string()
})
}
/// Internal error response (500) /// Internal error response (500)
fn internal_error(&mut self, error: Box<dyn Error>) -> RequestResult { fn internal_error(&mut self, error: Box<dyn Error>) -> RequestResult {
self.set_error(HttpError::internal_error("Internal server error.")); self.set_error(HttpError::internal_error("Internal server error."));

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 remove members from a conversation or not
pub fn can_user_remove_members(&self, user_id: &UserID) -> bool {
!self.is_managed() && self.is_admin(user_id)
}
/// Check out whether a user is the last administrator of a conversation or not /// Check out whether a user is the last administrator of a conversation or not
pub fn is_last_admin(&self, user_id: &UserID) -> bool { pub fn is_last_admin(&self, user_id: &UserID) -> bool {
let admins: Vec<&ConversationMember> = self.members let admins: Vec<&ConversationMember> = self.members

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/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)),
Route::post("/conversations/get_older_messages", Box::new(conversations_controller::get_older_messages)), Route::post("/conversations/get_older_messages", Box::new(conversations_controller::get_older_messages)),