Skip to content

A platform for community discussion. Free, open, simple.

UX: confirm user deletion step in review queue (#42605)

Krisf3d6411d449b
diff --git a/app/models/concerns/reviewable_action_builder.rb b/app/models/concerns/reviewable_action_builder.rb
index 0e324805955..2eabd055207 100644
--- a/app/models/concerns/reviewable_action_builder.rb
+++ b/app/models/concerns/reviewable_action_builder.rb
@@ -121,18 +121,6 @@ module ReviewableActionBuilder
private
- # Returns the user associated with the reviewable, if applicable.
- # For most reviewables, this will be the user who created the reviewable target.
- #
- # @return [User] The user associated with the reviewable.
- def target_user
- if target_type == "User"
- try(:target)
- else
- try(:target_created_by)
- end
- end
-
# Returns the post associated with the reviewable, if applicable.
# This method assumes that the including class has a `target` that is a Post or
# a `target_id` that can be used to look up the Post.
diff --git a/app/models/reviewable.rb b/app/models/reviewable.rb
index c9c8bb2eb70..911c04bb4ba 100644
--- a/app/models/reviewable.rb
+++ b/app/models/reviewable.rb
@@ -802,6 +802,13 @@ class Reviewable < ActiveRecord::Base
score
end
+ # The account that the delete user actions destroy. Subclasses that delete a
+ # different account must override this, otherwise the confirmation prompt will
+ # name the wrong user.
+ def target_user
+ target_type == "User" ? target : target_created_by
+ end
+
def delete_user_actions(actions, bundle = nil, require_reject_reason: false)
bundle ||=
actions.add_bundle(
@@ -810,11 +817,21 @@ class Reviewable < ActiveRecord::Base
label: "reviewables.actions.reject_user.title",
)
+ # The reject reason modal already acts as a confirmation step, so only ask
+ # for a separate confirmation when it isn't shown.
+ username = target_user&.username
+ confirmable = !require_reject_reason && username.present?
+
actions.add(:delete_user, bundle: bundle) do |a|
a.icon = "user-xmark"
a.label = "reviewables.actions.reject_user.delete.title"
a.description = "reviewables.actions.reject_user.delete.description"
a.require_reject_reason = require_reject_reason
+ if confirmable
+ a.confirm_message = "reviewables.actions.reject_user.delete.confirm"
+ a.confirm_message_args = { username: username }
+ a.confirm_destructive = true
+ end
end
actions.add(:delete_user_block, bundle: bundle) do |a|
@@ -822,6 +839,11 @@ class Reviewable < ActiveRecord::Base
a.label = "reviewables.actions.reject_user.block.title"
a.require_reject_reason = require_reject_reason
a.description = "reviewables.actions.reject_user.block.description"
+ if confirmable
+ a.confirm_message = "reviewables.actions.reject_user.block.confirm"
+ a.confirm_message_args = { username: username }
+ a.confirm_destructive = true
+ end
end
end
diff --git a/app/serializers/reviewable_action_serializer.rb b/app/serializers/reviewable_action_serializer.rb
index 62d53db66b6..3858b8dad79 100644
--- a/app/serializers/reviewable_action_serializer.rb
+++ b/app/serializers/reviewable_action_serializer.rb
@@ -6,6 +6,7 @@ class ReviewableActionSerializer < ApplicationSerializer
:button_class,
:label,
:confirm_message,
+ :confirm_destructive,
:description,
:server_action,
:client_action,
@@ -17,7 +18,7 @@ class ReviewableActionSerializer < ApplicationSerializer
end
def confirm_message
- I18n.t(object.confirm_message)
+ I18n.t(object.confirm_message, **(object.confirm_message_args || {}))
end
def description
@@ -40,6 +41,10 @@ class ReviewableActionSerializer < ApplicationSerializer
object.confirm_message.present?
end
+ def include_confirm_destructive?
+ object.confirm_destructive.present?
+ end
+
def include_client_action?
object.client_action.present?
end
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 7289524c881..80958d64dfc 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -6295,10 +6295,12 @@ en:
delete:
title: "Delete user"
description: "Delete the user without blocking their email or IP address"
+ confirm: "Are you sure you want to delete @%{username}?"
complete: "User deleted."
block:
title: "Delete and block user"
description: "Delete the user and block their email and IP address"
+ confirm: "Are you sure you want to delete and block @%{username}?"
complete: "User deleted and blocked."
confirm_spam:
title: "Yes"
diff --git a/frontend/discourse/app/components/reviewable/item.gjs b/frontend/discourse/app/components/reviewable/item.gjs
index 6893c4cd690..8a0c4e33a45 100644
--- a/frontend/discourse/app/components/reviewable/item.gjs
+++ b/frontend/discourse/app/components/reviewable/item.gjs
@@ -647,10 +647,18 @@ export default class ReviewableItem extends Component {
if (message) {
if (await this.#claimReviewable()) {
- this.dialog.confirm({
+ const confirmOptions = {
message,
didConfirm: () => this._performConfirmed(performableAction),
- });
+ // Claiming happens before the prompt, so release it if nothing is performed.
+ didCancel: () => this.#unclaimAutomaticReviewable(),
+ };
+
+ if (performableAction.get("confirm_destructive")) {
+ this.dialog.deleteConfirm(confirmOptions);
+ } else {
+ this.dialog.confirm(confirmOptions);
+ }
}
} else if (actionModalClass) {
if (await this.#claimReviewable()) {
diff --git a/lib/reviewable/actions.rb b/lib/reviewable/actions.rb
index 812e8a0107e..e33a2026ed6 100644
--- a/lib/reviewable/actions.rb
+++ b/lib/reviewable/actions.rb
@@ -46,6 +46,8 @@ class Reviewable < ActiveRecord::Base
:label,
:description,
:confirm_message,
+ :confirm_message_args,
+ :confirm_destructive,
:client_action,
:require_reject_reason,
:custom_modal,
diff --git a/spec/models/reviewable_user_spec.rb b/spec/models/reviewable_user_spec.rb
index e1b9a6ac155..8b8f32585f2 100644
--- a/spec/models/reviewable_user_spec.rb
+++ b/spec/models/reviewable_user_spec.rb
@@ -138,12 +138,33 @@ RSpec.describe ReviewableUser, type: :model do
assert_require_reject_reason(:delete_user_block, true)
end
- def assert_require_reject_reason(id, expected)
- actions = reviewable.actions_for(Guardian.new(moderator))
+ it "doesn't add a confirmation when a rejection reason is already required" do
+ action = find_action(:delete_user)
+
+ expect(action.confirm_message).to be_nil
+ expect(action.confirm_destructive).to be_nil
+ end
+
+ it "asks for confirmation when no rejection reason is required" do
+ reviewable.reviewable_scores.build(user: admin, reason: "suspect_user")
+
+ %i[delete_user delete_user_block].each do |id|
+ action = find_action(id)
- expect(actions.to_a.find { |a| a.server_action.to_sym == id }.require_reject_reason).to eq(
- expected,
- )
+ expect(action.confirm_destructive).to eq(true)
+ expect(action.confirm_message_args).to eq(username: reviewable.target.username)
+ expect(I18n.t(action.confirm_message, **action.confirm_message_args)).to include(
+ "@#{reviewable.target.username}",
+ )
+ end
+ end
+
+ def find_action(id)
+ reviewable.actions_for(Guardian.new(moderator)).to_a.find { |a| a.server_action.to_sym == id }
+ end
+
+ def assert_require_reject_reason(id, expected)
+ expect(find_action(id).require_reject_reason).to eq(expected)
end
end
diff --git a/spec/system/page_objects/pages/review.rb b/spec/system/page_objects/pages/review.rb
index 49e9d6fcc06..85af39ffde0 100644
--- a/spec/system/page_objects/pages/review.rb
+++ b/spec/system/page_objects/pages/review.rb
@@ -32,8 +32,11 @@ module PageObjects
end
end
- def delete_user_from_reviewable(reviewable, action)
+ # Pass confirm: false for reviewables that require a rejection reason, as
+ # those open a modal instead of the confirmation dialog.
+ def delete_user_from_reviewable(reviewable, action, confirm: true)
select_bundled_action(reviewable, action, bundle_index: 1)
+ PageObjects::Components::Dialog.new.click_danger if confirm
end
def click_post_body_toggle
diff --git a/spec/system/reviewables_spec.rb b/spec/system/reviewables_spec.rb
index 600f4eed928..c0ff2e5da11 100644
--- a/spec/system/reviewables_spec.rb
+++ b/spec/system/reviewables_spec.rb
@@ -10,6 +10,7 @@ describe "Reviewables" do
let(:moderator) { Fabricate(:moderator) }
let(:toasts) { PageObjects::Components::Toasts.new }
let(:suspend_user_modal) { PageObjects::Modals::PenalizeUser.new("suspend") }
+ let(:dialog) { PageObjects::Components::Dialog.new }
before { sign_in(admin) }
@@ -186,6 +187,12 @@ describe "Reviewables" do
review_page.select_bundled_action(queued_post_reviewable, "delete_user")
+ expect(dialog).to have_content(
+ "Are you sure you want to delete @#{queued_post_reviewable.target_created_by.username}?",
+ )
+ expect(page).to have_css(".dialog-footer .btn-danger", text: I18n.t("js.delete"))
+ dialog.click_danger
+
expect(review_page).to have_no_error_dialog_visible
expect(review_page).to have_reviewable_with_rejected_status(queued_post_reviewable)
expect(review_page).to have_no_reviewable_action_dropdown
@@ -193,6 +200,24 @@ describe "Reviewables" do
expect(queued_post_reviewable.target_created_by).to be_nil
end
+ it "delete_user can be cancelled from the confirmation dialog" do
+ review_page.visit_reviewable(queued_post_reviewable)
+ review_page.select_bundled_action(queued_post_reviewable, "delete_user")
+
+ dialog.click_no
+
+ expect(dialog).to be_closed
+ expect(review_page).to have_reviewable_with_pending_status(queued_post_reviewable)
+ expect(queued_post_reviewable.reload).to be_pending
+ expect(queued_post_reviewable.target_created_by).to be_present
+
+ try_until_success do
+ expect(
+ ReviewableClaimedTopic.where(topic_id: queued_post_reviewable.topic_id),
+ ).to be_empty
+ end
+ end
+
it "reject_and_suspend rejects the post and suspends its author" do
review_page.visit_reviewable(queued_post_reviewable)
review_page.select_bundled_action(queued_post_reviewable, "reject_and_suspend")