feat: preload messages_count

This commit is contained in:
Shivam Mishra
2023-05-04 13:17:55 +05:30
parent 9cff6c04ee
commit d3afe64bb1
2 changed files with 7 additions and 1 deletions
@@ -60,6 +60,12 @@ class Conversations::FilterService < FilterService
@conversations = @conversations.includes(
:taggings, :inbox, { assignee: { avatar_attachment: [:blob] } }, { contact: { avatar_attachment: [:blob] } }, :team
)
@conversations = @conversations
.left_joins(:messages)
.select('conversations.*, COUNT(DISTINCT messages.id) AS message_count')
.group('conversations.id')
@conversations.latest.page(current_page)
end
end
@@ -17,7 +17,7 @@ json.meta do
end
json.id conversation.display_id
if conversation.messages.count.zero?
if conversation.message_count.zero?
json.messages []
elsif conversation.unread_incoming_messages.count.zero?
json.messages [conversation.messages.includes([{ attachments: [{ file_attachment: [:blob] }] }]).last.try(:push_event_data)]