## Summary Outbound email messages never populate `content_attributes.email.subject`. The subject lives only on the parent conversation's `additional_attributes.mail_subject`. The search index doc built by `Messages::SearchDataPresenter` pulls from the message-level field only, so outbound email subjects are unsearchable for accounts on the advanced_search (Elasticsearch) path. This change makes the presenter fall back to `conversation.additional_attributes.mail_subject` when the message-level subject is blank. Inbound email messages keep their existing behavior (message-level subject takes precedence). Closes https://linear.app/chatwoot/issue/CW-6877 ## What changes - `Messages::SearchDataPresenter#content_attributes_data` now falls back to the conversation's `mail_subject` when the message-level subject is blank. - Added specs covering the fallback, precedence, and the neither-set case. ## What does not change - No schema changes, no migrations, no backfill of existing OpenSearch documents. - Searchkick's `after_commit :reindex_for_search` on `Message` will pick up the new field for all newly created or updated messages via the existing indexing path. - Postgres search path (free accounts, fallback) is untouched. Broader subject search for those users is a separate follow-up.
103 lines
3.6 KiB
Ruby
103 lines
3.6 KiB
Ruby
require 'rails_helper'
|
|
|
|
RSpec.describe Messages::SearchDataPresenter do
|
|
let(:presenter) { described_class.new(message) }
|
|
let(:account) { create(:account) }
|
|
let(:inbox) { create(:inbox, account: account) }
|
|
let(:contact) { create(:contact, account: account) }
|
|
let(:conversation) { create(:conversation, account: account, inbox: inbox, contact: contact) }
|
|
let(:message) { create(:message, account: account, inbox: inbox, conversation: conversation, sender: contact) }
|
|
|
|
describe '#search_data' do
|
|
let(:expected_data) do
|
|
{
|
|
content: message.content,
|
|
account_id: message.account_id,
|
|
inbox_id: message.inbox_id,
|
|
conversation_id: message.conversation_id,
|
|
message_type: message.message_type,
|
|
private: message.private,
|
|
created_at: message.created_at,
|
|
source_id: message.source_id,
|
|
sender_id: message.sender_id,
|
|
sender_type: message.sender_type,
|
|
conversation: {
|
|
id: conversation.display_id
|
|
}
|
|
}
|
|
end
|
|
|
|
it 'returns search index payload with core fields' do
|
|
expect(presenter.search_data).to include(expected_data)
|
|
end
|
|
|
|
context 'with attachments' do
|
|
before do
|
|
attachment = message.attachments.new(account_id: message.account_id, file_type: :image)
|
|
attachment.file.attach(io: Rails.root.join('spec/assets/avatar.png').open, filename: 'avatar.png', content_type: 'image/png')
|
|
attachment.meta = { 'transcribed_text' => 'Hello world' }
|
|
end
|
|
|
|
it 'includes attachment transcriptions' do
|
|
attachments_data = presenter.search_data[:attachments]
|
|
expect(attachments_data).to be_an(Array)
|
|
expect(attachments_data.first).to include(transcribed_text: 'Hello world')
|
|
end
|
|
end
|
|
|
|
context 'with email content attributes' do
|
|
before do
|
|
message.update(
|
|
content_attributes: { email: { subject: 'Test Subject' } }
|
|
)
|
|
end
|
|
|
|
it 'includes email subject' do
|
|
content_attrs = presenter.search_data[:content_attributes]
|
|
expect(content_attrs[:email][:subject]).to eq('Test Subject')
|
|
end
|
|
end
|
|
|
|
context 'when the message has no email subject but the conversation has a mail_subject' do
|
|
before do
|
|
conversation.update!(additional_attributes: { 'mail_subject' => 'Conversation Subject' })
|
|
end
|
|
|
|
it 'falls back to the conversation mail_subject' do
|
|
content_attrs = presenter.search_data[:content_attributes]
|
|
expect(content_attrs[:email][:subject]).to eq('Conversation Subject')
|
|
end
|
|
end
|
|
|
|
context 'when both the message email subject and conversation mail_subject are set' do
|
|
before do
|
|
conversation.update!(additional_attributes: { 'mail_subject' => 'Conversation subject' })
|
|
message.update!(content_attributes: { email: { subject: 'Message subject' } })
|
|
end
|
|
|
|
it 'prefers the message-level email subject' do
|
|
content_attrs = presenter.search_data[:content_attributes]
|
|
expect(content_attrs[:email][:subject]).to eq('Message subject')
|
|
end
|
|
end
|
|
|
|
context 'when neither message nor conversation has an email subject' do
|
|
it 'omits the email subject from content_attributes' do
|
|
expect(presenter.search_data[:content_attributes]).to eq({})
|
|
end
|
|
end
|
|
|
|
context 'with campaign and automation data' do
|
|
before do
|
|
message.update(
|
|
content_attributes: { 'automation_rule_id' => '456' }
|
|
)
|
|
end
|
|
|
|
it 'includes automation_rule_id' do
|
|
expect(presenter.search_data[:additional_attributes][:automation_rule_id]).to eq('456')
|
|
end
|
|
end
|
|
end
|
|
end
|