Skip to content

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

FIX: Return 422 when a chat message flag is rejected (#44125)

Gabriel Grubba927c9c9202e5
diff --git a/plugins/chat/app/controllers/chat/api/channels_messages_flags_controller.rb b/plugins/chat/app/controllers/chat/api/channels_messages_flags_controller.rb
index 8c179bdf563..72b1797265f 100644
--- a/plugins/chat/app/controllers/chat/api/channels_messages_flags_controller.rb
+++ b/plugins/chat/app/controllers/chat/api/channels_messages_flags_controller.rb
@@ -6,6 +6,7 @@ class Chat::Api::ChannelsMessagesFlagsController < Chat::ApiController
Chat::FlagMessage.call(service_params) do
on_success { render(json: success_json) }
+ on_failed_step(:flag_message) { |step| render_json_error(step.error) }
on_failure { render(json: failed_json, status: :unprocessable_entity) }
on_model_not_found(:message) { raise Discourse::NotFound }
on_failed_policy(:can_flag_message_in_channel) { raise Discourse::NotFound }
diff --git a/plugins/chat/app/services/chat/flag_message.rb b/plugins/chat/app/services/chat/flag_message.rb
index 7817ca62f64..6c6aa84bb0b 100644
--- a/plugins/chat/app/services/chat/flag_message.rb
+++ b/plugins/chat/app/services/chat/flag_message.rb
@@ -65,12 +65,14 @@ module Chat
end
def flag_message(message:, params:, guardian:)
- Chat::ReviewQueue.new.flag_message(
- message,
- guardian,
- params.flag_type_id,
- **params.slice(:message, :is_warning, :take_action, :queue_for_review),
- )
+ result =
+ Chat::ReviewQueue.new.flag_message(
+ message,
+ guardian,
+ params.flag_type_id,
+ **params.slice(:message, :is_warning, :take_action, :queue_for_review),
+ )
+ fail!(result[:errors]) if !result[:success]
end
end
end
diff --git a/plugins/chat/spec/requests/chat/api/channels_messages_flags_controller_spec.rb b/plugins/chat/spec/requests/chat/api/channels_messages_flags_controller_spec.rb
index 3974de9adf4..6c4a43e4a76 100644
--- a/plugins/chat/spec/requests/chat/api/channels_messages_flags_controller_spec.rb
+++ b/plugins/chat/spec/requests/chat/api/channels_messages_flags_controller_spec.rb
@@ -43,6 +43,29 @@ RSpec.describe Chat::Api::ChannelsMessagesFlagsController do
end
end
+ context "when the message was already flagged by the user" do
+ before do
+ Chat::FlagMessage.call(
+ guardian: current_user.guardian,
+ params: {
+ channel_id: message_1.chat_channel_id,
+ message_id: message_1.id,
+ **params,
+ },
+ )
+ end
+
+ it "returns a 422 with the error" do
+ post "/chat/api/channels/#{message_1.chat_channel.id}/messages/#{message_1.id}/flags",
+ params: params
+
+ expect(response.status).to eq(422)
+ expect(response.parsed_body["errors"]).to eq(
+ [I18n.t("chat.reviewables.message_already_handled")],
+ )
+ end
+ end
+
context "when user can't flag message" do
before { UserSilencer.new(current_user).silence }
diff --git a/plugins/chat/spec/services/chat/flag_message_spec.rb b/plugins/chat/spec/services/chat/flag_message_spec.rb
index 7ed31475d94..02d1c763221 100644
--- a/plugins/chat/spec/services/chat/flag_message_spec.rb
+++ b/plugins/chat/spec/services/chat/flag_message_spec.rb
@@ -58,6 +58,30 @@ RSpec.describe Chat::FlagMessage do
end
end
+ context "when the message was already flagged by the user" do
+ fab!(:current_user, :admin)
+
+ before { described_class.call(params:, **dependencies) }
+
+ it { is_expected.to fail_a_step(:flag_message) }
+ end
+
+ context "when the companion PM can't be created" do
+ fab!(:current_user) { Fabricate(:user, refresh_auto_groups: true) }
+
+ let(:flag_type_id) { ReviewableScore.types[:notify_user] }
+ let(:message) { "Please review your message" }
+
+ before { message_1.user.user_option.update!(allow_private_messages: false) }
+
+ it "fails with the PM error" do
+ expect(result).to fail_a_step(:flag_message)
+ expect(result["result.step.flag_message"].error).to include(
+ I18n.t(:not_accepting_pms, username: message_1.user.username),
+ )
+ end
+ end
+
context "when contract is invalid" do
let(:channel_id) { nil }