fix: Create new slack thread if the thread identifier changes (#7702)

Co-authored-by: Shivam Mishra <scm.mymail@gmail.com>
This commit is contained in:
Muhsin Keloth
2023-08-11 14:19:49 +05:30
committed by GitHub
parent 84240e197a
commit cfe86d9c06
2 changed files with 47 additions and 1 deletions

View File

@@ -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

View File

@@ -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,