feat: prevent deleted email conversations from syncing again (#14612)

# Pull Request Template

## Description

Prevent deleted email conversations from being synced into Chatwoot
again while they are still within the IMAP sync window.

When an admin explicitly deletes an email conversation, the incoming
email message IDs are stored temporarily in Redis. IMAP sync checks
these recently deleted message IDs in addition to existing message
records. Each Redis key expires automatically after two days.

This applies only to explicit conversation deletion. Individual message
deletion, inbox deletion, and account deletion keep their existing
behavior.

Fixes
[CW-7214](https://linear.app/chatwoot/issue/CW-7214/deleted-mails-in-gmail-inbox-gets-synced-again)
This commit is contained in:
Vishnu Narayanan
2026-06-15 17:06:40 +05:30
committed by GitHub
parent 35bef21f83
commit ee6382109a
7 changed files with 109 additions and 2 deletions
@@ -0,0 +1,35 @@
require 'rails_helper'
RSpec.describe Conversations::DeleteService do
let(:account) { create(:account) }
let(:user) { create(:user, account: account) }
let(:ip) { '127.0.0.1' }
let(:service) { described_class.new(conversation: conversation, user: user, ip: ip) }
context 'when deleting an email conversation' do
let(:inbox) { create(:channel_email, :imap_email, account: account).inbox }
let(:conversation) { create(:conversation, account: account, inbox: inbox) }
let!(:incoming_message) { create(:message, account: account, inbox: inbox, conversation: conversation, source_id: 'incoming@example.com') }
let(:deleted_message_tracker) { instance_double(Imap::DeletedMessageTracker, record: true) }
before do
allow(Imap::DeletedMessageTracker).to receive(:new).with(inbox: inbox).and_return(deleted_message_tracker)
end
it 'records incoming message source ids and enqueues the deletion job' do
expect { service.perform }.to have_enqueued_job(DeleteObjectJob).with(conversation, user, ip)
expect(deleted_message_tracker).to have_received(:record).with([incoming_message.source_id])
end
end
context 'when deleting a non-email conversation' do
let(:conversation) { create(:conversation, account: account) }
it 'enqueues the deletion job without recording message source ids' do
expect(Imap::DeletedMessageTracker).not_to receive(:new)
expect { service.perform }.to have_enqueued_job(DeleteObjectJob).with(conversation, user, ip)
end
end
end
@@ -119,6 +119,28 @@ RSpec.describe Imap::FetchEmailService do
end
end
it 'does not return recently deleted emails' do
travel_to '26.10.2020 10:00'.to_datetime do
email_object = create_inbound_email_from_fixture('only_text.eml')
email_header = Net::IMAP::FetchData.new(1, 'BODY[HEADER]' => eml_content_with_message_id)
redis_key = format(Redis::RedisKeys::IMAP_DELETED_MESSAGE,
inbox_id: imap_email_channel.inbox.id,
message_id_digest: Digest::SHA256.hexdigest(email_object.message_id))
Imap::DeletedMessageTracker.new(inbox: imap_email_channel.inbox).record([email_object.message_id])
allow(imap).to receive(:search).with(%w[SINCE 25-Oct-2020]).and_return([1])
allow(imap).to receive(:fetch).with([1], 'BODY.PEEK[HEADER]').and_return([email_header])
allow(imap).to receive(:logout)
result = described_class.new(channel: imap_email_channel).perform
expect(result).to be_empty
expect(imap).not_to have_received(:fetch).with(1, 'RFC822')
ensure
Redis::Alfred.delete(redis_key) if redis_key
end
end
it 'does not count emails without message ids toward the sync limit' do
travel_to '26.10.2020 10:00'.to_datetime do
email_object = create_inbound_email_from_fixture('only_text.eml')