From 99e9ef9c7b96398293a5a5a4736238e68e669d5d Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Fri, 24 Feb 2023 16:54:27 -0800 Subject: [PATCH] Fix bug when deleting user accounts if they own forum threads --- pkg/models/deletion/delete_user.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/pkg/models/deletion/delete_user.go b/pkg/models/deletion/delete_user.go index fc6a659..702ded9 100644 --- a/pkg/models/deletion/delete_user.go +++ b/pkg/models/deletion/delete_user.go @@ -223,6 +223,17 @@ func DeleteForumThreads(userID uint64) error { // Wipe all these threads and their comments. if len(threadIDs) > 0 { + // First, delete the threads - so they won't be referring to comment_ids that we + // delete next and causing errors to arise. + result = models.DB.Where( + "id IN ?", + threadIDs, + ).Delete(&models.Thread{}) + if result.Error != nil { + return fmt.Errorf("Couldn't delete your forum threads: %s", result.Error) + } + + // Remove the comments. result = models.DB.Where( "table_name = ? AND table_id IN ?", "threads", threadIDs, @@ -231,11 +242,6 @@ func DeleteForumThreads(userID uint64) error { return fmt.Errorf("Couldn't wipe threads of comments: %s", result.Error) } - // And finish the threads off too. - result = models.DB.Where( - "id IN ?", - threadIDs, - ).Delete(&models.Thread{}) return result.Error }