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
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')