From 8fe4ebde9bc4df01a0dd3ab39b94a74bafe4a015 Mon Sep 17 00:00:00 2001 From: Muhsin Keloth Date: Thu, 10 Aug 2023 13:17:07 +0530 Subject: [PATCH] fix: create thread if conversation identifier changes --- .../slack/send_on_slack_service.rb | 3 +- .../slack/send_on_slack_service_spec.rb | 37 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/integrations/slack/send_on_slack_service.rb b/lib/integrations/slack/send_on_slack_service.rb index 1f9a5fbb8..98df6569b 100644 --- a/lib/integrations/slack/send_on_slack_service.rb +++ b/lib/integrations/slack/send_on_slack_service.rb @@ -128,7 +128,8 @@ class Integrations::Slack::SendOnSlackService < Base::SendOnChannelService end def update_reference_id - return if conversation.identifier + # If the conversation identifier is present and conversation identifier is not equal to slack message ts, then do nothing + return if conversation.identifier && conversation.identifier == @slack_message['ts'] conversation.update!(identifier: @slack_message['ts']) 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..40dcf86ce 100644 --- a/spec/lib/integrations/slack/send_on_slack_service_spec.rb +++ b/spec/lib/integrations/slack/send_on_slack_service_spec.rb @@ -97,6 +97,43 @@ describe Integrations::Slack::SendOnSlackService do expect(message.external_source_id_slack).to eq 'cw-origin-6789.12345' end + it 'send a message to a previously non-existent slack thread' do + allow(slack_message).to receive(:[]).with('ts').and_return('1245.6789') + + 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 '1245.6789' + end + + it 'send a message to a differnt channel in slack' do + 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,