From 28b4ff6cb694d67af177287a8a249fd072701058 Mon Sep 17 00:00:00 2001 From: Krzysztof Kotlarek Date: Wed, 4 Dec 2024 14:46:52 +1100 Subject: [PATCH] FIX: update flag reason message with default value (#30026) Currently only system flags are translated. When we send message to the user that their post was deleted because of custom flag, we should default to custom flag name. --- app/models/post.rb | 1 + lib/post_destroyer.rb | 1 + spec/lib/post_destroyer_spec.rb | 23 +++++++++++++++++++++++ spec/models/post_spec.rb | 13 +++++++++++++ 4 files changed, 38 insertions(+) diff --git a/app/models/post.rb b/app/models/post.rb index 066d618fea1..832e427e0f3 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -658,6 +658,7 @@ class Post < ActiveRecord::Base "flag_reasons.#{post_action_type_view.types[post_action_type_id]}", locale: SiteSetting.default_locale, base_path: Discourse.base_path, + default: PostActionType.names[post_action_type_id], ), } diff --git a/lib/post_destroyer.rb b/lib/post_destroyer.rb index b7b8a6be840..f4dc3b1188c 100644 --- a/lib/post_destroyer.rb +++ b/lib/post_destroyer.rb @@ -413,6 +413,7 @@ class PostDestroyer "flag_reasons#{".responder" if notify_responders}.#{flag_type}", locale: SiteSetting.default_locale, base_path: Discourse.base_path, + default: PostActionType.flags.find { |flag| flag[:name_key] == flag_type.to_s }[:name], ), }, ) diff --git a/spec/lib/post_destroyer_spec.rb b/spec/lib/post_destroyer_spec.rb index daedd7630ef..8cb10a50497 100644 --- a/spec/lib/post_destroyer_spec.rb +++ b/spec/lib/post_destroyer_spec.rb @@ -954,6 +954,29 @@ RSpec.describe PostDestroyer do expect(Jobs::SendSystemMessage.jobs.size).to eq(0) expect(ReviewableFlaggedPost.pending.count).to eq(0) end + + context "when custom flags" do + fab!(:custom_flag) { Fabricate(:flag, name: "custom flag", notify_type: true) } + let(:third_post) { Fabricate(:post, topic_id: post.topic_id) } + + it "should send message to user with correct translation" do + PostActionCreator.new( + moderator, + third_post, + custom_flag.id, + is_warning: false, + flag_topic: true, + ).perform + PostDestroyer.new(moderator, third_post, { reviewable: Reviewable.last }).destroy + jobs = Jobs::SendSystemMessage.jobs + expect(jobs.size).to eq(1) + + Jobs::SendSystemMessage.new.execute(jobs[0]["args"][0].with_indifferent_access) + + expect(Post.last.raw).to match("custom flag") + custom_flag.destroy! + end + end end describe "user actions" do diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index abcf5fbfc50..403bcd53b15 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -1498,6 +1498,19 @@ RSpec.describe Post do ).to(true) end + it "should inform the user when custom flag" do + custom_flag = Fabricate(:flag, name: "custom flag") + post.hide!(PostActionType.types[:custom_custom_flag]) + + jobs = Jobs::SendSystemMessage.jobs + expect(jobs.size).to eq(1) + + Jobs::SendSystemMessage.new.execute(jobs[0]["args"][0].with_indifferent_access) + expect(Post.last.raw).to match("custom flag") + + custom_flag.destroy! + end + it "should decrease user_stat topic_count for first post" do expect do post.hide!(PostActionType.types[:off_topic]) end.to change { post.user.user_stat.reload.topic_count