cache this

This commit is contained in:
Pranav
2026-01-28 12:12:10 -08:00
parent e419cd5a07
commit e21f4f1dfe
10 changed files with 312 additions and 40 deletions
+3 -1
View File
@@ -180,7 +180,9 @@ class ConversationFinder
def conversations_base_query
@conversations.includes(
:taggings, :inbox, { assignee: { avatar_attachment: [:blob] } }, { contact: { avatar_attachment: [:blob] } }, :team, :contact_inbox
:taggings, :inbox, { assignee: { avatar_attachment: [:blob] } }, { contact: { avatar_attachment: [:blob] } }, :team, :contact_inbox,
{ cached_last_message: { attachments: { file_attachment: [:blob] } } },
:cached_last_non_activity_message
)
end
@@ -0,0 +1,22 @@
class Migration::BackfillConversationCachedMessageIdsJob < ApplicationJob
queue_as :low
def perform
Conversation.find_in_batches(batch_size: 1000) do |conversations|
conversations.each do |conversation|
updates = {}
last_message = conversation.messages.reorder(created_at: :desc).first
updates[:last_message_id] = last_message&.id
last_incoming = conversation.messages.incoming.reorder(created_at: :desc).first
updates[:last_incoming_message_id] = last_incoming&.id
last_non_activity = conversation.messages.where.not(message_type: :activity).reorder(created_at: :desc).first
updates[:last_non_activity_message_id] = last_non_activity&.id
conversation.update_columns(updates)
end
end
end
end
+3
View File
@@ -105,6 +105,9 @@ class Conversation < ApplicationRecord
belongs_to :contact_inbox
belongs_to :team, optional: true
belongs_to :campaign, optional: true
belongs_to :cached_last_message, class_name: 'Message', optional: true, foreign_key: 'last_message_id'
belongs_to :cached_last_incoming_message, class_name: 'Message', optional: true, foreign_key: 'last_incoming_message_id'
belongs_to :cached_last_non_activity_message, class_name: 'Message', optional: true, foreign_key: 'last_non_activity_message_id'
has_many :mentions, dependent: :destroy_async
has_many :messages, dependent: :destroy_async, autosave: true
+10
View File
@@ -133,6 +133,7 @@ class Message < ApplicationRecord
has_many :notifications, as: :primary_actor, dependent: :destroy_async
after_create_commit :execute_after_create_commit_callbacks
after_create_commit :update_conversation_cached_message_ids
after_update_commit :dispatch_update_event
after_commit :reindex_for_search, if: :should_index?, on: [:create, :update]
@@ -422,6 +423,15 @@ class Message < ApplicationRecord
def reindex_for_search
reindex(mode: :async)
end
def update_conversation_cached_message_ids
updates = { last_message_id: id }
updates[:last_incoming_message_id] = id if incoming?
updates[:last_non_activity_message_id] = id unless activity?
# rubocop:disable Rails/SkipsModelValidations
conversation.update_columns(updates)
# rubocop:enable Rails/SkipsModelValidations
end
end
Message.prepend_mod_with('Message')
@@ -27,13 +27,10 @@ json.meta do
end
json.id conversation.display_id
if conversation.messages.where(account_id: conversation.account_id).last.blank?
if conversation.cached_last_message.blank?
json.messages []
else
json.messages [
conversation.messages.where(account_id: conversation.account_id)
.includes([{ attachments: [{ file_attachment: [:blob] }] }]).last.try(:push_event_data)
]
json.messages [conversation.cached_last_message.push_event_data]
end
json.account_id conversation.account_id
@@ -54,7 +51,7 @@ json.updated_at conversation.updated_at.to_f
json.timestamp conversation.last_activity_at.to_i
json.first_reply_created_at conversation.first_reply_created_at.to_i
json.unread_count conversation.unread_incoming_messages.count
json.last_non_activity_message conversation.messages.where(account_id: conversation.account_id).non_activity_messages.first.try(:push_event_data)
json.last_non_activity_message conversation.cached_last_non_activity_message&.push_event_data
json.last_activity_at conversation.last_activity_at.to_i
json.priority conversation.priority
json.waiting_since conversation.waiting_since.to_i.to_i