Skip to content

Commit

Permalink
[Performance] Optimize queries in conversation/message finders (chatw…
Browse files Browse the repository at this point in the history
…oot#364)

* [Performance] Optimize queries in conversation/message finders

* Add message_finder spec

* Fix message_finder spec
  • Loading branch information
pranavrajs authored and sojan-official committed Dec 15, 2019
1 parent cfc5670 commit a7cb75e
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,5 @@ public/packs*
# coverage report
buildreports
coverage

/storage
4 changes: 3 additions & 1 deletion app/finders/conversation_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ def set_assignee_type
end

def find_all_conversations
@conversations = current_account.conversations.where(inbox_id: @inbox_ids)
@conversations = current_account.conversations.includes(
:assignee, :contact, :inbox
).where(inbox_id: @inbox_ids)
end

def filter_by_assignee_type
Expand Down
8 changes: 6 additions & 2 deletions app/finders/message_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ def perform

private

def conversation_messages
@conversation.messages.includes(:attachment, user: { avatar_attachment: :blob })
end

def messages
return @conversation.messages if @params[:filter_internal_messages].blank?
return conversation_messages if @params[:filter_internal_messages].blank?

@conversation.messages.where.not('private = ? OR message_type = ?', true, 2)
conversation_messages.where.not('private = ? OR message_type = ?', true, 2)
end

def current_messages
Expand Down
2 changes: 1 addition & 1 deletion app/views/api/v1/conversations/index.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ json.data do
else
json.messages conversation.unread_messages.map(&:push_event_data)
end
json.inbox_id conversation.inbox.id
json.inbox_id conversation.inbox_id
json.status conversation.status_before_type_cast
json.timestamp conversation.messages.last.try(:created_at).try(:to_i)
json.user_last_seen_at conversation.user_last_seen_at.to_i
Expand Down
47 changes: 47 additions & 0 deletions spec/finders/message_finder_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require 'rails_helper'

describe ::MessageFinder do
subject(:message_finder) { described_class.new(conversation, params) }

let!(:account) { create(:account) }
let!(:user) { create(:user, account: account) }
let!(:inbox) { create(:inbox, account: account) }
let!(:conversation) { create(:complete_conversation, account: account, inbox: inbox, assignee: user) }

before do
create(:message, account: account, inbox: inbox, conversation: conversation)
create(:message, message_type: 'activity', account: account, inbox: inbox, conversation: conversation)
create(:message, message_type: 'activity', account: account, inbox: inbox, conversation: conversation)
create(:message, message_type: 'outgoing', account: account, inbox: inbox, conversation: conversation)
end

describe '#perform' do
context 'with filter_internal_messages false' do
let(:params) { { filter_internal_messages: false } }

it 'filter conversations by status' do
result = message_finder.perform
expect(result.count).to be 4
end
end

context 'with filter_internal_messages true' do
let(:params) { { filter_internal_messages: true } }

it 'filter conversations by status' do
result = message_finder.perform
expect(result.count).to be 2
end
end

context 'with before attribute' do
let!(:outgoing) { create(:message, message_type: 'outgoing', account: account, inbox: inbox, conversation: conversation) }
let(:params) { { before: outgoing.id } }

it 'filter conversations by status' do
result = message_finder.perform
expect(result.count).to be 4
end
end
end
end

0 comments on commit a7cb75e

Please sign in to comment.