From bc3ace278bda6a1a462dcc2dffee54b2d0b2b992 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Espino=20Garc=C3=ADa?= Date: Wed, 22 Feb 2023 16:20:29 +0100 Subject: [PATCH] Remove posts in thread only when removing root posts (#7116) --- app/queries/servers/post.ts | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/app/queries/servers/post.ts b/app/queries/servers/post.ts index 34fb538c1f..8148d8dee5 100644 --- a/app/queries/servers/post.ts +++ b/app/queries/servers/post.ts @@ -18,7 +18,7 @@ const {SERVER: {POST, POSTS_IN_CHANNEL, POSTS_IN_THREAD}} = MM_TABLES; export const prepareDeletePost = async (post: PostModel): Promise => { const preparedModels: Model[] = [post.prepareDestroyPermanently()]; - const relations: Array> = [post.drafts, post.postsInThread, post.files, post.reactions]; + const relations: Array> = [post.drafts, post.files, post.reactions]; for await (const models of relations) { try { models.forEach((m) => { @@ -29,6 +29,20 @@ export const prepareDeletePost = async (post: PostModel): Promise => { } } + // If the post is a root post, delete the postsInThread model + if (!post.rootId) { + try { + const postsInThread = await post.postsInThread.fetch(); + if (postsInThread) { + postsInThread.forEach((m) => { + preparedModels.push(m.prepareDestroyPermanently()); + }); + } + } catch { + // Record not found, do nothing + } + } + // If thread exists, delete thread, participants and threadsInTeam try { const thread = await post.thread.fetch();