Class: Protege::EmailThread
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Protege::EmailThread
- Includes:
- BroadcastableThread
- Defined in:
- app/models/protege/email_thread.rb
Overview
Groups every Message belonging to a single email conversation.
A thread is identified by its canonical thread_id: the RFC 2822 Message-ID of the conversation
root. For a reply that is the first entry of the References chain (falling back to In-Reply-To);
for a brand-new conversation it is the message's own Message-ID. Threads belong to one Agent
and carry denormalised counters (+message_count+, last_message_at) that Message keeps fresh
so the inbox can sort and display without aggregating on every render.
Constant Summary
Constants included from BroadcastableThread
Class Method Summary collapse
-
.find_or_create_for(mail:, agent:) ⇒ Protege::EmailThread
Find or create the thread for the given mail and agent.
-
.thread_id_for(mail) ⇒ String
Derive the canonical RFC 2822 thread-root ID from a mail object.
Instance Method Summary collapse
-
#latest_message ⇒ Protege::Message?
Return the most recently sent message in the thread.
Methods included from BroadcastableThread
#broadcast_thread_created, #broadcast_thread_removed, #broadcast_thread_updated
Class Method Details
.find_or_create_for(mail:, agent:) ⇒ Protege::EmailThread
Find or create the thread for the given mail and agent.
On creation, seeds the agent, subject, and last-activity timestamp from the mail.
Scoped to the agent: the same conversation reaching two agents resolves to a separate thread
for each, since thread_id is unique only within an agent.
76 77 78 79 80 81 |
# File 'app/models/protege/email_thread.rb', line 76 def find_or_create_for(mail:, agent:) find_or_create_by!(thread_id: thread_id_for(mail), agent:) do |t| t.subject = mail.subject.to_s t. = Time.current end end |
.thread_id_for(mail) ⇒ String
Derive the canonical RFC 2822 thread-root ID from a mail object.
Checks the References chain first (its head is the conversation root), then In-Reply-To,
then falls back to the message's own Message-ID for a new conversation. The result is
normalized through Gateway::Mail so every thread id is stored in canonical angle-bracketed
form regardless of how the raw header was punctuated.
60 61 62 63 |
# File 'app/models/protege/email_thread.rb', line 60 def thread_id_for(mail) Gateway.reference_ids(mail.references).first || Gateway.(mail.in_reply_to.presence || mail.) end |
Instance Method Details
#latest_message ⇒ Protege::Message?
Return the most recently sent message in the thread.
44 45 46 |
# File 'app/models/protege/email_thread.rb', line 44 def .order(:sent_at).last end |