diff --git a/src/controllers/groups_controller.rs b/src/controllers/groups_controller.rs index 1921eb5..fce9165 100644 --- a/src/controllers/groups_controller.rs +++ b/src/controllers/groups_controller.rs @@ -18,4 +18,14 @@ pub fn create(r: &mut HttpRequestHandler) -> RequestResult { let group_id = groups_helper::create(&new_group)?; r.set_response(GroupCreationResult::new(&group_id)) +} + +/// 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::>(); + + r.set_response(list) } \ No newline at end of file diff --git a/src/controllers/routes.rs b/src/controllers/routes.rs index 57ee9d2..7fdf37e 100644 --- a/src/controllers/routes.rs +++ b/src/controllers/routes.rs @@ -1,6 +1,6 @@ use std::error::Error; -use crate::controllers::{account_controller, conversations_controller, search_controller, server_controller, user_controller, virtual_directory_controller, groups_controller}; +use crate::controllers::{account_controller, conversations_controller, groups_controller, search_controller, server_controller, user_controller, virtual_directory_controller}; use crate::controllers::routes::Method::{GET, POST}; use crate::data::http_request_handler::HttpRequestHandler; @@ -130,6 +130,8 @@ pub fn get_routes() -> Vec { // Groups controller Route::post("/groups/create", Box::new(groups_controller::create)), + Route::post("/groups/get_my_list", Box::new(groups_controller::get_list_user)), + // Virtual directory controller Route::post("/user/findbyfolder", Box::new(virtual_directory_controller::find_user)), diff --git a/src/helpers/groups_helper.rs b/src/helpers/groups_helper.rs index c1fcc95..01f39fc 100644 --- a/src/helpers/groups_helper.rs +++ b/src/helpers/groups_helper.rs @@ -8,6 +8,7 @@ use crate::data::group::GroupVisibilityLevel; use crate::data::group_id::GroupID; use crate::data::group_member::{GroupMember, GroupMembershipLevel}; use crate::data::new_group::NewGroup; +use crate::data::user::UserID; use crate::helpers::database; use crate::utils::date_utils::time; @@ -67,6 +68,19 @@ pub fn insert_member(m: &GroupMember) -> ResultBoxError<()> { .insert_drop_result() } +/// Get the list of groups of a user +pub fn get_list_user(user_id: UserID, only_followed: bool) -> ResultBoxError> { + let mut query = database::QueryInfo::new(GROUPS_MEMBERS_TABLE) + .add_field("groups_id") + .cond_user_id("user_id", user_id); + + if only_followed { + query = query.cond_legacy_bool("following", true); + } + + query.exec(|row| row.get_group_id("groups_id")) +} + /// Find a group id by virtual directory pub fn find_by_virtual_directory(dir: &str) -> ResultBoxError { database::QueryInfo::new(GROUPS_LIST_TABLE)