chore: Add delay for slack job when message has attachments (#7107)

This commit is contained in:
Tejaswini Chile
2023-05-22 13:51:14 +05:30
committed by GitHub
parent d6ce1ceeeb
commit 0e903d2365
4 changed files with 56 additions and 9 deletions

View File

@@ -20,8 +20,14 @@ class HookJob < ApplicationJob
return unless ['message.created'].include?(event_name)
message = event_data[:message]
Integrations::Slack::SendOnSlackService.new(message: message, hook: hook).perform
if message.attachments.blank?
::SendOnSlackJob.perform_later(message: message,
hook: hook)
else
::SendOnSlackJob.set(wait: 2.seconds).perform_later(
message: message, hook: hook
)
end
end
def process_dialogflow_integration(hook, event_name, event_data)

View File

@@ -0,0 +1,8 @@
class SendOnSlackJob < ApplicationJob
queue_as :medium
pattr_initialize [:message!, :hook!]
def perform
Integrations::Slack::SendOnSlackService.new(message: message, hook: hook).perform
end
end

View File

@@ -22,18 +22,19 @@ RSpec.describe HookJob do
allow(process_service).to receive(:perform)
end
it 'calls Integrations::Slack::SendOnSlackService when its a slack hook' do
it 'calls SendOnSlackJob when its a slack hook' do
hook = create(:integrations_hook, app_id: 'slack', account: account)
allow(Integrations::Slack::SendOnSlackService).to receive(:new).and_return(process_service)
expect(Integrations::Slack::SendOnSlackService).to receive(:new).with(message: event_data[:message], hook: hook)
allow(SendOnSlackJob).to receive(:perform_later).and_return(process_service)
expect(SendOnSlackJob).to receive(:perform_later).with(message: event_data[:message], hook: hook)
described_class.perform_now(hook, event_name, event_data)
end
it 'calls Integrations::Slack::SendOnSlackService when its a slack hook for template message' do
event_data = { message: create(:message, account: account, message_type: :template) }
it 'calls SendOnSlackJob when its a slack hook for message with attachments' do
event_data = { message: create(:message, :with_attachment, account: account) }
hook = create(:integrations_hook, app_id: 'slack', account: account)
allow(Integrations::Slack::SendOnSlackService).to receive(:new).and_return(process_service)
expect(Integrations::Slack::SendOnSlackService).to receive(:new)
allow(SendOnSlackJob).to receive(:set).with(wait: 2.seconds).and_return(SendOnSlackJob)
allow(SendOnSlackJob).to receive(:perform_later).and_return(process_service)
expect(SendOnSlackJob).to receive(:perform_later).with(message: event_data[:message], hook: hook)
described_class.perform_now(hook, event_name, event_data)
end

View File

@@ -0,0 +1,32 @@
require 'rails_helper'
RSpec.describe SendOnSlackJob do
let(:account) { create(:account) }
let(:hook) { create(:integrations_hook, account: account) }
let(:inbox) { create(:inbox, account: account) }
let(:event_name) { 'message.created' }
let(:event_data) { { message: create(:message, account: account, content: 'muchas muchas gracias', message_type: :incoming) } }
context 'when handleable events like message.created' do
let(:process_service) { double }
before do
allow(process_service).to receive(:perform)
end
it 'calls Integrations::Slack::SendOnSlackService when its a slack hook' do
hook = create(:integrations_hook, app_id: 'slack', account: account)
allow(Integrations::Slack::SendOnSlackService).to receive(:new).and_return(process_service)
expect(Integrations::Slack::SendOnSlackService).to receive(:new).with(message: event_data[:message], hook: hook)
described_class.perform_now(message: event_data[:message], hook: hook)
end
it 'calls Integrations::Slack::SendOnSlackService when its a slack hook for template message' do
event_data = { message: create(:message, account: account, message_type: :template) }
hook = create(:integrations_hook, app_id: 'slack', account: account)
allow(Integrations::Slack::SendOnSlackService).to receive(:new).and_return(process_service)
expect(Integrations::Slack::SendOnSlackService).to receive(:new).with(message: event_data[:message], hook: hook)
described_class.perform_now(message: event_data[:message], hook: hook)
end
end
end