mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Merge pull request #42634 from HackerIntro/actionmailbox-storage-service
Problem: ActionMailbox uses default ActiveStorage service
This commit is contained in:
commit
f263530bf7
9 changed files with 84 additions and 2 deletions
|
@ -1,3 +1,19 @@
|
|||
* Add ability to configure ActiveStorage service
|
||||
for storing email raw source.
|
||||
|
||||
```yml
|
||||
# config/storage.yml
|
||||
incoming_emails:
|
||||
service: Disk
|
||||
root: /secure/dir/for/emails/only
|
||||
```
|
||||
|
||||
```ruby
|
||||
config.action_mailbox.storage_service = :incoming_emails
|
||||
```
|
||||
|
||||
*Yurii Rashkovskii*
|
||||
|
||||
* Add ability to incinerate an inbound message through the conductor interface.
|
||||
|
||||
*Santiago Bartesaghi*
|
||||
|
|
|
@ -29,7 +29,7 @@ module ActionMailbox
|
|||
|
||||
include Incineratable, MessageId, Routable
|
||||
|
||||
has_one_attached :raw_email
|
||||
has_one_attached :raw_email, service: ActionMailbox.storage_service
|
||||
enum status: %i[ pending processing delivered failed bounced ]
|
||||
|
||||
def mail
|
||||
|
|
|
@ -35,7 +35,8 @@ module ActionMailbox::InboundEmail::MessageId
|
|||
end
|
||||
|
||||
def create_and_upload_raw_email!(source)
|
||||
ActiveStorage::Blob.create_and_upload! io: StringIO.new(source), filename: "message.eml", content_type: "message/rfc822"
|
||||
ActiveStorage::Blob.create_and_upload! io: StringIO.new(source), filename: "message.eml", content_type: "message/rfc822",
|
||||
service_name: ActionMailbox.storage_service
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -14,4 +14,5 @@ module ActionMailbox
|
|||
mattr_accessor :incinerate, default: true
|
||||
mattr_accessor :incinerate_after, default: 30.days
|
||||
mattr_accessor :queues, default: {}
|
||||
mattr_accessor :storage_service
|
||||
end
|
||||
|
|
|
@ -20,6 +20,8 @@ module ActionMailbox
|
|||
config.action_mailbox.queues = ActiveSupport::InheritableOptions.new \
|
||||
incineration: :action_mailbox_incineration, routing: :action_mailbox_routing
|
||||
|
||||
config.action_mailbox.storage_service = nil
|
||||
|
||||
initializer "action_mailbox.config" do
|
||||
config.after_initialize do |app|
|
||||
ActionMailbox.logger = app.config.action_mailbox.logger || Rails.logger
|
||||
|
@ -27,6 +29,7 @@ module ActionMailbox
|
|||
ActionMailbox.incinerate_after = app.config.action_mailbox.incinerate_after || 30.days
|
||||
ActionMailbox.queues = app.config.action_mailbox.queues || {}
|
||||
ActionMailbox.ingress = app.config.action_mailbox.ingress
|
||||
ActionMailbox.storage_service = app.config.action_mailbox.storage_service
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -6,6 +6,10 @@ local:
|
|||
service: Disk
|
||||
root: <%= Rails.root.join("storage") %>
|
||||
|
||||
test_email:
|
||||
service: Disk
|
||||
root: <%= Rails.root.join("tmp/storage_email") %>
|
||||
|
||||
# Use bin/rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key)
|
||||
# amazon:
|
||||
# service: S3
|
||||
|
|
|
@ -44,5 +44,41 @@ module ActionMailbox
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
test "email gets saved to the configured storage service" do
|
||||
ActionMailbox.storage_service = :test_email
|
||||
|
||||
assert_equal(:test_email, ActionMailbox.storage_service)
|
||||
|
||||
email = create_inbound_email_from_fixture("welcome.eml")
|
||||
|
||||
storage_service = ActiveStorage::Blob.services.fetch(ActionMailbox.storage_service)
|
||||
raw = email.raw_email_blob
|
||||
|
||||
# Not present in the main storage
|
||||
assert_not(ActiveStorage::Blob.service.exist?(raw.key))
|
||||
# Present in the email storage
|
||||
assert(storage_service.exist?(raw.key))
|
||||
ensure
|
||||
ActionMailbox.storage_service = nil
|
||||
end
|
||||
|
||||
test "email gets saved to the default storage service, even if it gets changed" do
|
||||
default_service = ActiveStorage::Blob.service
|
||||
ActiveStorage::Blob.service = ActiveStorage::Blob.services.fetch(:test_email)
|
||||
|
||||
# Doesn't change ActionMailbox.storage_service
|
||||
assert_nil(ActionMailbox.storage_service)
|
||||
|
||||
email = create_inbound_email_from_fixture("welcome.eml")
|
||||
raw = email.raw_email_blob
|
||||
|
||||
# Not present in the (previously) default storage
|
||||
assert_not(default_service.exist?(raw.key))
|
||||
# Present in the current default storage (email)
|
||||
assert(ActiveStorage::Blob.service.exist?(raw.key))
|
||||
ensure
|
||||
ActiveStorage::Blob.service = default_service
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -785,6 +785,8 @@ Defaults to `'signed cookie'`.
|
|||
|
||||
* `config.action_mailbox.queues.routing` accepts a symbol indicating the Active Job queue to use for routing jobs. When this option is `nil`, routing jobs are sent to the default Active Job queue (see `config.active_job.default_queue_name`).
|
||||
|
||||
* `config.action_mailbox.storage_service` accepts a symbol indicating the Active Storage service to use for uploading emails. When this option is `nil`, emails are uploaded to the default Active Storage service (see `config.active_storage.service`).
|
||||
|
||||
### Configuring Action Mailer
|
||||
|
||||
There are a number of settings available on `config.action_mailer`:
|
||||
|
|
|
@ -2946,6 +2946,25 @@ module ApplicationTests
|
|||
assert_equal :another_queue, ActionMailbox.queues[:routing]
|
||||
end
|
||||
|
||||
test "ActionMailbox.storage_service is nil by default (default service)" do
|
||||
app "development"
|
||||
assert_nil(ActionMailbox.storage_service)
|
||||
end
|
||||
|
||||
test "ActionMailbox.storage_service can be configured" do
|
||||
add_to_config <<-RUBY
|
||||
config.active_storage.service_configurations = {
|
||||
email: {
|
||||
root: "#{Dir.tmpdir}/email",
|
||||
service: "Disk"
|
||||
}
|
||||
}
|
||||
config.action_mailbox.storage_service = :email
|
||||
RUBY
|
||||
app "development"
|
||||
assert_equal(:email, ActionMailbox.storage_service)
|
||||
end
|
||||
|
||||
test "ActionMailer::Base.delivery_job is ActionMailer::MailDeliveryJob by default" do
|
||||
app "development"
|
||||
|
||||
|
|
Loading…
Reference in a new issue