From 0eeab8c56c1af6cd5ff8e5b9e048a49e59fb2fc1 Mon Sep 17 00:00:00 2001 From: Tejaswini Chile Date: Mon, 9 May 2022 19:18:30 +0530 Subject: [PATCH] Fix: slack repeated callback event message for attached files (#4610) --- lib/integrations/slack/incoming_message_builder.rb | 12 ++++++++++-- .../slack/incoming_message_builder_spec.rb | 12 ++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/integrations/slack/incoming_message_builder.rb b/lib/integrations/slack/incoming_message_builder.rb index b7c235b35..ec4f8e2b5 100644 --- a/lib/integrations/slack/incoming_message_builder.rb +++ b/lib/integrations/slack/incoming_message_builder.rb @@ -35,9 +35,9 @@ class Integrations::Slack::IncomingMessageBuilder def supported_message? if message.present? - SUPPORTED_MESSAGE_TYPES.include?(message[:type]) + SUPPORTED_MESSAGE_TYPES.include?(message[:type]) && !attached_file_message? else - params.dig(:event, :files).any? + params[:event][:files].any? && !attached_file_message? end end @@ -135,4 +135,12 @@ class Integrations::Slack::IncomingMessageBuilder :file end end + + # Ignoring the changes added here https://github.com/chatwoot/chatwoot/blob/5b5a6d89c0cf7f3148a1439d6fcd847784a79b94/lib/integrations/slack/send_on_slack_service.rb#L69 + # This make sure 'Attached File!' comment is not visible on CW dashboard. + # This is showing because of https://github.com/chatwoot/chatwoot/pull/4494/commits/07a1c0da1e522d76e37b5f0cecdb4613389ab9b6 change. + # As now we consider the postback message with event[:files] + def attached_file_message? + params[:event][:text] == 'Attached File!' + end end diff --git a/spec/lib/integrations/slack/incoming_message_builder_spec.rb b/spec/lib/integrations/slack/incoming_message_builder_spec.rb index 43bf2f35d..1695331c3 100644 --- a/spec/lib/integrations/slack/incoming_message_builder_spec.rb +++ b/spec/lib/integrations/slack/incoming_message_builder_spec.rb @@ -71,6 +71,18 @@ describe Integrations::Slack::IncomingMessageBuilder do expect(conversation.messages.last.content).to eql('this is test https://chatwoot.com Hey @Sojan Test again') expect(conversation.messages.last.attachments).to be_any end + + it 'ignore message if it is postback of CW attachment message' do + expect(hook).not_to eq nil + messages_count = conversation.messages.count + message_with_attachments[:event][:text] = 'Attached File!' + builder = described_class.new(message_with_attachments) + + allow(builder).to receive(:sender).and_return(nil) + builder.perform + + expect(conversation.messages.count).to eql(messages_count) + end end end end