diff --git a/app/finders/conversation_finder.rb b/app/finders/conversation_finder.rb index ecbccfb88..58e911362 100644 --- a/app/finders/conversation_finder.rb +++ b/app/finders/conversation_finder.rb @@ -2,6 +2,11 @@ class ConversationFinder attr_reader :current_user, :current_account, :params DEFAULT_STATUS = 'open'.freeze + SORT_OPTIONS = { + latest: 'latest', + sort_on_created_at: 'sort_on_created_at', + last_user_message_at: 'last_user_message_at' + }.with_indifferent_access # assumptions # inbox_id if not given, take from all conversations, else specific to inbox @@ -133,10 +138,7 @@ class ConversationFinder @conversations = @conversations.includes( :taggings, :inbox, { assignee: { avatar_attachment: [:blob] } }, { contact: { avatar_attachment: [:blob] } }, :team, :contact_inbox ) - if params[:conversation_type] == 'mention' - @conversations.page(current_page) - else - @conversations.latest.page(current_page) - end + sort_by = SORT_OPTIONS[params[:sort_by]] || SORT_OPTIONS['latest'] + @conversations.send(sort_by).page(current_page) end end diff --git a/app/models/concerns/sort_handler.rb b/app/models/concerns/sort_handler.rb new file mode 100644 index 000000000..01e124d1b --- /dev/null +++ b/app/models/concerns/sort_handler.rb @@ -0,0 +1,25 @@ +module SortHandler + extend ActiveSupport::Concern + + included do + def self.latest + order(last_activity_at: :desc) + end + + def self.sort_on_created_at + order(created_at: :asc) + end + + def self.last_messaged_conversations + Message.except(:order).select('DISTINCT ON (conversation_id) *').order('conversation_id, created_at DESC') + end + + def self.sort_on_last_user_message_at + where( + 'grouped_conversations.message_type = 0' + ).order( + 'grouped_conversations.created_at ASC' + ) + end + end +end diff --git a/app/models/conversation.rb b/app/models/conversation.rb index 4791ba924..49b827b68 100644 --- a/app/models/conversation.rb +++ b/app/models/conversation.rb @@ -50,6 +50,7 @@ class Conversation < ApplicationRecord include RoundRobinHandler include ActivityMessageHandler include UrlHelper + include SortHandler validates :account_id, presence: true validates :inbox_id, presence: true @@ -60,7 +61,6 @@ class Conversation < ApplicationRecord enum status: { open: 0, resolved: 1, pending: 2, snoozed: 3 } - scope :latest, -> { order(last_activity_at: :desc) } scope :unassigned, -> { where(assignee_id: nil) } scope :assigned, -> { where.not(assignee_id: nil) } scope :assigned_to, ->(agent) { where(assignee_id: agent.id) } @@ -70,6 +70,13 @@ class Conversation < ApplicationRecord open.where('last_activity_at < ? ', Time.now.utc - auto_resolve_duration.days) } + scope :last_user_message_at, lambda { + joins( + "INNER JOIN (#{last_messaged_conversations.to_sql}) grouped_conversations + ON grouped_conversations.conversation_id = conversations.id" + ).sort_on_last_user_message_at + } + belongs_to :account belongs_to :inbox belongs_to :assignee, class_name: 'User', optional: true diff --git a/app/models/mention.rb b/app/models/mention.rb index b326d88b6..baa7acad4 100644 --- a/app/models/mention.rb +++ b/app/models/mention.rb @@ -23,6 +23,8 @@ # fk_rails_... (user_id => users.id) ON DELETE => cascade # class Mention < ApplicationRecord + include SortHandler + before_validation :ensure_account_id validates :mentioned_at, presence: true validates :account_id, presence: true @@ -38,6 +40,17 @@ class Mention < ApplicationRecord scope :latest, -> { order(mentioned_at: :desc) } + def self.last_user_message_at + # INNER query finds the last message created in the conversation group + # The outer query JOINS with the latest created message conversations + # Then select only latest incoming message from the conversations which doesn't have last message as outgoing + # Order by message created_at + Mention.joins( + "INNER JOIN (#{last_messaged_conversations.to_sql}) grouped_conversations + ON grouped_conversations.conversation_id = mentions.conversation_id" + ).sort_on_last_user_message_at + end + private def ensure_account_id diff --git a/spec/factories/mentions.rb b/spec/factories/mentions.rb new file mode 100644 index 000000000..623102e6b --- /dev/null +++ b/spec/factories/mentions.rb @@ -0,0 +1,10 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :mention do + mentioned_at { Time.current } + account + conversation + user + end +end diff --git a/spec/models/conversation_spec.rb b/spec/models/conversation_spec.rb index 1b1e84ea5..95b205541 100644 --- a/spec/models/conversation_spec.rb +++ b/spec/models/conversation_spec.rb @@ -621,4 +621,73 @@ RSpec.describe Conversation, type: :model do expect(conversation['additional_attributes']['referer']).to eq('https://www.chatwoot.com/') end end + + describe 'Custom Sort' do + include ActiveJob::TestHelper + + let!(:conversation_4) { create(:conversation, created_at: DateTime.now - 10.days, last_activity_at: DateTime.now - 10.days) } + let!(:conversation_3) { create(:conversation, created_at: DateTime.now - 9.days, last_activity_at: DateTime.now - 9.days) } + let!(:conversation_1) { create(:conversation, created_at: DateTime.now - 8.days, last_activity_at: DateTime.now - 8.days) } + let!(:conversation_2) { create(:conversation, created_at: DateTime.now - 6.days, last_activity_at: DateTime.now - 6.days) } + + it 'Sort conversations based on created_at' do + records = described_class.sort_on_created_at + + expect(records.first.id).to eq(conversation_4.id) + expect(records.last.id).to eq(conversation_2.id) + end + + it 'Sort conversations based on last_user_message_at' do + create(:message, conversation_id: conversation_3.id, message_type: :outgoing, created_at: DateTime.now - 9.days) + create(:message, conversation_id: conversation_1.id, message_type: :incoming, created_at: DateTime.now - 8.days) + create(:message, conversation_id: conversation_1.id, message_type: :incoming, created_at: DateTime.now - 8.days) + create(:message, conversation_id: conversation_1.id, message_type: :outgoing, created_at: DateTime.now - 7.days) + create(:message, conversation_id: conversation_2.id, message_type: :incoming, created_at: DateTime.now - 6.days) + create(:message, conversation_id: conversation_2.id, message_type: :incoming, created_at: DateTime.now - 6.days) + create(:message, conversation_id: conversation_3.id, message_type: :incoming, created_at: DateTime.now - 6.days) + create(:message, conversation_id: conversation_3.id, message_type: :incoming, created_at: DateTime.now - 6.days) + create(:message, conversation_id: conversation_3.id, message_type: :incoming, created_at: DateTime.now - 2.days) + + records = described_class.last_user_message_at + + expect(records[0]['id']).to eq(conversation_2.id) + expect(records[1]['id']).to eq(conversation_3.id) + expect(records.pluck(:id)).not_to include(conversation_4.id) + end + + context 'when last_activity_at updated by some actions' do + before do + create(:message, conversation_id: conversation_1.id, message_type: :incoming, created_at: DateTime.now - 8.days) + create(:message, conversation_id: conversation_2.id, message_type: :incoming, created_at: DateTime.now - 6.days) + create(:message, conversation_id: conversation_3.id, message_type: :incoming, created_at: DateTime.now - 2.days) + end + + it 'sort conversations with latest resolved conversation at first' do + records = described_class.latest + + expect(records.first.id).to eq(conversation_3.id) + + conversation_1.toggle_status + perform_enqueued_jobs do + Conversations::ActivityMessageJob.perform_later( + conversation_1, + account_id: conversation_1.account_id, + inbox_id: conversation_1.inbox_id, + message_type: :activity, + content: 'Conversation was marked resolved by system due to days of inactivity' + ) + end + records = described_class.latest + + expect(records.first.id).to eq(conversation_1.id) + end + + it 'Sort conversations with latest message' do + create(:message, conversation_id: conversation_3.id, message_type: :incoming, created_at: DateTime.now) + records = described_class.latest + + expect(records.first.id).to eq(conversation_3.id) + end + end + end end diff --git a/spec/models/mention_spec.rb b/spec/models/mention_spec.rb new file mode 100644 index 000000000..5d4c71984 --- /dev/null +++ b/spec/models/mention_spec.rb @@ -0,0 +1,67 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe Mention, type: :model do + describe 'associations' do + it { is_expected.to belong_to(:account) } + it { is_expected.to belong_to(:user) } + it { is_expected.to belong_to(:conversation) } + end + + describe 'Custom Sort' do + let!(:account) { create(:account) } + let!(:user_1) { create(:user, email: 'agent2@example.com', account: account) } + let!(:user_2) { create(:user, email: 'agent11@example.com', account: account) } + let!(:conversation_1) { create(:conversation, created_at: DateTime.now - 8.days) } + let!(:conversation_2) { create(:conversation, created_at: DateTime.now - 6.days) } + let!(:conversation_3) { create(:conversation, created_at: DateTime.now - 9.days) } + let!(:conversation_4) { create(:conversation, created_at: DateTime.now - 10.days) } + + let!(:mention_1) { create(:mention, account: account, conversation: conversation_1, user: user_1) } + let!(:mention_2) { create(:mention, account: account, conversation: conversation_2, user: user_1) } + let!(:mention_3) { create(:mention, account: account, conversation: conversation_3, user: user_1) } + + it 'Sort mentioned conversations based on created_at' do + records = described_class.sort_on_created_at + + expect(records.first.id).to eq(mention_1.id) + expect(records.first.conversation_id).to eq(conversation_1.id) + expect(records.last.conversation_id).to eq(conversation_3.id) + end + + it 'Sort mentioned conversations based on last_user_message_at' do + create(:message, conversation_id: conversation_3.id, message_type: :incoming, created_at: DateTime.now - 2.days) + create(:message, conversation_id: conversation_2.id, message_type: :incoming, created_at: DateTime.now - 6.days) + create(:message, conversation_id: conversation_2.id, message_type: :incoming, created_at: DateTime.now - 6.days) + create(:message, conversation_id: conversation_3.id, message_type: :incoming, created_at: DateTime.now - 6.days) + create(:message, conversation_id: conversation_3.id, message_type: :incoming, created_at: DateTime.now - 6.days) + create(:message, conversation_id: conversation_1.id, message_type: :outgoing, created_at: DateTime.now - 7.days) + create(:message, conversation_id: conversation_1.id, message_type: :incoming, created_at: DateTime.now - 8.days) + create(:message, conversation_id: conversation_1.id, message_type: :incoming, created_at: DateTime.now - 8.days) + create(:message, conversation_id: conversation_3.id, message_type: :outgoing, created_at: DateTime.now - 9.days) + + records = described_class.last_user_message_at + + expect(records.first.id).to eq(mention_2.id) + expect(records.first.conversation_id).to eq(conversation_2.id) + expect(records.last.conversation_id).to eq(conversation_3.id) + expect(records.pluck(:id)).not_to include(conversation_4.id) + end + + it 'Sort conversations based on mentioned_at' do + records = described_class.latest + + expect(records.first.id).to eq(mention_3.id) + expect(records.first.conversation_id).to eq(conversation_3.id) + expect(records.last.conversation_id).to eq(conversation_1.id) + + travel_to DateTime.now + 1.day + mention = create(:mention, account: account, conversation: conversation_2, user: user_2) + records = described_class.latest + + expect(records.first.conversation_id).to eq(conversation_2.id) + expect(mention.created_at).to eq(DateTime.now) + end + end +end