chore: Email improvements. (#4901)

* Update email processing logic.
* Fix sentry issues
This commit is contained in:
Aswin Dev P.S
2022-07-08 16:43:24 +05:30
committed by GitHub
parent 19c637eb33
commit 13a4e0e6d9
3 changed files with 31 additions and 18 deletions

View File

@@ -34,16 +34,11 @@ class Inboxes::FetchImapEmailsJob < ApplicationJob
enable_ssl: channel.imap_enable_ssl
end
new_mails = false
Mail.find(what: :last, count: 10, order: :desc).each do |inbound_mail|
next unless inbound_mail.date.utc >= channel.imap_inbox_synced_at
Mail.find(what: :last, count: 10, order: :asc).each do |inbound_mail|
next if channel.inbox.messages.find_by(source_id: inbound_mail.message_id).present?
process_mail(inbound_mail, channel)
new_mails = true
end
channel.update(imap_inbox_synced_at: Time.now.utc) if new_mails
end
def process_mail(inbound_mail, channel)

View File

@@ -16,6 +16,6 @@ trigger_scheduled_items_job:
# executed At every 5th minute..
trigger_imap_email_inboxes_job:
cron: '*/5 * * * *'
cron: '*/1 * * * *'
class: 'Inboxes::FetchImapEmailInboxesJob'
queue: scheduled_jobs

View File

@@ -4,34 +4,52 @@ RSpec.describe Inboxes::FetchImapEmailsJob, type: :job do
let(:account) { create(:account) }
let(:imap_email_channel) do
create(:channel_email, imap_enabled: true, imap_address: 'imap.gmail.com', imap_port: 993, imap_login: 'imap@gmail.com',
imap_password: 'password', imap_inbox_synced_at: Time.now.utc - 10, account: account)
imap_password: 'password', imap_inbox_synced_at: Time.now.utc, account: account)
end
let(:email_inbox) { create(:inbox, channel: imap_email_channel, account: account) }
let!(:conversation) { create(:conversation, inbox: imap_email_channel.inbox, account: account) }
it 'enqueues the job' do
expect { described_class.perform_later }.to have_enqueued_job(described_class)
.on_queue('low')
end
context 'when imap fetch latest 10 emails' do
it 'check for the new emails' do
mail_date = Time.now.utc
mail = Mail.new do
context 'when imap fetch new emails' do
it 'process the email' do
email = Mail.new do
to 'test@outlook.com'
from 'test@gmail.com'
subject :test.to_s
body 'hello'
date mail_date
end
allow(Mail).to receive(:find).and_return([mail])
allow(Mail).to receive(:find).and_return([email])
imap_mailbox = double
allow(Imap::ImapMailbox).to receive(:new).and_return(imap_mailbox)
expect(imap_mailbox).to receive(:process).with(mail, imap_email_channel).once
expect(imap_mailbox).to receive(:process).with(email, imap_email_channel).once
described_class.perform_now(imap_email_channel)
end
end
expect(imap_email_channel.reload.imap_inbox_synced_at).to be > mail_date
context 'when imap fetch existing emails' do
it 'does not process the email' do
email = Mail.new do
to 'test@outlook.com'
from 'test@gmail.com'
subject :test.to_s
body 'hello'
message_id '<messageId@example.com>'
end
create(:message, message_type: 'incoming', source_id: email.message_id, account: account, inbox: imap_email_channel.inbox,
conversation: conversation)
allow(Mail).to receive(:find).and_return([email])
imap_mailbox = double
allow(Imap::ImapMailbox).to receive(:new).and_return(imap_mailbox)
expect(imap_mailbox).not_to receive(:process).with(email, imap_email_channel)
described_class.perform_now(imap_email_channel)
end
end
end