Admin mailer parameterization (#25759)
This commit is contained in:
		
							parent
							
								
									e75e734ac2
								
							
						
					
					
						commit
						6555c33503
					
				
					 10 changed files with 40 additions and 36 deletions
				
			
		| 
						 | 
				
			
			@ -6,45 +6,52 @@ class AdminMailer < ApplicationMailer
 | 
			
		|||
  helper :accounts
 | 
			
		||||
  helper :languages
 | 
			
		||||
 | 
			
		||||
  def new_report(recipient, report)
 | 
			
		||||
    @report   = report
 | 
			
		||||
    @me       = recipient
 | 
			
		||||
    @instance = Rails.configuration.x.local_domain
 | 
			
		||||
  before_action :process_params
 | 
			
		||||
  before_action :set_instance
 | 
			
		||||
 | 
			
		||||
  default to: -> { @me.user_email }
 | 
			
		||||
 | 
			
		||||
  def new_report(report)
 | 
			
		||||
    @report = report
 | 
			
		||||
 | 
			
		||||
    locale_for_account(@me) do
 | 
			
		||||
      mail to: @me.user_email, subject: I18n.t('admin_mailer.new_report.subject', instance: @instance, id: @report.id)
 | 
			
		||||
      mail subject: default_i18n_subject(instance: @instance, id: @report.id)
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def new_appeal(recipient, appeal)
 | 
			
		||||
    @appeal   = appeal
 | 
			
		||||
    @me       = recipient
 | 
			
		||||
    @instance = Rails.configuration.x.local_domain
 | 
			
		||||
  def new_appeal(appeal)
 | 
			
		||||
    @appeal = appeal
 | 
			
		||||
 | 
			
		||||
    locale_for_account(@me) do
 | 
			
		||||
      mail to: @me.user_email, subject: I18n.t('admin_mailer.new_appeal.subject', instance: @instance, username: @appeal.account.username)
 | 
			
		||||
      mail subject: default_i18n_subject(instance: @instance, username: @appeal.account.username)
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def new_pending_account(recipient, user)
 | 
			
		||||
    @account  = user.account
 | 
			
		||||
    @me       = recipient
 | 
			
		||||
    @instance = Rails.configuration.x.local_domain
 | 
			
		||||
  def new_pending_account(user)
 | 
			
		||||
    @account = user.account
 | 
			
		||||
 | 
			
		||||
    locale_for_account(@me) do
 | 
			
		||||
      mail to: @me.user_email, subject: I18n.t('admin_mailer.new_pending_account.subject', instance: @instance, username: @account.username)
 | 
			
		||||
      mail subject: default_i18n_subject(instance: @instance, username: @account.username)
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def new_trends(recipient, links, tags, statuses)
 | 
			
		||||
  def new_trends(links, tags, statuses)
 | 
			
		||||
    @links                  = links
 | 
			
		||||
    @tags                   = tags
 | 
			
		||||
    @statuses               = statuses
 | 
			
		||||
    @me                     = recipient
 | 
			
		||||
    @instance               = Rails.configuration.x.local_domain
 | 
			
		||||
 | 
			
		||||
    locale_for_account(@me) do
 | 
			
		||||
      mail to: @me.user_email, subject: I18n.t('admin_mailer.new_trends.subject', instance: @instance)
 | 
			
		||||
      mail subject: default_i18n_subject(instance: @instance)
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  private
 | 
			
		||||
 | 
			
		||||
  def process_params
 | 
			
		||||
    @me = params[:recipient]
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def set_instance
 | 
			
		||||
    @instance = Rails.configuration.x.local_domain
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -35,7 +35,7 @@ module Trends
 | 
			
		|||
    return if links_requiring_review.empty? && tags_requiring_review.empty? && statuses_requiring_review.empty?
 | 
			
		||||
 | 
			
		||||
    User.those_who_can(:manage_taxonomies).includes(:account).find_each do |user|
 | 
			
		||||
      AdminMailer.new_trends(user.account, links_requiring_review, tags_requiring_review, statuses_requiring_review).deliver_later! if user.allows_trends_review_emails?
 | 
			
		||||
      AdminMailer.with(recipient: user.account).new_trends(links_requiring_review, tags_requiring_review, statuses_requiring_review).deliver_later! if user.allows_trends_review_emails?
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -475,7 +475,7 @@ class User < ApplicationRecord
 | 
			
		|||
    User.those_who_can(:manage_users).includes(:account).find_each do |u|
 | 
			
		||||
      next unless u.allows_pending_account_emails?
 | 
			
		||||
 | 
			
		||||
      AdminMailer.new_pending_account(u.account, self).deliver_later
 | 
			
		||||
      AdminMailer.with(recipient: u.account).new_pending_account(self).deliver_later
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -23,7 +23,7 @@ class AppealService < BaseService
 | 
			
		|||
 | 
			
		||||
  def notify_staff!
 | 
			
		||||
    User.those_who_can(:manage_appeals).includes(:account).each do |u|
 | 
			
		||||
      AdminMailer.new_appeal(u.account, @appeal).deliver_later if u.allows_appeal_emails?
 | 
			
		||||
      AdminMailer.with(recipient: u.account).new_appeal(@appeal).deliver_later if u.allows_appeal_emails?
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -40,7 +40,7 @@ class ReportService < BaseService
 | 
			
		|||
 | 
			
		||||
    User.those_who_can(:manage_reports).includes(:account).each do |u|
 | 
			
		||||
      LocalNotificationWorker.perform_async(u.account_id, @report.id, 'Report', 'admin.report')
 | 
			
		||||
      AdminMailer.new_report(u.account, @report).deliver_later if u.allows_report_emails?
 | 
			
		||||
      AdminMailer.with(recipient: u.account).new_report(@report).deliver_later if u.allows_report_emails?
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -63,6 +63,7 @@ ignore_unused:
 | 
			
		|||
  - 'admin_mailer.new_appeal.actions.*'
 | 
			
		||||
  - 'statuses.attached.*'
 | 
			
		||||
  - 'move_handler.carry_{mutes,blocks}_over_text'
 | 
			
		||||
  - 'admin_mailer.*.subject'
 | 
			
		||||
  - 'notification_mailer.*'
 | 
			
		||||
  - 'imports.overwrite_preambles.{following,blocking,muting,domain_blocking,bookmarks}_html'
 | 
			
		||||
  - 'imports.preambles.{following,blocking,muting,domain_blocking,bookmarks}_html'
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -23,8 +23,6 @@ RSpec.describe Api::V1::ReportsController do
 | 
			
		|||
    let(:rule_ids) { nil }
 | 
			
		||||
 | 
			
		||||
    before do
 | 
			
		||||
      allow(AdminMailer).to receive(:new_report)
 | 
			
		||||
        .and_return(instance_double(ActionMailer::MessageDelivery, deliver_later: nil))
 | 
			
		||||
      post :create, params: { status_ids: [status.id], account_id: target_account.id, comment: 'reasons', category: category, rule_ids: rule_ids, forward: forward }
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -41,7 +39,7 @@ RSpec.describe Api::V1::ReportsController do
 | 
			
		|||
    end
 | 
			
		||||
 | 
			
		||||
    it 'sends e-mails to admins' do
 | 
			
		||||
      expect(AdminMailer).to have_received(:new_report).with(admin.account, Report)
 | 
			
		||||
      expect(ActionMailer::Base.deliveries.first.to).to eq([admin.email])
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    context 'when a status does not belong to the reported account' do
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -14,13 +14,11 @@ RSpec.describe Disputes::AppealsController do
 | 
			
		|||
    let(:strike) { Fabricate(:account_warning, target_account: current_user.account) }
 | 
			
		||||
 | 
			
		||||
    before do
 | 
			
		||||
      allow(AdminMailer).to receive(:new_appeal)
 | 
			
		||||
        .and_return(instance_double(ActionMailer::MessageDelivery, deliver_later: nil))
 | 
			
		||||
      post :create, params: { strike_id: strike.id, appeal: { text: 'Foo' } }
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it 'notifies staff about new appeal' do
 | 
			
		||||
      expect(AdminMailer).to have_received(:new_appeal).with(admin.account, Appeal.last)
 | 
			
		||||
      expect(ActionMailer::Base.deliveries.first.to).to eq([admin.email])
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    it 'redirects back to the strike page' do
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -7,7 +7,7 @@ RSpec.describe AdminMailer do
 | 
			
		|||
    let(:sender)    { Fabricate(:account, username: 'John') }
 | 
			
		||||
    let(:recipient) { Fabricate(:account, username: 'Mike') }
 | 
			
		||||
    let(:report)    { Fabricate(:report, account: sender, target_account: recipient) }
 | 
			
		||||
    let(:mail)      { described_class.new_report(recipient, report) }
 | 
			
		||||
    let(:mail)      { described_class.with(recipient: recipient).new_report(report) }
 | 
			
		||||
 | 
			
		||||
    before do
 | 
			
		||||
      recipient.user.update(locale: :en)
 | 
			
		||||
| 
						 | 
				
			
			@ -27,7 +27,7 @@ RSpec.describe AdminMailer do
 | 
			
		|||
  describe '.new_appeal' do
 | 
			
		||||
    let(:appeal) { Fabricate(:appeal) }
 | 
			
		||||
    let(:recipient) { Fabricate(:account, username: 'Kurt') }
 | 
			
		||||
    let(:mail)      { described_class.new_appeal(recipient, appeal) }
 | 
			
		||||
    let(:mail)      { described_class.with(recipient: recipient).new_appeal(appeal) }
 | 
			
		||||
 | 
			
		||||
    before do
 | 
			
		||||
      recipient.user.update(locale: :en)
 | 
			
		||||
| 
						 | 
				
			
			@ -47,7 +47,7 @@ RSpec.describe AdminMailer do
 | 
			
		|||
  describe '.new_pending_account' do
 | 
			
		||||
    let(:recipient) { Fabricate(:account, username: 'Barklums') }
 | 
			
		||||
    let(:user) { Fabricate(:user) }
 | 
			
		||||
    let(:mail) { described_class.new_pending_account(recipient, user) }
 | 
			
		||||
    let(:mail) { described_class.with(recipient: recipient).new_pending_account(user) }
 | 
			
		||||
 | 
			
		||||
    before do
 | 
			
		||||
      recipient.user.update(locale: :en)
 | 
			
		||||
| 
						 | 
				
			
			@ -69,7 +69,7 @@ RSpec.describe AdminMailer do
 | 
			
		|||
    let(:links) { [] }
 | 
			
		||||
    let(:statuses) { [] }
 | 
			
		||||
    let(:tags) { [] }
 | 
			
		||||
    let(:mail) { described_class.new_trends(recipient, links, tags, statuses) }
 | 
			
		||||
    let(:mail) { described_class.with(recipient: recipient).new_trends(links, tags, statuses) }
 | 
			
		||||
 | 
			
		||||
    before do
 | 
			
		||||
      recipient.user.update(locale: :en)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -5,16 +5,16 @@
 | 
			
		|||
class AdminMailerPreview < ActionMailer::Preview
 | 
			
		||||
  # Preview this email at http://localhost:3000/rails/mailers/admin_mailer/new_pending_account
 | 
			
		||||
  def new_pending_account
 | 
			
		||||
    AdminMailer.new_pending_account(Account.first, User.pending.first)
 | 
			
		||||
    AdminMailer.with(recipient: Account.first).new_pending_account(User.pending.first)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  # Preview this email at http://localhost:3000/rails/mailers/admin_mailer/new_trends
 | 
			
		||||
  def new_trends
 | 
			
		||||
    AdminMailer.new_trends(Account.first, PreviewCard.joins(:trend).limit(3), Tag.limit(3), Status.joins(:trend).where(reblog_of_id: nil).limit(3))
 | 
			
		||||
    AdminMailer.with(recipient: Account.first).new_trends(PreviewCard.joins(:trend).limit(3), Tag.limit(3), Status.joins(:trend).where(reblog_of_id: nil).limit(3))
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  # Preview this email at http://localhost:3000/rails/mailers/admin_mailer/new_appeal
 | 
			
		||||
  def new_appeal
 | 
			
		||||
    AdminMailer.new_appeal(Account.first, Appeal.first)
 | 
			
		||||
    AdminMailer.with(recipient: Account.first).new_appeal(Appeal.first)
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in a new issue