A platform for community discussion. Free, open, simple.
FIX: Explain why a chat approval message was refused
Régis Hanol74f6acbcf432
diff --git a/plugins/chat/config/locales/server.en.yml b/plugins/chat/config/locales/server.en.ymlindex f511a990945..724a04dcee3 100644--- a/plugins/chat/config/locales/server.en.yml+++ b/plugins/chat/config/locales/server.en.yml@@ -264,7 +264,8 @@ en:chat_approval:channel_not_found: "Chat message failed: channel %{channel_id} not found."invalid_params: "Chat message failed: %{errors}"- failed: "Chat message failed: %{steps}"+ channel_blocked: "Chat message failed: can't post in channel %{channel_id}: %{reason}"+ failed_step: "Chat message failed (failed step: %{step})."timeout_must_be_positive: "Timeout minutes must be greater than 0."invalid_timeout_action: "Invalid timeout action: %{action}."buttons_required: "At least one chat approval button is required."diff --git a/plugins/chat/lib/discourse_workflows/nodes/chat_approval/v1.rb b/plugins/chat/lib/discourse_workflows/nodes/chat_approval/v1.rbindex 5e384dcbfe3..f83373aa5ac 100644--- a/plugins/chat/lib/discourse_workflows/nodes/chat_approval/v1.rb+++ b/plugins/chat/lib/discourse_workflows/nodes/chat_approval/v1.rb@@ -182,11 +182,20 @@ if defined?(DiscourseWorkflows)),)end+ on_failed_policy(:channel_allows_message_creation) do |policy|+ raise_node_error!(+ I18n.t(+ "discourse_workflows.errors.chat_approval.channel_blocked",+ channel_id: channel_id,+ reason: policy.reason,+ ),+ )+ endon_failure doraise_node_error!(I18n.t(- "discourse_workflows.errors.chat_approval.failed",- steps: result.inspect_steps,+ "discourse_workflows.errors.chat_approval.failed_step",+ step: ChatServiceFailure.failed_step_name(result),),)enddiff --git a/plugins/chat/lib/discourse_workflows/nodes/chat_service_failure.rb b/plugins/chat/lib/discourse_workflows/nodes/chat_service_failure.rbnew file mode 100644index 00000000000..d98abd36962--- /dev/null+++ b/plugins/chat/lib/discourse_workflows/nodes/chat_service_failure.rb@@ -0,0 +1,20 @@+# frozen_string_literal: true++if defined?(DiscourseWorkflows)+ module DiscourseWorkflows+ module Nodes+ module ChatServiceFailure+ # `on_failure` gets no step context, and `inspect_steps` is developer output,+ # so name the failed step for the execution log instead.+ def self.failed_step_name(result)+ Service::StepsInspector+ .new(result)+ .steps+ .detect(&:failure?)+ &.result_key+ &.delete_prefix("result.") || "unknown"+ end+ end+ end+ end+enddiff --git a/plugins/chat/spec/lib/discourse_workflows/nodes/chat_approval/v1_spec.rb b/plugins/chat/spec/lib/discourse_workflows/nodes/chat_approval/v1_spec.rbindex e3988795448..4d480b456a9 100644--- a/plugins/chat/spec/lib/discourse_workflows/nodes/chat_approval/v1_spec.rb+++ b/plugins/chat/spec/lib/discourse_workflows/nodes/chat_approval/v1_spec.rb@@ -201,5 +201,24 @@ RSpec.describe DiscourseWorkflows::Nodes::ChatApproval::V1 do)expect(dm_channel.chat_messages).to be_emptyend++ it "explains why the channel refused the approval message" do+ channel.update!(status: :read_only)+ config = { "message" => "Approve?", "channel_id" => channel.id.to_s }+ instance = described_class.new(parameters: config)+ # Only open channels are selectable, so simulate a status change after selection.+ allow(instance).to receive(:selectable_chat_channel).and_return(channel)++ expect { instance.execute(build_exec_ctx(config)) }.to raise_error(+ DiscourseWorkflows::NodeError,+ node_error_message(+ :channel_blocked,+ scope: :chat_approval,+ channel_id: channel.id,+ reason: I18n.t("chat.errors.channel_new_message_disallowed.read_only"),+ ),+ )+ expect(channel.chat_messages).to be_empty+ endendenddiff --git a/plugins/chat/spec/lib/discourse_workflows/nodes/chat_service_failure_spec.rb b/plugins/chat/spec/lib/discourse_workflows/nodes/chat_service_failure_spec.rbnew file mode 100644index 00000000000..1532c951beb--- /dev/null+++ b/plugins/chat/spec/lib/discourse_workflows/nodes/chat_service_failure_spec.rb@@ -0,0 +1,25 @@+# frozen_string_literal: true++RSpec.describe DiscourseWorkflows::Nodes::ChatServiceFailure do+ describe ".failed_step_name" do+ fab!(:channel, :chat_channel)+ fab!(:user) { Fabricate(:user, refresh_auto_groups: true) }++ before { SiteSetting.chat_enabled = true }++ it "names the step that made the service fail" do+ UserSilencer.silence(user, Discourse.system_user)++ result =+ Chat::CreateMessage.call(+ guardian: user.guardian,+ params: {+ chat_channel_id: channel.id,+ message: "Hello",+ },+ )++ expect(described_class.failed_step_name(result)).to eq("policy.no_silenced_user")+ end+ end+end