diff --git a/src/api_data/advanced_group_api.rs b/src/api_data/advanced_group_api.rs new file mode 100644 index 0000000..96cfad9 --- /dev/null +++ b/src/api_data/advanced_group_api.rs @@ -0,0 +1,37 @@ +//! # Advanced group information +//! +//! @author Pierre Hubert +use serde::Serialize; + +use crate::api_data::group_api::GroupApi; +use crate::data::error::ResultBoxError; +use crate::data::group::Group; +use crate::data::user::UserID; +use crate::helpers::likes_helper; +use crate::helpers::likes_helper::LikeType; + +#[derive(Serialize)] +pub struct AdvancedGroupApi { + #[serde(flatten)] + base_info: GroupApi, + + time_create: u64, + description: String, + url: String, + number_likes: u64, + is_liking: bool, +} + +impl AdvancedGroupApi { + /// Construct a new advanced group membership instance + pub fn new(g: &Group, user_id: Option) -> ResultBoxError { + Ok(AdvancedGroupApi { + base_info: GroupApi::new(g, user_id.clone())?, + time_create: g.time_create, + description: g.description.clone().unwrap_or("null".to_string()), + url: g.url.clone().unwrap_or("null".to_string()), + number_likes: likes_helper::count(g.id.id(), LikeType::GROUP)? as u64, + is_liking: likes_helper::is_liking(&user_id.unwrap_or(UserID::invalid()), g.id.id(), LikeType::GROUP)?, + }) + } +} \ No newline at end of file diff --git a/src/api_data/mod.rs b/src/api_data/mod.rs index 785f64a..2c9370f 100644 --- a/src/api_data/mod.rs +++ b/src/api_data/mod.rs @@ -24,4 +24,5 @@ pub mod res_count_unread_conversations; pub mod list_unread_conversations_api; pub mod global_search_result_api; pub mod res_create_group; -pub mod group_api; \ No newline at end of file +pub mod group_api; +pub mod advanced_group_api; \ No newline at end of file diff --git a/src/controllers/groups_controller.rs b/src/controllers/groups_controller.rs index 4d4583e..af52608 100644 --- a/src/controllers/groups_controller.rs +++ b/src/controllers/groups_controller.rs @@ -12,6 +12,7 @@ use crate::data::group_id::GroupID; use crate::data::http_request_handler::HttpRequestHandler; use crate::data::new_group::NewGroup; use crate::helpers::groups_helper; +use crate::api_data::advanced_group_api::AdvancedGroupApi; /// Create a new group pub fn create(r: &mut HttpRequestHandler) -> RequestResult { @@ -63,4 +64,12 @@ pub fn get_info_multiple(r: &mut HttpRequestHandler) -> RequestResult { } r.set_response(list) +} + +/// 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())?) } \ No newline at end of file diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index 0a8b493..f1e8a0b 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -136,6 +136,8 @@ pub fn get_routes() -> Vec { Route::post("/groups/get_multiple_info", Box::new(groups_controller::get_info_multiple)), + Route::post("/groups/get_advanced_info", Box::new(groups_controller::get_advanced_info)), + // Virtual directory controller Route::post("/user/findbyfolder", Box::new(virtual_directory_controller::find_user)), diff --git a/src/data/user.rs b/src/data/user.rs index cebfadb..9868d8c 100644 --- a/src/data/user.rs +++ b/src/data/user.rs @@ -12,6 +12,11 @@ impl UserID { UserID(id) } + /// Create a new invalid user id + pub fn invalid() -> UserID { + UserID(0) + } + /// Get the current ID stored in this structure pub fn id(&self) -> u64 { self.0