diff --git a/src/controllers/account_controller.rs b/src/controllers/account_controller.rs index ca0c158..4492320 100644 --- a/src/controllers/account_controller.rs +++ b/src/controllers/account_controller.rs @@ -199,7 +199,7 @@ pub fn delete_account(r: &mut HttpRequestHandler) -> RequestResult { r.forbidden("You shall not delete MY account (whoever you are, please note that hacking is bad !!!)".to_string())?; } - account_helper::delete(r.user_id_ref()?); + account_helper::delete(r.user_id_ref()?)?; r.success("Account deleted.") } \ No newline at end of file diff --git a/src/controllers/groups_controller.rs b/src/controllers/groups_controller.rs index 29b7ebd..f586061 100644 --- a/src/controllers/groups_controller.rs +++ b/src/controllers/groups_controller.rs @@ -11,7 +11,6 @@ use crate::api_data::res_change_group_logo::ResChangeGroupLogo; use crate::api_data::res_create_group::GroupCreationResult; use crate::constants::{DEFAULT_GROUP_LOGO, PATH_GROUPS_LOGOS}; use crate::controllers::routes::RequestResult; -use crate::data::error::ExecError; use crate::data::group::{Group, GroupAccessLevel, GroupPostsCreationLevel, GroupRegistrationLevel, GroupVisibilityLevel}; use crate::data::group_id::GroupID; use crate::data::group_member::{GroupMember, GroupMembershipLevel}; @@ -367,6 +366,10 @@ pub fn set_following(r: &mut HttpRequestHandler) -> RequestResult { /// Delete a group pub fn delete_group(r: &mut HttpRequestHandler) -> RequestResult { - // TODO : implement groups deletion - r.internal_error(ExecError::boxed_new("Method not implemented yet")) + let group_id = r.post_group_id_with_access("groupID", GroupAccessLevel::ADMIN_ACCESS)?; + r.need_user_password("password")?; + + groups_helper::delete(&group_id)?; + + r.success("Group deleted.") } \ No newline at end of file diff --git a/src/data/notification.rs b/src/data/notification.rs index 8463f7a..02dbaf3 100644 --- a/src/data/notification.rs +++ b/src/data/notification.rs @@ -215,4 +215,14 @@ impl PartialNotification { self.dest_user_id = Some(id.clone()); self } + + pub fn set_on_elem_id(mut self, id: u64) -> PartialNotification { + self.on_elem_id = Some(id); + self + } + + pub fn set_on_elem_type(mut self, t: NotifElemType) -> PartialNotification { + self.on_elem_type = Some(t); + self + } } \ No newline at end of file diff --git a/src/helpers/account_helper.rs b/src/helpers/account_helper.rs index 17f0d4e..2eab8b6 100644 --- a/src/helpers/account_helper.rs +++ b/src/helpers/account_helper.rs @@ -300,7 +300,7 @@ pub fn export(user_id: &UserID) -> ResultBoxError { } /// Delete a user's account -pub fn delete(user_id: &UserID) -> ResultBoxError { +pub fn delete(_user_id: &UserID) -> ResultBoxError { // TODO : close all websockets of user // TODO : Delete all group membership diff --git a/src/helpers/groups_helper.rs b/src/helpers/groups_helper.rs index 2e59689..5a2f057 100644 --- a/src/helpers/groups_helper.rs +++ b/src/helpers/groups_helper.rs @@ -9,7 +9,8 @@ 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, posts_helper}; +use crate::helpers::{database, likes_helper, notifications_helper, posts_helper}; +use crate::helpers::likes_helper::LikeType; use crate::utils::date_utils::time; impl GroupVisibilityLevel { @@ -462,6 +463,32 @@ pub fn can_user_create_posts(group_id: &GroupID, user_id: &UserID) -> ResultBoxE } } +/// Delete a group +pub fn delete(group_id: &GroupID) -> ResultBoxError { + // Delete all likes of the group + likes_helper::delete_all(group_id.id(), LikeType::GROUP)?; + + // Delete the logo of the group + delete_logo(group_id)?; + + // Delete all group posts + posts_helper::delete_all_group(group_id)?; + + // Delete all group related notifications + notifications_helper::delete_all_related_with_group(group_id)?; + + // Delete all group members + database::DeleteQuery::new(GROUPS_MEMBERS_TABLE) + .cond_group_id("groups_id", group_id) + .exec()?; + + // Delete group information + database::DeleteQuery::new(GROUPS_LIST_TABLE) + .cond_group_id("id", group_id) + .exec() +} + + /// Turn a database entry into a group struct fn db_to_group(row: &database::RowResult) -> ResultBoxError { let group_id = row.get_group_id("id")?; diff --git a/src/helpers/notifications_helper.rs b/src/helpers/notifications_helper.rs index 8f517fb..e087371 100644 --- a/src/helpers/notifications_helper.rs +++ b/src/helpers/notifications_helper.rs @@ -6,6 +6,7 @@ use std::collections::HashMap; use crate::constants::database_tables_names::NOTIFICATIONS_TABLE; use crate::data::error::ResultBoxError; +use crate::data::group_id::GroupID; use crate::data::notification::{NotifElemType, NotifEventType, NotifEventVisibility, Notification, PartialNotification}; use crate::data::user::UserID; use crate::helpers::database; @@ -26,6 +27,19 @@ pub fn delete_all_user(user_id: &UserID) -> ResultBoxError { delete(&PartialNotification::new().set_dest_user_id(user_id)) } +/// Delete all the notifications related with a group +pub fn delete_all_related_with_group(group_id: &GroupID) -> ResultBoxError { + delete(&PartialNotification::new() + .set_on_elem_type(NotifElemType::GROUP_MEMBERSHIP) + .set_on_elem_id(group_id.id()) + )?; + + delete(&PartialNotification::new() + .set_on_elem_type(NotifElemType::GROUP_PAGE) + .set_on_elem_id(group_id.id()) + ) +} + /// Check out whether a similar notification exists for given specifications pub fn similar_exists(n: &PartialNotification) -> ResultBoxError { database::QueryInfo::new(NOTIFICATIONS_TABLE) diff --git a/src/helpers/posts_helper.rs b/src/helpers/posts_helper.rs index a04dc03..0719de0 100644 --- a/src/helpers/posts_helper.rs +++ b/src/helpers/posts_helper.rs @@ -334,6 +334,13 @@ pub fn export_all_posts_user(user_id: &UserID) -> ResultBoxError> { .exec(db_to_post) } +/// Get the entire list of posts of a given group +pub fn export_all_posts_group(group_id: &GroupID) -> ResultBoxError> { + database::QueryInfo::new(POSTS_TABLE) + .cond_group_id("group_id", group_id) + .exec(db_to_post) +} + /// Get the access level of a user over a post pub fn get_access_level(p: &Post, user_id: &Option) -> ResultBoxError { if user_id == &p.user_id.as_option() { @@ -450,6 +457,15 @@ pub fn delete(p: &Post) -> ResultBoxError { Ok(()) } +/// Delete all the posts related with a group +pub fn delete_all_group(group_id: &GroupID) -> ResultBoxError { + for post in export_all_posts_group(group_id)? { + delete(&post)?; + } + + Ok(()) +} + /// Get all the posts that use of movie pub fn get_posts_for_movie(m: &Movie) -> ResultBoxError> { database::QueryInfo::new(POSTS_TABLE)