diff --git a/src/controllers/GroupsController.ts b/src/controllers/GroupsController.ts index 657b51c..d9a57f3 100644 --- a/src/controllers/GroupsController.ts +++ b/src/controllers/GroupsController.ts @@ -570,8 +570,9 @@ export class GroupsController { await h.needUserPostPassword("password"); - // TODO : implement method - h.error(500, "Method not implemented yet!"); + await GroupsHelper.Delete(groupID); + + h.success(); } /** diff --git a/src/helpers/GroupsHelper.ts b/src/helpers/GroupsHelper.ts index 8faa61f..915db8b 100644 --- a/src/helpers/GroupsHelper.ts +++ b/src/helpers/GroupsHelper.ts @@ -6,6 +6,8 @@ import { time } from "../utils/DateUtils"; import { GroupSettings } from "../entities/GroupSettings"; import { existsSync, unlinkSync } from "fs"; import { PostsHelper } from "./PostsHelper"; +import { LikesHelper, LikesType } from "./LikesHelper"; +import { NotificationsHelper } from "./NotificationsHelper"; /** * Groups helper @@ -585,6 +587,28 @@ export class GroupsHelper { return false; } + /** + * Permanently delete a group + * + * @param groupID Target group ID + */ + public static async Delete(groupID: number) { + + // Delete all likes of the group + await LikesHelper.DeleteAll(groupID, LikesType.GROUP); + + // Delete the logo of the group + await this.DeleteLogo(groupID); + + // Delete all group posts + await PostsHelper.DeleteAllGroup(groupID); + + // Delete all group related notifications + await NotificationsHelper.DeleteAllRelatedWithGroup(groupID); + + // TODO : continue deletion + } + /** * Turn a database row into a {GroupInfo} object * diff --git a/src/helpers/NotificationsHelper.ts b/src/helpers/NotificationsHelper.ts index 3d3b311..9ada9b5 100644 --- a/src/helpers/NotificationsHelper.ts +++ b/src/helpers/NotificationsHelper.ts @@ -266,6 +266,22 @@ export class NotificationsHelper { })); } + /** + * Delete all the notifications related with a group + * + * @param groupID Target group ID + */ + public static async DeleteAllRelatedWithGroup(groupID: number) { + const n = new Notif({ + onElemType: NotifElemType.GROUP_MEMBERSHIP, + onElemID: groupID + }); + await this.Delete(n); + + n.onElemType = NotifElemType.GROUP_PAGE; + await this.Delete(n); + } + /** * Count the number of unread notifications of a user * diff --git a/src/helpers/PostsHelper.ts b/src/helpers/PostsHelper.ts index bf1f77f..b51c978 100644 --- a/src/helpers/PostsHelper.ts +++ b/src/helpers/PostsHelper.ts @@ -358,6 +358,22 @@ export class PostsHelper { return list.map((l) => this.DBToPost(l)); } + /** + * Delete all the posts of a given group + * + * @param groupID Target group ID + */ + public static async ExportAllPostsGroup(groupID: number) : Promise { + const list = await DatabaseHelper.Query({ + table: TABLE_NAME, + where: { + group_id: groupID + } + }); + + return list.map((l) => this.DBToPost(l)); + } + /** * Get the access level of a user over a post * @@ -534,6 +550,16 @@ export class PostsHelper { }); } + /** + * Delete all the posts of a given group + * + * @param groupID Target group ID + */ + public static async DeleteAllGroup(groupID: number) { + for(const post of await this.ExportAllPostsGroup(groupID)) + await this.Delete(post.id); + } + /** * Turn a database entry into a row object *