chore: Handle duplicate Webhooks from slack (#9187)
* chore: Handle duplicate Webhooks from slack * chore: fixes --------- Co-authored-by: Muhsin Keloth <muhsinkeramam@gmail.com>
This commit is contained in:
@@ -16,8 +16,8 @@ class Integrations::Slack::IncomingMessageBuilder
|
|||||||
|
|
||||||
if hook_verification?
|
if hook_verification?
|
||||||
verify_hook
|
verify_hook
|
||||||
elsif create_message?
|
elsif process_message_payload?
|
||||||
create_message
|
process_message_payload
|
||||||
elsif link_shared?
|
elsif link_shared?
|
||||||
SlackUnfurlJob.perform_later(params)
|
SlackUnfurlJob.perform_later(params)
|
||||||
end
|
end
|
||||||
@@ -67,7 +67,7 @@ class Integrations::Slack::IncomingMessageBuilder
|
|||||||
params[:event][:thread_ts].present?
|
params[:event][:thread_ts].present?
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_message?
|
def process_message_payload?
|
||||||
thread_timestamp_available? && supported_message? && integration_hook
|
thread_timestamp_available? && supported_message? && integration_hook
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1,21 +1,32 @@
|
|||||||
module Integrations::Slack::SlackMessageHelper
|
module Integrations::Slack::SlackMessageHelper
|
||||||
def create_message
|
def process_message_payload
|
||||||
return unless conversation
|
return unless conversation
|
||||||
|
|
||||||
build_message
|
handle_conversation
|
||||||
@message.save!
|
success_response
|
||||||
{ status: 'success' }
|
|
||||||
rescue Slack::Web::Api::Errors::MissingScope => e
|
rescue Slack::Web::Api::Errors::MissingScope => e
|
||||||
ChatwootExceptionTracker.new(e, account: conversation.account).capture_exception
|
ChatwootExceptionTracker.new(e, account: conversation.account).capture_exception
|
||||||
disable_and_reauthorize
|
disable_and_reauthorize
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def handle_conversation
|
||||||
|
create_message unless message_exists?
|
||||||
|
end
|
||||||
|
|
||||||
|
def success_response
|
||||||
|
{ status: 'success' }
|
||||||
|
end
|
||||||
|
|
||||||
def disable_and_reauthorize
|
def disable_and_reauthorize
|
||||||
integration_hook.prompt_reauthorization!
|
integration_hook.prompt_reauthorization!
|
||||||
integration_hook.disable
|
integration_hook.disable
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_message
|
def message_exists?
|
||||||
|
conversation.messages.exists?(external_source_ids: { slack: params[:event][:ts] })
|
||||||
|
end
|
||||||
|
|
||||||
|
def create_message
|
||||||
@message = conversation.messages.build(
|
@message = conversation.messages.build(
|
||||||
message_type: :outgoing,
|
message_type: :outgoing,
|
||||||
account_id: conversation.account_id,
|
account_id: conversation.account_id,
|
||||||
@@ -26,6 +37,7 @@ module Integrations::Slack::SlackMessageHelper
|
|||||||
sender: sender
|
sender: sender
|
||||||
)
|
)
|
||||||
process_attachments(params[:event][:files]) if attachments_present?
|
process_attachments(params[:event][:files]) if attachments_present?
|
||||||
|
@message.save!
|
||||||
end
|
end
|
||||||
|
|
||||||
def attachments_present?
|
def attachments_present?
|
||||||
|
|||||||
@@ -65,6 +65,16 @@ describe Integrations::Slack::IncomingMessageBuilder do
|
|||||||
expect(conversation.messages.count).to eql(messages_count)
|
expect(conversation.messages.count).to eql(messages_count)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'does not create message if message already exists' do
|
||||||
|
expect(hook).not_to be_nil
|
||||||
|
messages_count = conversation.messages.count
|
||||||
|
builder = described_class.new(message_params)
|
||||||
|
allow(builder).to receive(:sender).and_return(nil)
|
||||||
|
2.times.each { builder.perform }
|
||||||
|
expect(conversation.messages.count).to eql(messages_count + 1)
|
||||||
|
expect(conversation.messages.last.content).to eql('this is test https://chatwoot.com Hey @Sojan Test again')
|
||||||
|
end
|
||||||
|
|
||||||
it 'creates message' do
|
it 'creates message' do
|
||||||
expect(hook).not_to be_nil
|
expect(hook).not_to be_nil
|
||||||
messages_count = conversation.messages.count
|
messages_count = conversation.messages.count
|
||||||
|
|||||||
Reference in New Issue
Block a user