From 42b82206322c73c4a4d7ac29ca9a781ab11e7b1a Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Tue, 27 Jun 2017 00:04:00 +0200 Subject: [PATCH] Fix #1624 - Send e-mail notifications to admins about new reports (#3949) --- app/controllers/api/v1/reports_controller.rb | 3 +++ app/mailers/admin_mailer.rb | 13 +++++++++++++ app/mailers/application_mailer.rb | 8 ++++++++ app/mailers/notification_mailer.rb | 8 -------- app/views/admin_mailer/new_report.text.erb | 5 +++++ config/locales/en.yml | 8 +++++--- .../controllers/api/v1/reports_controller_spec.rb | 15 ++++++++++++--- 7 files changed, 46 insertions(+), 14 deletions(-) create mode 100644 app/mailers/admin_mailer.rb create mode 100644 app/views/admin_mailer/new_report.text.erb diff --git a/app/controllers/api/v1/reports_controller.rb b/app/controllers/api/v1/reports_controller.rb index 71df76e922..8e7070d077 100644 --- a/app/controllers/api/v1/reports_controller.rb +++ b/app/controllers/api/v1/reports_controller.rb @@ -17,6 +17,9 @@ class Api::V1::ReportsController < Api::BaseController status_ids: reported_status_ids, comment: report_params[:comment] ) + + User.admins.includes(:account).each { |u| AdminMailer.new_report(u.account, @report).deliver_later } + render :show end diff --git a/app/mailers/admin_mailer.rb b/app/mailers/admin_mailer.rb new file mode 100644 index 0000000000..fc19a6d40b --- /dev/null +++ b/app/mailers/admin_mailer.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +class AdminMailer < ApplicationMailer + def new_report(recipient, report) + @report = report + @me = recipient + @instance = Rails.configuration.x.local_domain + + locale_for_account(@me) do + mail to: @me.user_email, subject: I18n.t('admin_mailer.new_report.subject', instance: @instance, id: @report.id) + end + end +end diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index e5dbfeeda9..2e730c19b0 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -4,4 +4,12 @@ class ApplicationMailer < ActionMailer::Base default from: ENV.fetch('SMTP_FROM_ADDRESS') { 'notifications@localhost' } layout 'mailer' helper :instance + + protected + + def locale_for_account(account) + I18n.with_locale(account.user_locale || I18n.default_locale) do + yield + end + end end diff --git a/app/mailers/notification_mailer.rb b/app/mailers/notification_mailer.rb index a944db137f..12b92bf451 100644 --- a/app/mailers/notification_mailer.rb +++ b/app/mailers/notification_mailer.rb @@ -67,12 +67,4 @@ class NotificationMailer < ApplicationMailer ) end end - - private - - def locale_for_account(account) - I18n.with_locale(account.user_locale || I18n.default_locale) do - yield - end - end end diff --git a/app/views/admin_mailer/new_report.text.erb b/app/views/admin_mailer/new_report.text.erb new file mode 100644 index 0000000000..6fa744bc3d --- /dev/null +++ b/app/views/admin_mailer/new_report.text.erb @@ -0,0 +1,5 @@ +<%= display_name(@me) %>, + +<%= raw t('admin_mailer.new_report.body', target: @report.target_account.acct, reporter: @report.account.acct) %> + +<%= raw t('application_mailer.view')%> <%= admin_report_url(@report) %> diff --git a/config/locales/en.yml b/config/locales/en.yml index 9daaf53ecd..944c24c60c 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -193,6 +193,10 @@ en: title: PubSubHubbub topic: Topic title: Administration + admin_mailer: + new_report: + body: "%{reporter} has reported %{target}" + subject: New report for %{instance} (#%{id}) application_mailer: settings: 'Change e-mail preferences: %{link}' signature: Mastodon notifications from %{instance} @@ -399,9 +403,7 @@ en: manual_instructions: 'If you can''t scan the QR code and need to enter it manually, here is the plain-text secret:' recovery_codes: Backup recovery codes recovery_codes_regenerated: Recovery codes successfully regenerated - recovery_instructions_html: - If you ever lose access to your phone, you can use one of the recovery codes below to regain access to your account. Keep the recovery codes safe. - For example, you may print them and store them with other important documents. + recovery_instructions_html: If you ever lose access to your phone, you can use one of the recovery codes below to regain access to your account. Keep the recovery codes safe. For example, you may print them and store them with other important documents. setup: Set up wrong_code: The entered code was invalid! Are server time and device time correct? users: diff --git a/spec/controllers/api/v1/reports_controller_spec.rb b/spec/controllers/api/v1/reports_controller_spec.rb index 3df6cdfe74..471ea4e0bc 100644 --- a/spec/controllers/api/v1/reports_controller_spec.rb +++ b/spec/controllers/api/v1/reports_controller_spec.rb @@ -21,12 +21,21 @@ RSpec.describe Api::V1::ReportsController, type: :controller do end describe 'POST #create' do - it 'creates a report' do - status = Fabricate(:status) - post :create, params: { status_ids: [status.id], account_id: status.account.id, comment: 'reasons' } + let!(:status) { Fabricate(:status) } + let!(:admin) { Fabricate(:user, admin: true) } + before do + allow(AdminMailer).to receive(:new_report).and_return(double('email', deliver_later: nil)) + post :create, params: { status_ids: [status.id], account_id: status.account.id, comment: 'reasons' } + end + + it 'creates a report' do expect(status.reload.account.targeted_reports).not_to be_empty expect(response).to have_http_status(:success) end + + it 'sends e-mails to admins' do + expect(AdminMailer).to have_received(:new_report).with(admin.account, Report) + end end end