-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1902 from mysociety/excel-analyzer-new-response-n…
…otifications [ExcelAnalyzer] Patch RequestMailer.new_response
- Loading branch information
Showing
4 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
module ExcelAnalyzer | ||
module RequestMailer | ||
def new_response(info_request, incoming_message) | ||
# Don't deliver new response email if 30 minutes has elapsed | ||
return if incoming_message.sent_at < 30.minutes.ago | ||
|
||
# Wait 5 minutes if the attachments haven't been processed, I.E. | ||
# ActiveStorage has completed all the analyzing of attachments, this | ||
# includes the ExcelAnalyzer.on_hidden_metadata callback | ||
not_analyzed_attachments = incoming_message.foi_attachments. | ||
joins(:file_blob). | ||
where("active_storage_blobs.metadata::json->>'analyzed' IS NULL") | ||
|
||
# Or wait 5 minutes if there are hidden attachments, I.E. ones we've | ||
# hidden due to detecting hidden content which are waiting for the job to | ||
# run to check for personal identifiable information. | ||
hidden_attachments = incoming_message.foi_attachments.where( | ||
prominence: 'hidden', | ||
prominence_reason: ExcelAnalyzer::PROMINENCE_REASON | ||
) | ||
|
||
if not_analyzed_attachments.any? || hidden_attachments.any? | ||
::RequestMailer.new_response(info_request, incoming_message). | ||
deliver_later(wait: 5.minutes) | ||
|
||
return | ||
end | ||
|
||
# No hidden attachments, proceed as normal | ||
self.action_name = 'new_response' | ||
super | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
require_relative '../../spec_helper' | ||
|
||
RSpec.describe RequestMailer, type: :mailer do | ||
describe '#new_response' do | ||
let(:info_request) { incoming_message.info_request } | ||
let(:incoming_message) { FactoryBot.create(:incoming_message) } | ||
let(:attachment) { incoming_message.foi_attachments.first } | ||
let(:blob) { attachment.file_blob } | ||
|
||
context 'when the email is older than 30 minutes' do | ||
before do | ||
allow(incoming_message).to receive(:sent_at).and_return(31.minutes.ago) | ||
end | ||
|
||
it 'does not deliver the email' do | ||
mail = subject.new_response(info_request, incoming_message) | ||
expect(mail).to be_nil | ||
end | ||
end | ||
|
||
context 'when the attachments have not been analyzed' do | ||
before do | ||
blob.update(metadata: nil) | ||
allow(incoming_message).to receive(:sent_at).and_return(Time.current) | ||
end | ||
|
||
it 'retrys the email after a delay' do | ||
expect { subject.new_response(info_request, incoming_message) }.to( | ||
have_enqueued_mail(described_class, :new_response). | ||
with(info_request, incoming_message) | ||
) | ||
end | ||
|
||
it 'does not deliver the email' do | ||
mail = subject.new_response(info_request, incoming_message) | ||
expect(mail).to be_nil | ||
end | ||
end | ||
|
||
context 'when there are hidden attachments' do | ||
let!(:attachment) do | ||
FactoryBot.create( | ||
:foi_attachment, | ||
incoming_message: incoming_message, | ||
prominence: 'hidden', | ||
prominence_reason: ExcelAnalyzer::PROMINENCE_REASON | ||
) | ||
end | ||
|
||
before do | ||
blob.update(metadata: { analyzed: true }) | ||
allow(incoming_message).to receive(:sent_at).and_return(Time.current) | ||
end | ||
|
||
it 'retrys the email after a delay' do | ||
expect { subject.new_response(info_request, incoming_message) }.to( | ||
have_enqueued_mail(described_class, :new_response). | ||
with(info_request, incoming_message) | ||
) | ||
end | ||
|
||
it 'does not deliver the email' do | ||
mail = subject.new_response(info_request, incoming_message) | ||
expect(mail).to be_nil | ||
end | ||
end | ||
|
||
context 'when there are no hidden attachments and the email is recent' do | ||
before do | ||
blob.update(metadata: { analyzed: true }) | ||
allow(incoming_message).to receive(:sent_at).and_return(Time.current) | ||
end | ||
|
||
it 'delivers the email immediately' do | ||
mail = subject.new_response(info_request, incoming_message) | ||
expect(mail).to be_a(Mail::Message) | ||
end | ||
end | ||
end | ||
end |