Add optional bulk mailer settings (#35203)
This commit is contained in:
		
							
								
								
									
										18
									
								
								app/mailers/concerns/bulk_mail_settings_concern.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								app/mailers/concerns/bulk_mail_settings_concern.rb
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,18 @@
 | 
				
			|||||||
 | 
					# frozen_string_literal: true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					module BulkMailSettingsConcern
 | 
				
			||||||
 | 
					  include ActiveSupport::Concern
 | 
				
			||||||
 | 
					  include Mastodon::EmailConfigurationHelper
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  private
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  def use_bulk_mail_delivery_settings
 | 
				
			||||||
 | 
					    return if bulk_mail_configuration&.dig(:smtp_settings, :address).blank?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    mail.delivery_method.settings = convert_smtp_settings(bulk_mail_configuration[:smtp_settings])
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  def bulk_mail_configuration
 | 
				
			||||||
 | 
					    Rails.configuration.x.email&.bulk_mail
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					end
 | 
				
			||||||
@@ -1,6 +1,8 @@
 | 
				
			|||||||
# frozen_string_literal: true
 | 
					# frozen_string_literal: true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class UserMailer < Devise::Mailer
 | 
					class UserMailer < Devise::Mailer
 | 
				
			||||||
 | 
					  include BulkMailSettingsConcern
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  layout 'mailer'
 | 
					  layout 'mailer'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  helper :accounts
 | 
					  helper :accounts
 | 
				
			||||||
@@ -12,6 +14,8 @@ class UserMailer < Devise::Mailer
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  before_action :set_instance
 | 
					  before_action :set_instance
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  after_action :use_bulk_mail_delivery_settings, only: [:announcement_published, :terms_of_service_changed]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  default to: -> { @resource.email }
 | 
					  default to: -> { @resource.email }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def confirmation_instructions(user, token, *, **)
 | 
					  def confirmation_instructions(user, token, *, **)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,7 +2,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
class Admin::DistributeAnnouncementNotificationWorker
 | 
					class Admin::DistributeAnnouncementNotificationWorker
 | 
				
			||||||
  include Sidekiq::IterableJob
 | 
					  include Sidekiq::IterableJob
 | 
				
			||||||
  include BulkMailer
 | 
					  include BulkMailingConcern
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def build_enumerator(announcement_id, cursor:)
 | 
					  def build_enumerator(announcement_id, cursor:)
 | 
				
			||||||
    @announcement = Announcement.find(announcement_id)
 | 
					    @announcement = Announcement.find(announcement_id)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,7 +2,7 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
class Admin::DistributeTermsOfServiceNotificationWorker
 | 
					class Admin::DistributeTermsOfServiceNotificationWorker
 | 
				
			||||||
  include Sidekiq::IterableJob
 | 
					  include Sidekiq::IterableJob
 | 
				
			||||||
  include BulkMailer
 | 
					  include BulkMailingConcern
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  def build_enumerator(terms_of_service_id, cursor:)
 | 
					  def build_enumerator(terms_of_service_id, cursor:)
 | 
				
			||||||
    @terms_of_service = TermsOfService.find(terms_of_service_id)
 | 
					    @terms_of_service = TermsOfService.find(terms_of_service_id)
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,6 @@
 | 
				
			|||||||
# frozen_string_literal: true
 | 
					# frozen_string_literal: true
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module BulkMailer
 | 
					module BulkMailingConcern
 | 
				
			||||||
  def push_bulk_mailer(mailer_class, mailer_method, args_array)
 | 
					  def push_bulk_mailer(mailer_class, mailer_method, args_array)
 | 
				
			||||||
    raise ArgumentError, "No method #{mailer_method} on class #{mailer_class.name}" unless mailer_class.respond_to?(mailer_method)
 | 
					    raise ArgumentError, "No method #{mailer_method} on class #{mailer_class.name}" unless mailer_class.respond_to?(mailer_method)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -19,3 +19,18 @@ production:
 | 
				
			|||||||
    tls: <%= ENV.fetch('SMTP_TLS', false) == 'true' ? true : nil %>
 | 
					    tls: <%= ENV.fetch('SMTP_TLS', false) == 'true' ? true : nil %>
 | 
				
			||||||
    ssl: <%= ENV.fetch('SMTP_SSL', false) == 'true' ? true : nil %>
 | 
					    ssl: <%= ENV.fetch('SMTP_SSL', false) == 'true' ? true : nil %>
 | 
				
			||||||
    read_timeout: 20
 | 
					    read_timeout: 20
 | 
				
			||||||
 | 
					  bulk_mail:
 | 
				
			||||||
 | 
					    smtp_settings:
 | 
				
			||||||
 | 
					      port: <%= ENV.fetch('BULK_SMTP_PORT', nil) %>
 | 
				
			||||||
 | 
					      address: <%= ENV.fetch('BULK_SMTP_SERVER', nil) %>
 | 
				
			||||||
 | 
					      user_name: <%= ENV.fetch('BULK_SMTP_LOGIN', nil) %>
 | 
				
			||||||
 | 
					      password: <%= ENV.fetch('BULK_SMTP_PASSWORD', nil) %>
 | 
				
			||||||
 | 
					      domain: <%= ENV.fetch('BULK_SMTP_DOMAIN', ENV.fetch('LOCAL_DOMAIN', nil)) %>
 | 
				
			||||||
 | 
					      authentication: <%= ENV.fetch('BULK_SMTP_AUTH_METHOD', 'plain') %>
 | 
				
			||||||
 | 
					      ca_file: <%= ENV.fetch('BULK_SMTP_CA_FILE', '/etc/ssl/certs/ca-certificates.crt') %>
 | 
				
			||||||
 | 
					      openssl_verify_mode: <%= ENV.fetch('BULK_SMTP_OPENSSL_VERIFY_MODE', nil) %>
 | 
				
			||||||
 | 
					      enable_starttls: <%= ENV.fetch('BULK_SMTP_ENABLE_STARTTLS', nil) %>
 | 
				
			||||||
 | 
					      enable_starttls_auto: <%= ENV.fetch('BULK_SMTP_ENABLE_STARTTLS_AUTO', true) != 'false' %>
 | 
				
			||||||
 | 
					      tls: <%= ENV.fetch('BULK_SMTP_TLS', false) == 'true' ? true : nil %>
 | 
				
			||||||
 | 
					      ssl: <%= ENV.fetch('BULK_SMTP_SSL', false) == 'true' ? true : nil %>
 | 
				
			||||||
 | 
					      read_timeout: 20
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -110,7 +110,7 @@ Rails.application.configure do
 | 
				
			|||||||
  config.action_mailer.default_options[:reply_to]    = config.x.email.reply_to if config.x.email.reply_to.present?
 | 
					  config.action_mailer.default_options[:reply_to]    = config.x.email.reply_to if config.x.email.reply_to.present?
 | 
				
			||||||
  config.action_mailer.default_options[:return_path] = config.x.email.return_path if config.x.email.return_path.present?
 | 
					  config.action_mailer.default_options[:return_path] = config.x.email.return_path if config.x.email.return_path.present?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  config.action_mailer.smtp_settings = Mastodon::EmailConfigurationHelper.smtp_settings(config.x.email.smtp_settings)
 | 
					  config.action_mailer.smtp_settings = Mastodon::EmailConfigurationHelper.convert_smtp_settings(config.x.email.smtp_settings)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  config.action_mailer.delivery_method = config.x.email.delivery_method.to_sym
 | 
					  config.action_mailer.delivery_method = config.x.email.delivery_method.to_sym
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -6,7 +6,7 @@ module Mastodon
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    # Convert smtp settings from environment variables (or defaults in
 | 
					    # Convert smtp settings from environment variables (or defaults in
 | 
				
			||||||
    # `config/email.yml`) into the format that `ActionMailer` understands
 | 
					    # `config/email.yml`) into the format that `ActionMailer` understands
 | 
				
			||||||
    def smtp_settings(config)
 | 
					    def convert_smtp_settings(config)
 | 
				
			||||||
      enable_starttls = nil
 | 
					      enable_starttls = nil
 | 
				
			||||||
      enable_starttls_auto = nil
 | 
					      enable_starttls_auto = nil
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -3,10 +3,10 @@
 | 
				
			|||||||
require 'rails_helper'
 | 
					require 'rails_helper'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
RSpec.describe Mastodon::EmailConfigurationHelper do
 | 
					RSpec.describe Mastodon::EmailConfigurationHelper do
 | 
				
			||||||
  describe '#smtp_settings' do
 | 
					  describe '#convert_smtp_settings' do
 | 
				
			||||||
    subject { described_class }
 | 
					    subject { described_class }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let(:converted_settings) { subject.smtp_settings(configuration) }
 | 
					    let(:converted_settings) { subject.convert_smtp_settings(configuration) }
 | 
				
			||||||
    let(:base_configuration) do
 | 
					    let(:base_configuration) do
 | 
				
			||||||
      {
 | 
					      {
 | 
				
			||||||
        address: 'localhost',
 | 
					        address: 'localhost',
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -103,4 +103,9 @@ class UserMailerPreview < ActionMailer::Preview
 | 
				
			|||||||
  def terms_of_service_changed
 | 
					  def terms_of_service_changed
 | 
				
			||||||
    UserMailer.terms_of_service_changed(User.first, TermsOfService.live.first)
 | 
					    UserMailer.terms_of_service_changed(User.first, TermsOfService.live.first)
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  # Preview this email at http://localhost:3000/rails/mailers/user_mailer/announcement_published
 | 
				
			||||||
 | 
					  def announcement_published
 | 
				
			||||||
 | 
					    UserMailer.announcement_published(User.first, Announcement.last)
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -14,6 +14,43 @@ RSpec.describe UserMailer do
 | 
				
			|||||||
    end
 | 
					    end
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  shared_examples 'optional bulk mailer settings' do
 | 
				
			||||||
 | 
					    context 'when no optional bulk mailer settings are present' do
 | 
				
			||||||
 | 
					      it 'does not include delivery method options' do
 | 
				
			||||||
 | 
					        expect(mail.message.delivery_method.settings).to be_empty
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    context 'when optional bulk mailer settings are present' do
 | 
				
			||||||
 | 
					      let(:smtp_settings) do
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					          address: 'localhost',
 | 
				
			||||||
 | 
					          port: 25,
 | 
				
			||||||
 | 
					          authentication: 'none',
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      before do
 | 
				
			||||||
 | 
					        Rails.configuration.x.email ||= ActiveSupport::OrderedOptions.new
 | 
				
			||||||
 | 
					        Rails.configuration.x.email.update({ bulk_mail: { smtp_settings: } })
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      after do
 | 
				
			||||||
 | 
					        Rails.configuration.x.email = nil
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      it 'uses the bulk mailer settings' do
 | 
				
			||||||
 | 
					        expect(mail.message.delivery_method.settings).to eq({
 | 
				
			||||||
 | 
					          address: 'localhost',
 | 
				
			||||||
 | 
					          port: 25,
 | 
				
			||||||
 | 
					          authentication: nil,
 | 
				
			||||||
 | 
					          enable_starttls: nil,
 | 
				
			||||||
 | 
					          enable_starttls_auto: true,
 | 
				
			||||||
 | 
					        })
 | 
				
			||||||
 | 
					      end
 | 
				
			||||||
 | 
					    end
 | 
				
			||||||
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  let(:receiver) { Fabricate(:user) }
 | 
					  let(:receiver) { Fabricate(:user) }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  describe '#confirmation_instructions' do
 | 
					  describe '#confirmation_instructions' do
 | 
				
			||||||
@@ -316,6 +353,8 @@ RSpec.describe UserMailer do
 | 
				
			|||||||
        .and(have_subject(I18n.t('user_mailer.terms_of_service_changed.subject')))
 | 
					        .and(have_subject(I18n.t('user_mailer.terms_of_service_changed.subject')))
 | 
				
			||||||
        .and(have_body_text(I18n.t('user_mailer.terms_of_service_changed.changelog')))
 | 
					        .and(have_body_text(I18n.t('user_mailer.terms_of_service_changed.changelog')))
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    it_behaves_like 'optional bulk mailer settings'
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  describe '#announcement_published' do
 | 
					  describe '#announcement_published' do
 | 
				
			||||||
@@ -328,5 +367,7 @@ RSpec.describe UserMailer do
 | 
				
			|||||||
        .and(have_subject(I18n.t('user_mailer.announcement_published.subject')))
 | 
					        .and(have_subject(I18n.t('user_mailer.announcement_published.subject')))
 | 
				
			||||||
        .and(have_body_text(I18n.t('user_mailer.announcement_published.description', domain: local_domain_uri.host)))
 | 
					        .and(have_body_text(I18n.t('user_mailer.announcement_published.description', domain: local_domain_uri.host)))
 | 
				
			||||||
    end
 | 
					    end
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    it_behaves_like 'optional bulk mailer settings'
 | 
				
			||||||
  end
 | 
					  end
 | 
				
			||||||
end
 | 
					end
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user