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

Can delete group

This commit is contained in:
Pierre HUBERT 2021-01-20 19:20:28 +01:00
parent f031f7b4b3
commit e26d40183e
7 changed files with 76 additions and 6 deletions

View File

@ -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.")
}

View File

@ -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.")
}

View File

@ -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
}
}

View File

@ -300,7 +300,7 @@ pub fn export(user_id: &UserID) -> ResultBoxError<AccountExport> {
}
/// 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

View File

@ -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<Group> {
let group_id = row.get_group_id("id")?;

View File

@ -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<bool> {
database::QueryInfo::new(NOTIFICATIONS_TABLE)

View File

@ -334,6 +334,13 @@ pub fn export_all_posts_user(user_id: &UserID) -> ResultBoxError<Vec<Post>> {
.exec(db_to_post)
}
/// Get the entire list of posts of a given group
pub fn export_all_posts_group(group_id: &GroupID) -> ResultBoxError<Vec<Post>> {
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<UserID>) -> ResultBoxError<PostAccessLevel> {
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<Vec<Post>> {
database::QueryInfo::new(POSTS_TABLE)