Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-griffen committed Jul 17, 2023
1 parent b3417b6 commit e57e1b8
Showing 1 changed file with 45 additions and 32 deletions.
77 changes: 45 additions & 32 deletions app/mailers/request_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,48 +251,61 @@ def requests_matching_email(email)
InfoRequest.matching_incoming_email(addresses.uniq)
end

def send_to_holding_pen(email, raw_email, opts)
opts[:rejected_reason] =
_("Could not identify the request from the email address")
request = InfoRequest.holding_pen_request
request.receive(email, raw_email, opts)
end

# Member function, called on the new class made in self.receive above
def receive(email, raw_email, source = :mailin)
opts = { source: source }
# Only check mail that doesn't have spam in the header
unless SpamAddress.spam?(MailHandler.get_all_addresses(email))
used_guesses = false
# Find exact matches for info requests
reply_info_requests = requests_matching_email(email)

# Find which info requests the email is for
reply_info_requests = requests_matching_email(email)

# Nothing found OR multiple different info requests, so a candidate for the
# holding pen
if reply_info_requests.empty? || reply_info_requests.count > 1
# Guess which requests this COULD be related to
# Find any guesses for info requests
guessed_info_requests = guess_email_request_address(email)
# If only one request is guessed, then this is should be automatically
# attached to that request
if guessed_info_requests.count == 1
guessed_info_requests.first.log_event(
'redeliver_incoming',
editor: 'automatic',
destination_request: guessed_info_requests.first,
)
else
opts[:rejected_reason] =
_("Could not identify the request from the email address")
request = InfoRequest.holding_pen_request

unless SpamAddress.spam?(MailHandler.get_all_addresses(email))
request.receive(email, raw_email, opts)
# If there are any guesses which aren't already in the reply_info_requests
# then add them, and set the 'used_guesses' flag to true for event logging
guessed_info_requests.each do |guess|
unless reply_info_requests.include?(guess)
reply_info_requests.append(guess)
used_guesses = true
end
return # TODO: Check structure of if/else
end
end

# Send the message to each request, to be archived with it
reply_info_requests.each do |reply_info_request|
# If environment variable STOP_DUPLICATES is set, don't send message with same id again
if ENV['STOP_DUPLICATES']
if reply_info_request.already_received?(email, raw_email)
raise "message #{ email.message_id } already received by request"
# If there is only one info request matching mail, it gets attached to the
# request to be archived with it
if reply_info_requests.count == 1 and guessed_info_requests.count < 2
reply_info_request = reply_info_requests.first

# If environment variable STOP_DUPLICATES is set, don't send message
# with same id again
if ENV['STOP_DUPLICATES']
if reply_info_request.already_received?(email, raw_email)
raise "message #{ email.message_id } already received by request"
end
end

if used_guesses
reply_info_request.log_event(
'redeliver_incoming',
editor: 'automatic',
destination_request: reply_info_request,
)
end
end

reply_info_request.receive(email, raw_email, opts)
reply_info_request.receive(email, raw_email, opts)

else
# Otherwise, if there are no matching IRs, multiple IRs, or multiple IR
# guesses, we send the mail to the holding pen
send_to_holding_pen(email, raw_email, opts)
end
end
end

Expand Down

0 comments on commit e57e1b8

Please sign in to comment.