From 482cbe7fcc649d8fc4fad10ded72984642b67914 Mon Sep 17 00:00:00 2001 From: Steffen van Bergerem Date: Sun, 7 Feb 2016 16:21:36 +0100 Subject: [PATCH] Add reason for post report to email sent to admins --- app/mailers/report_mailer.rb | 14 ++++++++------ app/models/report.rb | 2 +- app/views/report/report_email.markerb | 7 +++++-- app/workers/mail/report_worker.rb | 4 ++-- config/locales/diaspora/en.yml | 2 ++ spec/mailers/report_spec.rb | 23 +++++++++++++++++------ 6 files changed, 35 insertions(+), 17 deletions(-) diff --git a/app/mailers/report_mailer.rb b/app/mailers/report_mailer.rb index a89468b8b..dbb3c2f12 100644 --- a/app/mailers/report_mailer.rb +++ b/app/mailers/report_mailer.rb @@ -1,15 +1,17 @@ class ReportMailer < ActionMailer::Base default from: AppConfig.mail.sender_address - def self.new_report(type, id) - Role.moderators.map {|role| super(type, id, role) } + def self.new_report(report_id) + report = Report.find_by_id(report_id) + Role.moderators.map {|role| super(report.item_type, report.item_id, report.text, role) } end - def new_report(type, id, role) + def new_report(type, id, reason, role) resource = { - url: report_index_url, - type: I18n.t("notifier.report_email.type.#{type.downcase}"), - id: id + url: report_index_url, + type: I18n.t("notifier.report_email.type.#{type.downcase}"), + id: id, + reason: reason } person = Person.find(role.person_id) return unless person.local? diff --git a/app/models/report.rb b/app/models/report.rb index 0ecaea927..a116e9083 100644 --- a/app/models/report.rb +++ b/app/models/report.rb @@ -56,6 +56,6 @@ class Report < ActiveRecord::Base end def send_report_notification - Workers::Mail::ReportWorker.perform_async(self.item_type, self.item_id) + Workers::Mail::ReportWorker.perform_async(id) end end diff --git a/app/views/report/report_email.markerb b/app/views/report/report_email.markerb index ba787d56a..2729aa96b 100644 --- a/app/views/report/report_email.markerb +++ b/app/views/report/report_email.markerb @@ -1,2 +1,5 @@ -<%= t('notifier.report_email.body', url: resource[:url], type: resource[:type], id: resource[:id]) %> - +<%= t("notifier.report_email.body", + url: resource[:url], + type: resource[:type], + id: resource[:id], + reason: resource[:reason]) %> diff --git a/app/workers/mail/report_worker.rb b/app/workers/mail/report_worker.rb index ede0f0867..2d34787ae 100644 --- a/app/workers/mail/report_worker.rb +++ b/app/workers/mail/report_worker.rb @@ -3,8 +3,8 @@ module Workers class ReportWorker < Base sidekiq_options queue: :mail - def perform(type, id) - ReportMailer.new_report(type, id).each(&:deliver_now) + def perform(report_id) + ReportMailer.new_report(report_id).each(&:deliver_now) end end end diff --git a/config/locales/diaspora/en.yml b/config/locales/diaspora/en.yml index cbad80c43..0c45c38e4 100644 --- a/config/locales/diaspora/en.yml +++ b/config/locales/diaspora/en.yml @@ -793,6 +793,8 @@ en: the %{type} with ID %{id} was marked as offensive. + Reason: %{reason} + [%{url}][1] Please review as soon as possible! diff --git a/spec/mailers/report_spec.rb b/spec/mailers/report_spec.rb index 04869e5d6..5ecb66923 100644 --- a/spec/mailers/report_spec.rb +++ b/spec/mailers/report_spec.rb @@ -7,6 +7,17 @@ require "spec_helper" describe Report, type: :mailer do describe "#make_notification" do before do + @reported_user = bob + @post = @reported_user.post(:status_message, text: "hello", to: @reported_user.aspects.first.id) + @comment = @reported_user.comment!(@post, "welcome") + @post_report = @reported_user.reports.create( + item_id: @post.id, item_type: "Post", + text: "offensive content" + ) + @comment_report = @reported_user.reports.create( + item_id: @comment.id, item_type: "Comment", + text: "offensive comment" + ) @remote = FactoryGirl.create(:person, diaspora_handle: "remote@remote.net") @user = FactoryGirl.create(:user_with_aspect, username: "local", language: "de") @user2 = FactoryGirl.create(:user_with_aspect, username: "locally") @@ -16,35 +27,35 @@ describe Report, type: :mailer do it "should deliver successfully" do expect { - ReportMailer.new_report("post", 666).each(&:deliver_now) + ReportMailer.new_report(@post_report.id).each(&:deliver_now) }.to_not raise_error end it "should be added to the delivery queue" do expect { - ReportMailer.new_report("post", 666).each(&:deliver_now) + ReportMailer.new_report(@post_report.id).each(&:deliver_now) }.to change(ActionMailer::Base.deliveries, :size).by(2) end it "should include correct recipient" do - ReportMailer.new_report("post", 666).each(&:deliver_now) + ReportMailer.new_report(@post_report.id).each(&:deliver_now) expect(ActionMailer::Base.deliveries[0].to[0]).to include(@user.email) expect(ActionMailer::Base.deliveries[1].to[0]).to include(@user2.email) end it "should send mail in recipent's prefered language" do - ReportMailer.new_report("post", 666).each(&:deliver_now) + ReportMailer.new_report(@post_report.id).each(&:deliver_now) expect(ActionMailer::Base.deliveries[0].subject).to match("Ein neuer post wurde als anstößig markiert") expect(ActionMailer::Base.deliveries[1].subject).to match("A new post was marked as offensive") end it "should find correct post translation" do - ReportMailer.new_report("PosT", 666).each(&:deliver_now) + ReportMailer.new_report(@post_report.id).each(&:deliver_now) expect(ActionMailer::Base.deliveries[0].subject).not_to match("translation missing") end it "should find correct comment translation" do - ReportMailer.new_report("CoMMenT", 666).each(&:deliver_now) + ReportMailer.new_report(@comment_report.id).each(&:deliver_now) expect(ActionMailer::Base.deliveries[0].subject).not_to match("translation missing") end end