From 73eef1770e1d8afe86a33bb3775d896bef772f0e Mon Sep 17 00:00:00 2001 From: antopalidi Date: Wed, 14 Sep 2022 16:57:08 +0200 Subject: [PATCH 1/8] send email to author when resource was hidden --- .../commands/decidim/admin/hide_resource.rb | 8 +++++ .../decidim/admin/hidden_resource_mailer.rb | 29 +++++++++++++++++++ .../notify_mail.html.erb | 9 ++++++ decidim-admin/config/locales/en.yml | 6 ++++ 4 files changed, 52 insertions(+) create mode 100644 decidim-admin/app/mailers/decidim/admin/hidden_resource_mailer.rb create mode 100644 decidim-admin/app/views/decidim/admin/hidden_resource_mailer/notify_mail.html.erb diff --git a/decidim-admin/app/commands/decidim/admin/hide_resource.rb b/decidim-admin/app/commands/decidim/admin/hide_resource.rb index 6fcbf46ff3541..db9635c919092 100644 --- a/decidim-admin/app/commands/decidim/admin/hide_resource.rb +++ b/decidim-admin/app/commands/decidim/admin/hide_resource.rb @@ -26,6 +26,8 @@ def call send_hide_notification_to_author + send_hide_email_to_author + broadcast(:ok, @reportable) end @@ -63,6 +65,12 @@ def send_hide_notification_to_author Decidim::EventsManager.publish(data) end + def send_hide_email_to_author + Decidim::Admin::HiddenResourceMailer.notify_mail( + @reportable, @author, report_reasons + ).deliver_later + end + def report_reasons @reportable.moderation.reports.pluck(:reason).uniq end diff --git a/decidim-admin/app/mailers/decidim/admin/hidden_resource_mailer.rb b/decidim-admin/app/mailers/decidim/admin/hidden_resource_mailer.rb new file mode 100644 index 0000000000000..ac4df7b0faa48 --- /dev/null +++ b/decidim-admin/app/mailers/decidim/admin/hidden_resource_mailer.rb @@ -0,0 +1,29 @@ +# frozen_string_literal: true + +module Decidim + module Admin + # A custom mailer to mail Decidim users + # that they have been hidden + class HiddenResourceMailer < Decidim::ApplicationMailer + include Decidim::TranslationsHelper + include Decidim::SanitizeHelper + include Decidim::ApplicationHelper + include Decidim::TranslatableAttributes + + helper Decidim::ResourceHelper + helper Decidim::TranslationsHelper + helper Decidim::ApplicationHelper + + def notify_mail(resource, author, reason) + @author = author + @organization = author.organization + @resource = resource + @reason = reason + mail(to: author.email, subject: I18n.t( + "decidim.admin.hidden_resource_mailer.notify_mail.subject", + organization_name: @organization.name + )) + end + end + end +end diff --git a/decidim-admin/app/views/decidim/admin/hidden_resource_mailer/notify_mail.html.erb b/decidim-admin/app/views/decidim/admin/hidden_resource_mailer/notify_mail.html.erb new file mode 100644 index 0000000000000..1db22d9653782 --- /dev/null +++ b/decidim-admin/app/views/decidim/admin/hidden_resource_mailer/notify_mail.html.erb @@ -0,0 +1,9 @@ +

<%= t ".hello", user_name: h(@author.name) %>

+ +

<%= t ".body_1", reason: h(@reason.join(", ")) %>:

+<%= translated_attribute(@resource.title) %>

+ +

+ <%= t ".body_2" %> + <%= link_to @organization.name, decidim.root_url(host: @organization.host).html_safe %> +

diff --git a/decidim-admin/config/locales/en.yml b/decidim-admin/config/locales/en.yml index b5bc998f36025..a71317ca0f01e 100644 --- a/decidim-admin/config/locales/en.yml +++ b/decidim-admin/config/locales/en.yml @@ -350,6 +350,12 @@ en: update: error: Failed to update domain whitelist success: Domain whitelist updated successfully + hidden_resource_mailer: + notify_mail: + subject: Your proposal has been hidden + hello: Hello %{user_name} + body_1: An administrator removed your proposal because it has been reported as %{reason} + body_2: If you feel that has been a mistake please contact the administrators of exports: export_as: "%{name} as %{export_format}" formats: From 0d4b55fdc42574b41a8283129200a864438c1986 Mon Sep 17 00:00:00 2001 From: antopalidi Date: Wed, 14 Sep 2022 21:10:33 +0200 Subject: [PATCH 2/8] fix: authors --- .../app/commands/decidim/admin/hide_resource.rb | 10 +++++++++- .../decidim/admin/hidden_resource_mailer.rb | 15 ++++++++------- .../hidden_resource_mailer/notify_mail.html.erb | 8 +++++--- decidim-admin/config/locales/en.yml | 2 +- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/decidim-admin/app/commands/decidim/admin/hide_resource.rb b/decidim-admin/app/commands/decidim/admin/hide_resource.rb index db9635c919092..da0fe93918b3a 100644 --- a/decidim-admin/app/commands/decidim/admin/hide_resource.rb +++ b/decidim-admin/app/commands/decidim/admin/hide_resource.rb @@ -67,10 +67,18 @@ def send_hide_notification_to_author def send_hide_email_to_author Decidim::Admin::HiddenResourceMailer.notify_mail( - @reportable, @author, report_reasons + @reportable, authors_email_of_resource, authors_name_of_resource, report_reasons ).deliver_later end + def authors_email_of_resource + @reportable.authors.pluck(:email).uniq + end + + def authors_name_of_resource + @reportable.authors.pluck(:name) + end + def report_reasons @reportable.moderation.reports.pluck(:reason).uniq end diff --git a/decidim-admin/app/mailers/decidim/admin/hidden_resource_mailer.rb b/decidim-admin/app/mailers/decidim/admin/hidden_resource_mailer.rb index ac4df7b0faa48..6ed3ee722e975 100644 --- a/decidim-admin/app/mailers/decidim/admin/hidden_resource_mailer.rb +++ b/decidim-admin/app/mailers/decidim/admin/hidden_resource_mailer.rb @@ -14,15 +14,16 @@ class HiddenResourceMailer < Decidim::ApplicationMailer helper Decidim::TranslationsHelper helper Decidim::ApplicationHelper - def notify_mail(resource, author, reason) - @author = author - @organization = author.organization + def notify_mail(resource, authors_email, authors_name, reason) + @authors_email = authors_email + @authors_name = authors_name + @organization = resource.organization @resource = resource @reason = reason - mail(to: author.email, subject: I18n.t( - "decidim.admin.hidden_resource_mailer.notify_mail.subject", - organization_name: @organization.name - )) + + mail(to: "#{authors_email.join(',')}", + subject: I18n.t("decidim.admin.hidden_resource_mailer.notify_mail.subject", + organization_name: @organization.name)) end end end diff --git a/decidim-admin/app/views/decidim/admin/hidden_resource_mailer/notify_mail.html.erb b/decidim-admin/app/views/decidim/admin/hidden_resource_mailer/notify_mail.html.erb index 1db22d9653782..66d5ef282e088 100644 --- a/decidim-admin/app/views/decidim/admin/hidden_resource_mailer/notify_mail.html.erb +++ b/decidim-admin/app/views/decidim/admin/hidden_resource_mailer/notify_mail.html.erb @@ -1,7 +1,9 @@ - + - -<%= translated_attribute(@resource.title) %>

+ - From 03e32166614a0735216e506c5f07304194338790 Mon Sep 17 00:00:00 2001 From: antopalidi Date: Thu, 15 Sep 2022 09:05:58 +0200 Subject: [PATCH 5/8] add tests --- .../spec/commands/decidim/admin/hide_resource_spec.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/decidim-admin/spec/commands/decidim/admin/hide_resource_spec.rb b/decidim-admin/spec/commands/decidim/admin/hide_resource_spec.rb index 24397838c1a38..62863187cba70 100644 --- a/decidim-admin/spec/commands/decidim/admin/hide_resource_spec.rb +++ b/decidim-admin/spec/commands/decidim/admin/hide_resource_spec.rb @@ -52,10 +52,19 @@ module Decidim::Admin context "when the resource is already hidden" do let(:moderation) { create(:moderation, reportable: reportable, report_count: 1, hidden_at: Time.current) } + let(:authors) { reportable.try(:authors) || [reportable.try(:author)] } + let(:reasons) { reportable.moderation.reports.pluck(:reason).uniq } it "broadcasts invalid" do expect { command.call }.to broadcast(:invalid) end + + it "sends email to author" do + Decidim::Admin::HiddenResourceMailer.notify_mail(reportable, authors, reasons).deliver_now + + expect(last_email.subject).to eq(I18n.t("decidim.admin.hidden_resource_mailer.notify_mail.subject")) + expect(last_email.to).to eq(authors.pluck(:email).uniq) + end end context "when the resource is not reported" do From d8d48a99227061bae6218b86df75a2462fa60909 Mon Sep 17 00:00:00 2001 From: antopalidi Date: Thu, 15 Sep 2022 11:54:05 +0200 Subject: [PATCH 6/8] fix test and change email view --- .../app/mailers/decidim/admin/hidden_resource_mailer.rb | 3 +-- .../admin/hidden_resource_mailer/notify_mail.html.erb | 7 +++++-- decidim-admin/config/locales/en.yml | 5 +++-- .../spec/commands/decidim/admin/hide_resource_spec.rb | 9 +++++---- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/decidim-admin/app/mailers/decidim/admin/hidden_resource_mailer.rb b/decidim-admin/app/mailers/decidim/admin/hidden_resource_mailer.rb index 86145e3189cbd..521a001465414 100644 --- a/decidim-admin/app/mailers/decidim/admin/hidden_resource_mailer.rb +++ b/decidim-admin/app/mailers/decidim/admin/hidden_resource_mailer.rb @@ -21,8 +21,7 @@ def notify_mail(resource, resource_authors, reason) @reason = reason mail(to: resource_authors.pluck(:email).uniq, - subject: I18n.t("decidim.admin.hidden_resource_mailer.notify_mail.subject", - organization_name: @organization.name)) + subject: I18n.t("decidim.admin.hidden_resource_mailer.notify_mail.subject")) end end end diff --git a/decidim-admin/app/views/decidim/admin/hidden_resource_mailer/notify_mail.html.erb b/decidim-admin/app/views/decidim/admin/hidden_resource_mailer/notify_mail.html.erb index 9b206422bd9c0..32ef849c04084 100644 --- a/decidim-admin/app/views/decidim/admin/hidden_resource_mailer/notify_mail.html.erb +++ b/decidim-admin/app/views/decidim/admin/hidden_resource_mailer/notify_mail.html.erb @@ -1,10 +1,13 @@ +

<%= t ".reason", reason: h(@reason.join(", ")) %>

+ +

<%= translated_attribute(@resource.title) %>

+ - +

<%= t ".reason", reason: h(@reason.join(", ")) %>

<%= translated_attribute(@resource.title) %>

From 429c2a9a8767af0929c0381e0aad779de2b66a8e Mon Sep 17 00:00:00 2001 From: antopalidi Date: Thu, 15 Sep 2022 15:18:10 +0200 Subject: [PATCH 8/8] fix test: change a variable name --- decidim-admin/spec/commands/decidim/admin/hide_resource_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/decidim-admin/spec/commands/decidim/admin/hide_resource_spec.rb b/decidim-admin/spec/commands/decidim/admin/hide_resource_spec.rb index 8c8c19c38075c..7fb2b8a092225 100644 --- a/decidim-admin/spec/commands/decidim/admin/hide_resource_spec.rb +++ b/decidim-admin/spec/commands/decidim/admin/hide_resource_spec.rb @@ -63,7 +63,7 @@ module Decidim::Admin clear_enqueued_jobs command.call expect(ActionMailer::MailDeliveryJob).to have_been_enqueued.on_queue("mailers") - queued_user, _, queued_options = ActiveJob::Arguments.deserialize(ActiveJob::Base.queue_adapter.enqueued_jobs.first[:args]).last[:args] + queued_user, _, _queued_options = ActiveJob::Arguments.deserialize(ActiveJob::Base.queue_adapter.enqueued_jobs.first[:args]).last[:args] expect(queued_user).to eq(current_user) end end