chore: Improve the behavior of lock to single conversation (#7899)
This commit is contained in:
10
.rubocop.yml
10
.rubocop.yml
@@ -8,15 +8,11 @@ Layout/LineLength:
|
|||||||
Max: 150
|
Max: 150
|
||||||
|
|
||||||
Metrics/ClassLength:
|
Metrics/ClassLength:
|
||||||
Max: 125
|
Max: 175
|
||||||
Exclude:
|
Exclude:
|
||||||
- 'app/models/conversation.rb'
|
|
||||||
- 'app/models/contact.rb'
|
|
||||||
- 'app/mailers/conversation_reply_mailer.rb'
|
|
||||||
- 'app/models/message.rb'
|
- 'app/models/message.rb'
|
||||||
- 'app/builders/messages/facebook/message_builder.rb'
|
- 'app/models/conversation.rb'
|
||||||
- 'app/controllers/api/v1/accounts/contacts_controller.rb'
|
|
||||||
- 'app/listeners/action_cable_listener.rb'
|
|
||||||
RSpec/ExampleLength:
|
RSpec/ExampleLength:
|
||||||
Max: 25
|
Max: 25
|
||||||
Style/Documentation:
|
Style/Documentation:
|
||||||
|
|||||||
@@ -57,7 +57,13 @@ class Sms::IncomingMessageService
|
|||||||
end
|
end
|
||||||
|
|
||||||
def set_conversation
|
def set_conversation
|
||||||
@conversation = @contact_inbox.conversations.last
|
# if lock to single conversation is disabled, we will create a new conversation if previous conversation is resolved
|
||||||
|
@conversation = if @inbox.lock_to_single_conversation
|
||||||
|
@contact_inbox.conversations.last
|
||||||
|
else
|
||||||
|
@contact_inbox.conversations.where
|
||||||
|
.not(status: :resolved).last
|
||||||
|
end
|
||||||
return if @conversation
|
return if @conversation
|
||||||
|
|
||||||
@conversation = ::Conversation.create!(conversation_params)
|
@conversation = ::Conversation.create!(conversation_params)
|
||||||
|
|||||||
@@ -72,7 +72,13 @@ class Twilio::IncomingMessageService
|
|||||||
end
|
end
|
||||||
|
|
||||||
def set_conversation
|
def set_conversation
|
||||||
@conversation = @contact_inbox.conversations.first
|
# if lock to single conversation is disabled, we will create a new conversation if previous conversation is resolved
|
||||||
|
@conversation = if @inbox.lock_to_single_conversation
|
||||||
|
@contact_inbox.conversations.last
|
||||||
|
else
|
||||||
|
@contact_inbox.conversations.where
|
||||||
|
.not(status: :resolved).last
|
||||||
|
end
|
||||||
return if @conversation
|
return if @conversation
|
||||||
|
|
||||||
@conversation = ::Conversation.create!(conversation_params)
|
@conversation = ::Conversation.create!(conversation_params)
|
||||||
|
|||||||
@@ -91,7 +91,13 @@ class Whatsapp::IncomingMessageBaseService
|
|||||||
end
|
end
|
||||||
|
|
||||||
def set_conversation
|
def set_conversation
|
||||||
@conversation = @contact_inbox.conversations.last
|
# if lock to single conversation is disabled, we will create a new conversation if previous conversation is resolved
|
||||||
|
@conversation = if @inbox.lock_to_single_conversation
|
||||||
|
@contact_inbox.conversations.last
|
||||||
|
else
|
||||||
|
@contact_inbox.conversations
|
||||||
|
.where.not(status: :resolved).last
|
||||||
|
end
|
||||||
return if @conversation
|
return if @conversation
|
||||||
|
|
||||||
@conversation = ::Conversation.create!(conversation_params)
|
@conversation = ::Conversation.create!(conversation_params)
|
||||||
|
|||||||
@@ -37,7 +37,43 @@ describe Sms::IncomingMessageService do
|
|||||||
# no new conversation should be created
|
# no new conversation should be created
|
||||||
expect(sms_channel.inbox.conversations.count).to eq(3)
|
expect(sms_channel.inbox.conversations.count).to eq(3)
|
||||||
# message appended to the last conversation
|
# message appended to the last conversation
|
||||||
expect(last_conversation.messages.last.content).to eq('test message')
|
expect(last_conversation.messages.last.content).to eq(params[:text])
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'reopen last conversation if last conversation is resolved and lock to single conversation is enabled' do
|
||||||
|
sms_channel.inbox.update(lock_to_single_conversation: true)
|
||||||
|
contact_inbox = create(:contact_inbox, inbox: sms_channel.inbox, source_id: params[:from])
|
||||||
|
last_conversation = create(:conversation, inbox: sms_channel.inbox, contact_inbox: contact_inbox)
|
||||||
|
last_conversation.update(status: 'resolved')
|
||||||
|
described_class.new(inbox: sms_channel.inbox, params: params).perform
|
||||||
|
# no new conversation should be created
|
||||||
|
expect(sms_channel.inbox.conversations.count).to eq(1)
|
||||||
|
expect(sms_channel.inbox.conversations.open.last.messages.last.content).to eq(params[:text])
|
||||||
|
expect(sms_channel.inbox.conversations.open.last.status).to eq('open')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'creates a new conversation if last conversation is resolved and lock to single conversation is disabled' do
|
||||||
|
sms_channel.inbox.update(lock_to_single_conversation: false)
|
||||||
|
contact_inbox = create(:contact_inbox, inbox: sms_channel.inbox, source_id: params[:from])
|
||||||
|
last_conversation = create(:conversation, inbox: sms_channel.inbox, contact_inbox: contact_inbox)
|
||||||
|
last_conversation.update(status: 'resolved')
|
||||||
|
described_class.new(inbox: sms_channel.inbox, params: params).perform
|
||||||
|
# new conversation should be created
|
||||||
|
expect(sms_channel.inbox.conversations.count).to eq(2)
|
||||||
|
# message appended to the last conversation
|
||||||
|
expect(contact_inbox.conversations.last.messages.last.content).to eq(params[:text])
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'will not create a new conversation if last conversation is not resolved and lock to single conversation is disabled' do
|
||||||
|
sms_channel.inbox.update(lock_to_single_conversation: false)
|
||||||
|
contact_inbox = create(:contact_inbox, inbox: sms_channel.inbox, source_id: params[:from])
|
||||||
|
last_conversation = create(:conversation, inbox: sms_channel.inbox, contact_inbox: contact_inbox)
|
||||||
|
last_conversation.update(status: Conversation.statuses.except('resolved').keys.sample)
|
||||||
|
described_class.new(inbox: sms_channel.inbox, params: params).perform
|
||||||
|
# new conversation should be created
|
||||||
|
expect(sms_channel.inbox.conversations.count).to eq(1)
|
||||||
|
# message appended to the last conversation
|
||||||
|
expect(contact_inbox.conversations.last.messages.last.content).to eq(params[:text])
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'creates attachment messages and ignores .smil files' do
|
it 'creates attachment messages and ignores .smil files' do
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ describe Twilio::IncomingMessageService do
|
|||||||
expect(conversation.reload.messages.last.content).to be_nil
|
expect(conversation.reload.messages.last.content).to be_nil
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'creates a new conversation' do
|
it 'creates a new conversation when payload is from different number' do
|
||||||
params = {
|
params = {
|
||||||
SmsSid: 'SMxx',
|
SmsSid: 'SMxx',
|
||||||
From: '+123456',
|
From: '+123456',
|
||||||
@@ -62,6 +62,7 @@ describe Twilio::IncomingMessageService do
|
|||||||
expect(twilio_channel.inbox.conversations.count).to eq(2)
|
expect(twilio_channel.inbox.conversations.count).to eq(2)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Since we support the case with phone number as well. the previous case is with accoud_sid and messaging_service_sid
|
||||||
context 'with a phone number' do
|
context 'with a phone number' do
|
||||||
let!(:twilio_channel) do
|
let!(:twilio_channel) do
|
||||||
create(:channel_twilio_sms, :with_phone_number, account: account, account_sid: 'ACxxx',
|
create(:channel_twilio_sms, :with_phone_number, account: account, account_sid: 'ACxxx',
|
||||||
@@ -81,7 +82,7 @@ describe Twilio::IncomingMessageService do
|
|||||||
expect(conversation.reload.messages.last.content).to eq('testing3')
|
expect(conversation.reload.messages.last.content).to eq('testing3')
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'creates a new conversation' do
|
it 'creates a new conversation when payload is from different number' do
|
||||||
params = {
|
params = {
|
||||||
SmsSid: 'SMxx',
|
SmsSid: 'SMxx',
|
||||||
From: '+123456',
|
From: '+123456',
|
||||||
@@ -93,6 +94,55 @@ describe Twilio::IncomingMessageService do
|
|||||||
described_class.new(params: params).perform
|
described_class.new(params: params).perform
|
||||||
expect(twilio_channel.inbox.conversations.count).to eq(2)
|
expect(twilio_channel.inbox.conversations.count).to eq(2)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'reopen last conversation if last conversation is resolved and lock to single conversation is enabled' do
|
||||||
|
params = {
|
||||||
|
SmsSid: 'SMxx',
|
||||||
|
From: '+12345',
|
||||||
|
AccountSid: 'ACxxx',
|
||||||
|
To: twilio_channel.phone_number,
|
||||||
|
Body: 'testing3'
|
||||||
|
}
|
||||||
|
|
||||||
|
twilio_channel.inbox.update(lock_to_single_conversation: true)
|
||||||
|
conversation.update(status: 'resolved')
|
||||||
|
described_class.new(params: params).perform
|
||||||
|
# message appended to the last conversation
|
||||||
|
expect(conversation.reload.messages.last.content).to eq('testing3')
|
||||||
|
expect(conversation.reload.status).to eq('open')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'creates a new conversation if last conversation is resolved and lock to single conversation is disabled' do
|
||||||
|
params = {
|
||||||
|
SmsSid: 'SMxx',
|
||||||
|
From: '+12345',
|
||||||
|
AccountSid: 'ACxxx',
|
||||||
|
To: twilio_channel.phone_number,
|
||||||
|
Body: 'testing3'
|
||||||
|
}
|
||||||
|
|
||||||
|
twilio_channel.inbox.update(lock_to_single_conversation: false)
|
||||||
|
conversation.update(status: 'resolved')
|
||||||
|
described_class.new(params: params).perform
|
||||||
|
expect(twilio_channel.inbox.conversations.count).to eq(2)
|
||||||
|
expect(twilio_channel.inbox.conversations.last.messages.last.content).to eq('testing3')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'will not create a new conversation if last conversation is not resolved and lock to single conversation is disabled' do
|
||||||
|
params = {
|
||||||
|
SmsSid: 'SMxx',
|
||||||
|
From: '+12345',
|
||||||
|
AccountSid: 'ACxxx',
|
||||||
|
To: twilio_channel.phone_number,
|
||||||
|
Body: 'testing3'
|
||||||
|
}
|
||||||
|
|
||||||
|
twilio_channel.inbox.update(lock_to_single_conversation: false)
|
||||||
|
conversation.update(status: Conversation.statuses.except('resolved').keys.sample)
|
||||||
|
described_class.new(params: params).perform
|
||||||
|
expect(twilio_channel.inbox.conversations.count).to eq(1)
|
||||||
|
expect(twilio_channel.inbox.conversations.last.messages.last.content).to eq('testing3')
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'with multiple channels configured' do
|
context 'with multiple channels configured' do
|
||||||
|
|||||||
@@ -35,6 +35,41 @@ describe Whatsapp::IncomingMessageService do
|
|||||||
expect(last_conversation.messages.last.content).to eq(params[:messages].first[:text][:body])
|
expect(last_conversation.messages.last.content).to eq(params[:messages].first[:text][:body])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'reopen last conversation if last conversation is resolved and lock to single conversation is enabled' do
|
||||||
|
whatsapp_channel.inbox.update(lock_to_single_conversation: true)
|
||||||
|
contact_inbox = create(:contact_inbox, inbox: whatsapp_channel.inbox, source_id: params[:messages].first[:from])
|
||||||
|
last_conversation = create(:conversation, inbox: whatsapp_channel.inbox, contact_inbox: contact_inbox)
|
||||||
|
last_conversation.update(status: 'resolved')
|
||||||
|
described_class.new(inbox: whatsapp_channel.inbox, params: params).perform
|
||||||
|
# no new conversation should be created
|
||||||
|
expect(whatsapp_channel.inbox.conversations.count).to eq(1)
|
||||||
|
# message appended to the last conversation
|
||||||
|
expect(last_conversation.messages.last.content).to eq(params[:messages].first[:text][:body])
|
||||||
|
expect(last_conversation.reload.status).to eq('open')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'creates a new conversation if last conversation is resolved and lock to single conversation is disabled' do
|
||||||
|
whatsapp_channel.inbox.update(lock_to_single_conversation: false)
|
||||||
|
contact_inbox = create(:contact_inbox, inbox: whatsapp_channel.inbox, source_id: params[:messages].first[:from])
|
||||||
|
last_conversation = create(:conversation, inbox: whatsapp_channel.inbox, contact_inbox: contact_inbox)
|
||||||
|
last_conversation.update(status: 'resolved')
|
||||||
|
described_class.new(inbox: whatsapp_channel.inbox, params: params).perform
|
||||||
|
# new conversation should be created
|
||||||
|
expect(whatsapp_channel.inbox.conversations.count).to eq(2)
|
||||||
|
expect(contact_inbox.conversations.last.messages.last.content).to eq(params[:messages].first[:text][:body])
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'will not create a new conversation if last conversation is not resolved and lock to single conversation is disabled' do
|
||||||
|
whatsapp_channel.inbox.update(lock_to_single_conversation: false)
|
||||||
|
contact_inbox = create(:contact_inbox, inbox: whatsapp_channel.inbox, source_id: params[:messages].first[:from])
|
||||||
|
last_conversation = create(:conversation, inbox: whatsapp_channel.inbox, contact_inbox: contact_inbox)
|
||||||
|
last_conversation.update(status: Conversation.statuses.except('resolved').keys.sample)
|
||||||
|
described_class.new(inbox: whatsapp_channel.inbox, params: params).perform
|
||||||
|
# new conversation should be created
|
||||||
|
expect(whatsapp_channel.inbox.conversations.count).to eq(1)
|
||||||
|
expect(contact_inbox.conversations.last.messages.last.content).to eq(params[:messages].first[:text][:body])
|
||||||
|
end
|
||||||
|
|
||||||
it 'will not create duplicate messages when same message is received' do
|
it 'will not create duplicate messages when same message is received' do
|
||||||
described_class.new(inbox: whatsapp_channel.inbox, params: params).perform
|
described_class.new(inbox: whatsapp_channel.inbox, params: params).perform
|
||||||
expect(whatsapp_channel.inbox.messages.count).to eq(1)
|
expect(whatsapp_channel.inbox.messages.count).to eq(1)
|
||||||
|
|||||||
Reference in New Issue
Block a user