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();