diff --git a/lib/integrations/slack/send_on_slack_service.rb b/lib/integrations/slack/send_on_slack_service.rb index 1f9a5fbb8..3ac595ee5 100644 --- a/lib/integrations/slack/send_on_slack_service.rb +++ b/lib/integrations/slack/send_on_slack_service.rb @@ -128,7 +128,7 @@ class Integrations::Slack::SendOnSlackService < Base::SendOnChannelService end def update_reference_id - return if conversation.identifier + return unless should_update_reference_id? conversation.update!(identifier: @slack_message['ts']) end @@ -146,4 +146,12 @@ class Integrations::Slack::SendOnSlackService < Base::SendOnChannelService def link_to_conversation "<#{ENV.fetch('FRONTEND_URL', nil)}/app/accounts/#{conversation.account_id}/conversations/#{conversation.display_id}|Click here>" end + + # Determines whether the conversation identifier should be updated with the ts value. + # The identifier should be updated in the following cases: + # - If the conversation identifier is blank, it means a new conversation is being created. + # - If the thread_ts is blank, it means that the conversation was previously connected in a different channel. + def should_update_reference_id? + conversation.identifier.blank? || @slack_message['message']['thread_ts'].blank? + end end diff --git a/spec/lib/integrations/slack/send_on_slack_service_spec.rb b/spec/lib/integrations/slack/send_on_slack_service_spec.rb index 7fc75b2db..bf6ff142a 100644 --- a/spec/lib/integrations/slack/send_on_slack_service_spec.rb +++ b/spec/lib/integrations/slack/send_on_slack_service_spec.rb @@ -27,6 +27,7 @@ describe Integrations::Slack::SendOnSlackService do allow(slack_message).to receive(:[]).with('ts').and_return('12345.6789') allow(slack_message).to receive(:[]).with('message').and_return(slack_message_content) allow(slack_message_content).to receive(:[]).with('ts').and_return('6789.12345') + allow(slack_message_content).to receive(:[]).with('thread_ts').and_return('12345.6789') end describe '#perform' do @@ -97,6 +98,43 @@ describe Integrations::Slack::SendOnSlackService do expect(message.external_source_id_slack).to eq 'cw-origin-6789.12345' end + it 'sent message will send to the the previous thread if the slack disconnects and connects to a same channel.' do + allow(slack_message).to receive(:[]).with('message').and_return({ 'thread_ts' => conversation.identifier }) + expect(slack_client).to receive(:chat_postMessage).with( + channel: hook.reference_id, + text: message.content, + username: "#{message.sender.name} (Contact)", + thread_ts: conversation.identifier, + icon_url: anything, + unfurl_links: true + ).and_return(slack_message) + + builder.perform + + expect(conversation.identifier).to eq 'random_slack_thread_ts' + end + + it 'sent message will create a new thread if the slack disconnects and connects to a different channel' do + allow(slack_message).to receive(:[]).with('message').and_return({ 'thread_ts' => nil }) + allow(slack_message).to receive(:[]).with('ts').and_return('1691652432.896169') + + hook.update!(reference_id: 'C12345') + + expect(slack_client).to receive(:chat_postMessage).with( + channel: 'C12345', + text: message.content, + username: "#{message.sender.name} (Contact)", + thread_ts: conversation.identifier, + icon_url: anything, + unfurl_links: true + ).and_return(slack_message) + + builder.perform + + expect(hook.reload.reference_id).to eq 'C12345' + expect(conversation.identifier).to eq '1691652432.896169' + end + it 'sent attachment on slack' do expect(slack_client).to receive(:chat_postMessage).with( channel: hook.reference_id,