UX: improve user delete error message & return correct post count. (#13282)

UX: improve user delete error message & return correct post count. (#13282)

Post count was incorrect on admin page causing confusion when admins attempted to delete users.

diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb
index a880acf..15c70a1 100644
--- a/app/controllers/admin/users_controller.rb
+++ b/app/controllers/admin/users_controller.rb
@@ -420,7 +420,7 @@ class Admin::UsersController < Admin::AdminController
       rescue UserDestroyer::PostsExistError
         render json: {
           deleted: false,
-          message: "User #{user.username} has #{user.post_count} posts, so they can't be deleted."
+          message: I18n.t("user.cannot_delete_has_posts", username: user.username, post_count: user.posts.joins(:topic).count),
         }, status: 403
       end
     end
diff --git a/app/services/user_destroyer.rb b/app/services/user_destroyer.rb
index 5af6a4d..24980ce 100644
--- a/app/services/user_destroyer.rb
+++ b/app/services/user_destroyer.rb
@@ -15,7 +15,7 @@ class UserDestroyer
   # Returns a frozen instance of the User if the delete succeeded.
   def destroy(user, opts = {})
     raise Discourse::InvalidParameters.new('user is nil') unless user && user.is_a?(User)
-    raise PostsExistError if !opts[:delete_posts] && user.posts.count != 0
+    raise PostsExistError if !opts[:delete_posts] && user.posts.joins(:topic).count != 0
     @guardian.ensure_can_delete_user!(user)
 
     # default to using a transaction
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 07c405f..56f93b8 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -2594,6 +2594,7 @@ en:
     email_in_spam_header: "User's first email was flagged as spam"
     already_silenced: "User was already silenced by %{staff} %{time_ago}."
     already_suspended: "User was already suspended by %{staff} %{time_ago}."
+    cannot_delete_has_posts: "User %{username} has %{post_count} posts(s), either public posts or personal messages, so they can't be deleted."
 
   reviewables_reminder:
     submitted:
diff --git a/spec/requests/admin/users_controller_spec.rb b/spec/requests/admin/users_controller_spec.rb
index 1fec557..8aa4924 100644
--- a/spec/requests/admin/users_controller_spec.rb
+++ b/spec/requests/admin/users_controller_spec.rb
@@ -626,10 +626,16 @@ RSpec.describe Admin::UsersController do
       let!(:post) { Fabricate(:post, topic: topic, user: delete_me) }
 
       it "returns an api response that the user can't be deleted because it has posts" do
+        post_count = delete_me.posts.joins(:topic).count
+        delete_me_topic = Fabricate(:topic)
+        Fabricate(:post, topic: delete_me_topic, user: delete_me)
+        PostDestroyer.new(admin, delete_me_topic.first_post, context: "Deleted by admin").destroy
+
         delete "/admin/users/#{delete_me.id}.json"
         expect(response.status).to eq(403)
         json = response.parsed_body
         expect(json['deleted']).to eq(false)
+        expect(json['message']).to eq(I18n.t("user.cannot_delete_has_posts", username: delete_me.username, post_count: post_count))
       end
 
       it "doesn't return an error if delete_posts == true" do

GitHub sha: cd6ab7bdd78d1709f55d9fd0938d59c4660e1767

This commit appears in #13282 which was approved by tgxworld. It was merged by SamSaffron.