Skip to content

Commit

Permalink
Merge pull request #1902 from mysociety/excel-analyzer-new-response-n…
Browse files Browse the repository at this point in the history
…otifications

[ExcelAnalyzer] Patch RequestMailer.new_response
  • Loading branch information
gbp authored Aug 13, 2024
2 parents ef69b92 + fbb8d83 commit 82d6703
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/excel_analyzer/jobs/republish_or_report_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class RepublishOrReportJob < ApplicationJob
queue_as :excel_analyzer

attr_reader :attachment_blob
delegate :info_request, :incoming_message, to: :attachment

def perform(attachment_blob)
@attachment_blob = attachment_blob
Expand Down
34 changes: 34 additions & 0 deletions lib/excel_analyzer/mailers/request_mailer.rb
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
4 changes: 4 additions & 0 deletions lib/mailer_patches.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@
prepend VolunteerContactForm::MailerMethods
prepend DataBreach::MailerMethods
end

RequestMailer.class_eval do
prepend ExcelAnalyzer::RequestMailer
end
end
80 changes: 80 additions & 0 deletions spec/excel_analyzer/mailers/request_mailer_spec.rb
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

0 comments on commit 82d6703

Please sign in to comment.