Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display friendly report reason and details in moderation emails #8840

Merged
merged 2 commits into from Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion decidim-core/app/commands/decidim/create_user_report.rb
Expand Up @@ -56,7 +56,7 @@ def update_report_count!

def send_notification_to_admins!
current_organization.admins.each do |admin|
Decidim::UserReportJob.perform_later(admin, current_user, form.reason, reportable)
Decidim::UserReportJob.perform_later(admin, report)
end
end

Expand Down
4 changes: 2 additions & 2 deletions decidim-core/app/jobs/decidim/user_report_job.rb
Expand Up @@ -4,8 +4,8 @@ module Decidim
class UserReportJob < ApplicationJob
queue_as :user_report

def perform(admin, token, reason, user)
UserReportMailer.notify(admin, token, reason, user).deliver_now
def perform(admin, report)
UserReportMailer.notify(admin, report).deliver_now
end
end
end
13 changes: 5 additions & 8 deletions decidim-core/app/mailers/decidim/user_report_mailer.rb
Expand Up @@ -4,18 +4,15 @@ module Decidim
# A custom mailer to notify Decidim users
# that they have been reported
class UserReportMailer < ApplicationMailer
def notify(admin, token, reason, user)
@user = user
@organization = user.organization
@token = token
@reason = reason
def notify(admin, report)
@report = report
@admin = admin
@organization = report.moderation.user.organization
with_user(admin) do
mail(to: admin.email, subject: I18n.t(
"decidim.user_report_mailer.notify.subject",
organization_name: @organization.name,
reason: @reason,
token: @token
organization_name: report.moderation.user.organization.name,
reason: @report.reason
))
end
end
Expand Down
13 changes: 10 additions & 3 deletions decidim-core/app/views/decidim/user_report_mailer/notify.html.erb
@@ -1,7 +1,14 @@
<p class="email-greeting"><%= t ".hello", admin: h(@admin.name) %></p>

<p class="email-instructions"><%= t ".body_1", user: h(@user.user_name), token: h(@token.name) %></p>
<p class="email-instructions"><%= t ".body_1", user: h(@report.moderation.user.name), token: h(@report.user.name) %></p>

<p class="email-instructions"><%= t ".body_2", reason: h(@reason) %></p>
<p class="email-instructions"><%= t ".body_2", reason: t(@report.reason, organization_name: @report.moderation.user.organization.name, scope: "decidim.shared.flag_user_modal") %></p>

<p class="email-closing"><%= t(".greetings", organization_name: h(@organization.name), organization_url: decidim.root_url(host: @organization.host)).html_safe %></p>
<% if @report.details.present? %>
<p class="email-instructions"><%= t(".details") %></p>
<blockquote>
<%= @report.details %>
</blockquote>
<% end %>

<p class="email-closing"><%= t(".greetings", organization_name: h(@report.moderation.user.organization.name), organization_url: decidim.root_url(host: @report.moderation.user.organization.host)).html_safe %></p>
1 change: 1 addition & 0 deletions decidim-core/config/locales/en.yml
Expand Up @@ -1402,6 +1402,7 @@ en:
notify:
body_1: User %{user} has been reported by %{token}
body_2: 'Reason: %{reason}'
details: User provided details
greetings: Greetings,<br/>%{organization_name}<br/><a href="%{organization_url}">%{organization_url}</a>
hello: Hello %{admin},
subject: A new user has been reported in %{organization_name}
Expand Down
4 changes: 2 additions & 2 deletions decidim-core/lib/decidim/core/test/factories.rb
Expand Up @@ -713,8 +713,8 @@ def generate_localized_title

factory :user_report, class: "Decidim::UserReport" do
reason { "spam" }
moderation { build(:user_moderation) }
user { build(:user, organization: moderation.organization) }
moderation { create(:user_moderation, user: user) }
user { build(:user) }
end

factory :user_moderation, class: "Decidim::UserModeration" do
Expand Down
11 changes: 7 additions & 4 deletions decidim-core/spec/mailers/user_report_mailer_spec.rb
Expand Up @@ -7,11 +7,13 @@ module Decidim
let(:organization) { create(:organization, name: "Test Organization") }
let(:admin) { create(:user, :admin, organization: organization) }
let(:reporter) { create(:user, :confirmed, organization: organization) }
let(:moderation) { create(:user_moderation, user: user) }
let(:user_report) { create(:user_report, user: reporter, reason: reason, details: "Lorem ipsum", moderation: moderation) }
let(:user) { create(:user, :confirmed, organization: organization) }
let(:reason) { "spam" }

describe "#notify" do
let(:mail) { described_class.notify(admin, reporter, reason, user) }
let(:mail) { described_class.notify(admin, user_report) }

describe "email body" do
it "includes the reported user name" do
Expand All @@ -23,16 +25,17 @@ module Decidim
end

it "includes the reason" do
expect(email_body(mail)).to include(reason)
expect(email_body(mail)).to include(I18n.t(reason, scope: "decidim.shared.flag_user_modal"))
end

context "when the user is already blocked" do
let(:user) { create(:user, :blocked, organization: organization) }

it "includes the reported user's original name" do
# The `user.name` is set to "Blocked user"
andreslucena marked this conversation as resolved.
Show resolved Hide resolved
expect(email_body(mail)).not_to include(user.name)
expect(email_body(mail)).to include(user.user_name)
expect(email_body(mail)).to include(user.name)
expect(email_body(mail)).to include(reporter.name)
expect(email_body(mail)).to include(admin.name)
end
end
end
Expand Down