diff --git a/.gitignore b/.gitignore index c024460ab..7f299df70 100644 --- a/.gitignore +++ b/.gitignore @@ -43,3 +43,5 @@ public/packs* # coverage report buildreports coverage + +/storage diff --git a/app/finders/conversation_finder.rb b/app/finders/conversation_finder.rb index cc41bb557..b6c6f966c 100644 --- a/app/finders/conversation_finder.rb +++ b/app/finders/conversation_finder.rb @@ -66,7 +66,9 @@ class ConversationFinder 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 diff --git a/app/finders/message_finder.rb b/app/finders/message_finder.rb index e839e4946..f21093b48 100644 --- a/app/finders/message_finder.rb +++ b/app/finders/message_finder.rb @@ -10,10 +10,14 @@ class MessageFinder private - def messages - return @conversation.messages if @params[:filter_internal_messages].blank? + def conversation_messages + @conversation.messages.includes(:attachment, user: { avatar_attachment: :blob }) + end - @conversation.messages.where.not('private = ? OR message_type = ?', true, 2) + def messages + return conversation_messages if @params[:filter_internal_messages].blank? + + conversation_messages.where.not('private = ? OR message_type = ?', true, 2) end def current_messages diff --git a/app/views/api/v1/conversations/index.json.jbuilder b/app/views/api/v1/conversations/index.json.jbuilder index 0f73c85ef..d21aa740d 100644 --- a/app/views/api/v1/conversations/index.json.jbuilder +++ b/app/views/api/v1/conversations/index.json.jbuilder @@ -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 diff --git a/spec/finders/message_finder_spec.rb b/spec/finders/message_finder_spec.rb new file mode 100644 index 000000000..6676987b2 --- /dev/null +++ b/spec/finders/message_finder_spec.rb @@ -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