A platform for community discussion. Free, open, simple.
FIX: Missing/incorrect flag reason in `account_deleted` emails (#34955)
Selase Krakani467fcb460aa2
diff --git a/app/mailers/user_notifications.rb b/app/mailers/user_notifications.rbindex 52f862f4f97..cad8b658d4e 100644--- a/app/mailers/user_notifications.rb+++ b/app/mailers/user_notifications.rb@@ -179,14 +179,20 @@ class UserNotifications < ActionMailer::Baseenddef account_deleted(email, reviewable)- post_action_type_id =- reviewable.reviewable_scores.first&.reviewable_score_type ||- PostActionTypeView.new.types[:spam]- build_email(- email,- template: "user_notifications.account_deleted",- flag_reason: I18n.t("flag_reasons.#{PostActionTypeView.new.types[post_action_type_id]}"),- )+ post_action_type_view = PostActionTypeView.new+ post_action_type_id = reviewable.reviewable_scores.first&.reviewable_score_type++ flag_reason =+ if post_action_type_id+ reason_key = post_action_type_view.flag_and_score_types[post_action_type_id]++ reason = I18n.t("flag_reasons.#{reason_key}", default: nil).presence if reason_key+ reason || post_action_type_view.descriptions[post_action_type_id].presence+ end++ flag_reason ||= I18n.t("flag_reasons.spam")++ build_email(email, template: "user_notifications.account_deleted", flag_reason: flag_reason)enddef account_suspended(user, opts = nil)diff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex 68b4c68c11b..37afcf179f5 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -3379,11 +3379,13 @@ en:illegal: "Your post was flagged as **illegal**: the community thinks it might be breaking the law."spam: "Your post was flagged as **spam**: the community feels it is an advertisement, something that is overly promotional in nature instead of being useful or relevant to the topic as expected."notify_moderators: "Your post was flagged **for moderator attention**: the community feels something about the post requires manual intervention by a staff member."+ needs_approval: "Your post was flagged **for approval**: it has been automatically queued for review by a staff member to ensure it meets [our community guidelines](%{base_path}/guidelines)."responder:off_topic: "The post was flagged as **off-topic**: the community feels it is not a good fit for the topic, as currently defined by the title and the first post."inappropriate: "The post was flagged as **inappropriate**: the community feels it is offensive, abusive, to be hateful conduct or a violation of [our community guidelines](%{base_path}/guidelines)."spam: "The post was flagged as **spam**: the community feels it is an advertisement, something that is overly promotional in nature instead of being useful or relevant to the topic as expected."notify_moderators: "The post was flagged **for moderator attention**: the community feels something about the post requires manual intervention by a staff member."+ needs_approval: "The post was flagged **for approval**: it has been automatically queued for review by a staff member to ensure it meets [our community guidelines](%{base_path}/guidelines)."flags_dispositions:agreed: "Thanks for letting us know. We agree there is an issue and we're looking into it."diff --git a/lib/post_action_type_view.rb b/lib/post_action_type_view.rbindex 2317b89e6b1..e7cd0cfd76d 100644--- a/lib/post_action_type_view.rb+++ b/lib/post_action_type_view.rb@@ -79,6 +79,10 @@ class PostActionTypeViewflag_enum(all_flags.filter { |flag| flag[:score_type] })end+ def flag_and_score_types+ flag_types.merge(score_types)+ end+# flags resulting in mod notificationsdef notify_flag_type_idsnotify_flag_types.valuesdiff --git a/spec/lib/post_action_type_view_spec.rb b/spec/lib/post_action_type_view_spec.rbindex 5d5a85fc04c..132690da788 100644--- a/spec/lib/post_action_type_view_spec.rb+++ b/spec/lib/post_action_type_view_spec.rb@@ -29,6 +29,18 @@ RSpec.describe PostActionTypeView do)expect(post_action_type_view.score_types).to eq({ needs_approval: 9 })+ expect(post_action_type_view.flag_and_score_types).to eq(+ {+ illegal: 10,+ inappropriate: 4,+ notify_moderators: 7,+ notify_user: 6,+ off_topic: 3,+ spam: 8,+ needs_approval: 9,+ },+ )+flag = Fabricate(:flag, name: "flag", enabled: false)expect(PostActionTypeView.new.disabled_flag_types).to eq({ custom_flag: flag.id })flag.destroy!diff --git a/spec/mailers/user_notifications_spec.rb b/spec/mailers/user_notifications_spec.rbindex 6e380ea4d81..ffd23295155 100644--- a/spec/mailers/user_notifications_spec.rb+++ b/spec/mailers/user_notifications_spec.rb@@ -1577,6 +1577,63 @@ RSpec.describe UserNotifications doendend+ describe ".account_deleted" do+ fab!(:reviewable) { Fabricate(:reviewable_flagged_post, reviewable_scores: []) }++ it "includes flag reason for standard flags" do+ Fabricate(+ :reviewable_score,+ reviewable: reviewable,+ reviewable_score_type: PostActionType.types[:spam],+ )++ reviewable.reload+ mail = UserNotifications.account_deleted("[email protected]", reviewable)++ expect(mail.body).to include(I18n.t("flag_reasons.spam"))+ expect(mail.body).to_not include(I18n.t("flag_reasons.illegal"))+ end++ it "includes flag reason for automated system flags" do+ Fabricate(+ :reviewable_score,+ reviewable: reviewable,+ reviewable_score_type: ReviewableScore.types[:needs_approval],+ )+ reviewable.reload++ mail = UserNotifications.account_deleted("[email protected]", reviewable)++ expect(mail.body).to include(I18n.t("flag_reasons.needs_approval"))+ expect(mail.body).to_not include(I18n.t("flag_reasons.spam"))+ end++ it "falls back to the default spam reason if no score is present" do+ mail = UserNotifications.account_deleted("[email protected]", reviewable)++ expect(mail.body).to include(I18n.t("flag_reasons.spam"))+ expect(mail.body).to_not include(I18n.t("flag_reasons.illegal"))+ end++ it "falls back to the flag's description for custom flags" do+ custom_flag =+ Fabricate(+ :flag,+ name_key: "custom_delete_flag",+ description: "This is a custom deletion reason.",+ applies_to: %w[Post],+ )++ Fabricate(:reviewable_score, reviewable: reviewable, reviewable_score_type: custom_flag.id)+ reviewable.reload++ mail = UserNotifications.account_deleted("[email protected]", reviewable)++ expect(mail.body).to include(custom_flag.description)+ expect(mail.body).to_not include(I18n.t("flag_reasons.spam"))+ end+ end+describe ".account_suspended" dofab!(:user_history) { Fabricate(:user_history, action: UserHistory.actions[:suspend_user]) }