676fe1b8eb
This commit addresses `DEPRECATION WARNING: Defining enums with keyword arguments is deprecated and will be removed` warning in Action Mailbox. * Steps to reproduce ```ruby git clone https://github.com/rails/rails cd rails/actionmailbox bundle install bin/test test/unit/router_test.rb ``` * Without this commit ``` $ bin/test test/unit/router_test.rb ... snip .. DEPRECATION WARNING: Defining enums with keyword arguments is deprecated and will be removed in Rails 7.3. Positional arguments should be used instead: enum :status, [:pending, :processing, :delivered, :failed, :bounced] (called from <class:InboundEmail> at /home/yahonda/src/github.com/rails/rails/actionmailbox/app/models/action_mailbox/inbound_email.rb:31) Run options: --seed 65254 ............... Finished in 0.230357s, 65.1163 runs/s, 108.5271 assertions/s. 15 runs, 25 assertions, 0 failures, 0 errors, 0 skips $ ``` Follow up https://github.com/rails/rails/pull/50987 Refer to https://github.com/rails/rails/pull/51037
56 lines
2.0 KiB
Ruby
56 lines
2.0 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require "mail"
|
|
|
|
module ActionMailbox
|
|
# The +InboundEmail+ is an Active Record that keeps a reference to the raw email stored in Active Storage
|
|
# and tracks the status of processing. By default, incoming emails will go through the following lifecycle:
|
|
#
|
|
# * Pending: Just received by one of the ingress controllers and scheduled for routing.
|
|
# * Processing: During active processing, while a specific mailbox is running its #process method.
|
|
# * Delivered: Successfully processed by the specific mailbox.
|
|
# * Failed: An exception was raised during the specific mailbox's execution of the +#process+ method.
|
|
# * Bounced: Rejected processing by the specific mailbox and bounced to sender.
|
|
#
|
|
# Once the +InboundEmail+ has reached the status of being either +delivered+, +failed+, or +bounced+,
|
|
# it'll count as having been +#processed?+. Once processed, the +InboundEmail+ will be scheduled for
|
|
# automatic incineration at a later point.
|
|
#
|
|
# When working with an +InboundEmail+, you'll usually interact with the parsed version of the source,
|
|
# which is available as a +Mail+ object from +#mail+. But you can also access the raw source directly
|
|
# using the +#source+ method.
|
|
#
|
|
# Examples:
|
|
#
|
|
# inbound_email.mail.from # => 'david@loudthinking.com'
|
|
# inbound_email.source # Returns the full rfc822 source of the email as text
|
|
class InboundEmail < Record
|
|
include Incineratable, MessageId, Routable
|
|
|
|
has_one_attached :raw_email, service: ActionMailbox.storage_service
|
|
enum :status, %i[ pending processing delivered failed bounced ]
|
|
|
|
def mail
|
|
@mail ||= Mail.from_source(source)
|
|
end
|
|
|
|
def source
|
|
@source ||= raw_email.download
|
|
end
|
|
|
|
def processed?
|
|
delivered? || failed? || bounced?
|
|
end
|
|
|
|
def instrumentation_payload # :nodoc:
|
|
{
|
|
id: id,
|
|
message_id: message_id,
|
|
status: status
|
|
}
|
|
end
|
|
end
|
|
end
|
|
|
|
ActiveSupport.run_load_hooks :action_mailbox_inbound_email, ActionMailbox::InboundEmail
|