2020-06-23 17:04:32 +00:00
|
|
|
//! # Groups controller
|
|
|
|
//!
|
|
|
|
//! @author Pierre Hubert
|
|
|
|
|
2020-06-25 07:26:58 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2020-06-25 11:45:21 +00:00
|
|
|
use crate::api_data::advanced_group_api::AdvancedGroupApi;
|
2020-06-25 07:26:58 +00:00
|
|
|
use crate::api_data::group_api::GroupApi;
|
2020-06-24 07:08:31 +00:00
|
|
|
use crate::api_data::res_create_group::GroupCreationResult;
|
2020-06-23 17:04:32 +00:00
|
|
|
use crate::controllers::routes::RequestResult;
|
2020-06-26 06:58:00 +00:00
|
|
|
use crate::data::group::{Group, GroupAccessLevel, GroupPostsCreationLevel, GroupRegistrationLevel, GroupVisibilityLevel};
|
2020-06-25 07:26:58 +00:00
|
|
|
use crate::data::group_id::GroupID;
|
2020-06-23 17:04:32 +00:00
|
|
|
use crate::data::http_request_handler::HttpRequestHandler;
|
2020-06-24 07:08:31 +00:00
|
|
|
use crate::data::new_group::NewGroup;
|
|
|
|
use crate::helpers::groups_helper;
|
2020-06-26 06:58:00 +00:00
|
|
|
use crate::helpers::virtual_directory_helper::VirtualDirType;
|
2020-06-23 17:04:32 +00:00
|
|
|
|
|
|
|
/// Create a new group
|
|
|
|
pub fn create(r: &mut HttpRequestHandler) -> RequestResult {
|
2020-06-24 07:08:31 +00:00
|
|
|
let new_group = NewGroup {
|
|
|
|
name: r.post_string_opt("name", 3, true)?,
|
|
|
|
owner_id: r.user_id()?,
|
|
|
|
};
|
|
|
|
|
|
|
|
let group_id = groups_helper::create(&new_group)?;
|
|
|
|
|
|
|
|
r.set_response(GroupCreationResult::new(&group_id))
|
2020-06-24 07:21:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the list of groups of the current user
|
|
|
|
pub fn get_list_user(r: &mut HttpRequestHandler) -> RequestResult {
|
|
|
|
let list = groups_helper::get_list_user(r.user_id()?, false)?
|
|
|
|
.iter()
|
|
|
|
.map(|f| f.id())
|
|
|
|
.collect::<Vec<u64>>();
|
|
|
|
|
|
|
|
r.set_response(list)
|
2020-06-24 11:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get information about a single group
|
|
|
|
pub fn get_info_single(r: &mut HttpRequestHandler) -> RequestResult {
|
|
|
|
let group_id = r.post_group_id_with_access("id", GroupAccessLevel::LIMITED_ACCESS)?;
|
2020-06-24 16:23:44 +00:00
|
|
|
let group = groups_helper::get_info(&group_id)?;
|
2020-06-24 11:34:09 +00:00
|
|
|
|
2020-06-25 06:16:20 +00:00
|
|
|
r.set_response(GroupApi::new(&group, r.user_id_opt())?)
|
2020-06-25 07:26:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get information about multiple users
|
|
|
|
pub fn get_info_multiple(r: &mut HttpRequestHandler) -> RequestResult {
|
|
|
|
let groups_id = r.post_numbers_list("list", 1)?;
|
|
|
|
|
|
|
|
let mut list = HashMap::new();
|
|
|
|
|
|
|
|
for id in groups_id {
|
|
|
|
let id = GroupID::new(id as u64);
|
|
|
|
|
|
|
|
if !groups_helper::exists(&id)? ||
|
|
|
|
groups_helper::get_access_level(&id, r.user_id_opt())? < GroupAccessLevel::LIMITED_ACCESS {
|
|
|
|
r.not_found(format!("Group {} not found!", id.id()))?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let group = groups_helper::get_info(&id)?;
|
|
|
|
|
|
|
|
list.insert(id.id().to_string(), GroupApi::new(&group, r.user_id_opt())?);
|
|
|
|
}
|
|
|
|
|
|
|
|
r.set_response(list)
|
2020-06-25 11:39:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get advanced information about a user
|
|
|
|
pub fn get_advanced_info(r: &mut HttpRequestHandler) -> RequestResult {
|
|
|
|
let group_id = r.post_group_id_with_access("id", GroupAccessLevel::VIEW_ACCESS)?;
|
|
|
|
let group = groups_helper::get_info(&group_id)?;
|
|
|
|
|
|
|
|
r.set_response(AdvancedGroupApi::new(&group, r.user_id_opt())?)
|
2020-06-25 11:45:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the settings of the group
|
|
|
|
pub fn get_settings(r: &mut HttpRequestHandler) -> RequestResult {
|
2020-06-25 15:52:52 +00:00
|
|
|
let group_id = r.post_group_id_with_access("id", GroupAccessLevel::ADMIN_ACCESS)?;
|
|
|
|
let group = groups_helper::get_info(&group_id)?;
|
|
|
|
|
|
|
|
// For now, this method is the same as the get advanced info method,
|
|
|
|
// but this might change in the future...
|
|
|
|
r.set_response(AdvancedGroupApi::new(&group, r.user_id_opt())?)
|
2020-06-25 15:55:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Set new settings to the group
|
|
|
|
pub fn set_settings(r: &mut HttpRequestHandler) -> RequestResult {
|
2020-06-26 06:58:00 +00:00
|
|
|
let group_id = r.post_group_id_with_access("id", GroupAccessLevel::ADMIN_ACCESS)?;
|
|
|
|
|
|
|
|
let new_settings = Group {
|
|
|
|
id: group_id.clone(),
|
|
|
|
name: r.post_string_without_html("name", 3, true)?,
|
|
|
|
visibility: GroupVisibilityLevel::from_api(&r.post_string("visibility")?),
|
|
|
|
registration_level: GroupRegistrationLevel::from_api(&r.post_string("registration_level")?),
|
|
|
|
posts_creation_level: GroupPostsCreationLevel::from_api(&r.post_string("posts_level")?),
|
|
|
|
logo: None,
|
|
|
|
virtual_directory: r.post_checked_virtual_directory_opt("virtual_directory", group_id.id(), VirtualDirType::GROUP)?,
|
|
|
|
time_create: 0,
|
|
|
|
description: r.post_string_without_html_opt("description", 0)?,
|
|
|
|
url: r.post_url_opt("url", false)?,
|
|
|
|
};
|
|
|
|
|
|
|
|
println!("New settings: {:#?}", new_settings);
|
|
|
|
|
|
|
|
r.success("complete implementation")
|
2020-06-23 17:04:32 +00:00
|
|
|
}
|