From 0af765a3bdc28e405a8027d98f79ec1f8ce3349f Mon Sep 17 00:00:00 2001 From: taitus Date: Tue, 29 Dec 2020 12:26:55 +0100 Subject: [PATCH] Update "comment" notification email to add unsubscribe link We modified the link that previously redirected us to the "My content" page to redirect us to the new page for managing subscriptions. We also adapted the existing generic text by adding a description of the related notification. --- app/mailers/mailer.rb | 6 +++++ app/views/mailer/comment.html.erb | 9 +++++++- config/locales/en/mailers.yml | 2 ++ config/locales/es/mailers.yml | 2 ++ spec/mailers/mailer_spec.rb | 22 ++++++++++++++++++ spec/system/admin/system_emails_spec.rb | 3 +++ spec/system/emails_spec.rb | 30 ++++++++++++++++--------- 7 files changed, 63 insertions(+), 11 deletions(-) diff --git a/app/mailers/mailer.rb b/app/mailers/mailer.rb index 9fbb8ec88..17440bec2 100644 --- a/app/mailers/mailer.rb +++ b/app/mailers/mailer.rb @@ -9,6 +9,7 @@ class Mailer < ApplicationMailer @comment = comment @commentable = comment.commentable @email_to = @commentable.author.email + manage_subscriptions_token(@commentable.author) with_user(@commentable.author) do subject = t("mailers.comment.subject", commentable: t("activerecord.models.#{@commentable.class.name.underscore}", count: 1).downcase) @@ -150,4 +151,9 @@ class Mailer < ApplicationMailer mail.perform_deliveries = false end end + + def manage_subscriptions_token(user) + user.add_subscriptions_token + @token = user.subscriptions_token + end end diff --git a/app/views/mailer/comment.html.erb b/app/views/mailer/comment.html.erb index c4e7d1354..d831b60bc 100644 --- a/app/views/mailer/comment.html.erb +++ b/app/views/mailer/comment.html.erb @@ -18,6 +18,13 @@

- <%= t("mailers.config.manage_email_subscriptions") %> <%= link_to t("account.show.title"), account_url, style: "color: #2895F1; text-decoration:none;" %> + <%= sanitize(t("mailers.config.unsubscribe_text", + notifications: link_to( + t("mailers.config.notifications_link"), + edit_subscriptions_url(token: @token), + style: "color: #2895F1; text-decoration: none;" + ), + notification: User.human_attribute_name(:email_on_comment) + )) %>

diff --git a/config/locales/en/mailers.yml b/config/locales/en/mailers.yml index 552aa9831..d8d5f05fd 100644 --- a/config/locales/en/mailers.yml +++ b/config/locales/en/mailers.yml @@ -9,6 +9,8 @@ en: title: New comment config: manage_email_subscriptions: To stop receiving these emails change your settings in + notifications_link: Notifications + unsubscribe_text: 'To unsubscribe from these emails, visit %{notifications} and uncheck "%{notification}".' email_verification: click_here_to_verify: this link instructions_2: This email will verify your account with %{document_type} %{document_number}. If these don't belong to you, please don't click on the previous link and ignore this email. diff --git a/config/locales/es/mailers.yml b/config/locales/es/mailers.yml index ec4974a4d..d45c500c7 100644 --- a/config/locales/es/mailers.yml +++ b/config/locales/es/mailers.yml @@ -9,6 +9,8 @@ es: title: Nuevo comentario config: manage_email_subscriptions: Puedes dejar de recibir estos emails cambiando tu configuración en + notifications_link: Notificaciones + unsubscribe_text: 'Para darse de baja de estos emails, puedes entrar en %{notifications} y desmarcar la opción "%{notification}".' email_verification: click_here_to_verify: en este enlace instructions_2: Este email es para verificar tu cuenta con %{document_type} %{document_number}. Si esos no son tus datos, por favor no pulses el enlace anterior e ignora este email. diff --git a/spec/mailers/mailer_spec.rb b/spec/mailers/mailer_spec.rb index d69d9cc01..21231ca4d 100644 --- a/spec/mailers/mailer_spec.rb +++ b/spec/mailers/mailer_spec.rb @@ -29,4 +29,26 @@ describe Mailer do expect(email.subject).to include("commented on your proposal") end end + + describe "#manage_subscriptions_token" do + let(:user) { create(:user) } + let(:proposal) { create(:proposal, author: user) } + let(:comment) { create(:comment, commentable: proposal) } + + it "generates a subscriptions token when the receiver doesn't have one" do + user.update!(subscriptions_token: nil) + + Mailer.comment(comment).deliver_now + + expect(user.reload.subscriptions_token).to be_present + end + + it "uses the existing subscriptions token when the receivesr already has one" do + user.update!(subscriptions_token: "subscriptions_token_value") + + Mailer.comment(comment).deliver_now + + expect(user.subscriptions_token).to eq "subscriptions_token_value" + end + end end diff --git a/spec/system/admin/system_emails_spec.rb b/spec/system/admin/system_emails_spec.rb index e464998f2..7786631f7 100644 --- a/spec/system/admin/system_emails_spec.rb +++ b/spec/system/admin/system_emails_spec.rb @@ -155,6 +155,9 @@ describe "System Emails" do expect(page).to have_content comment.body expect(page).to have_link "Let's do...", href: debate_url(debate, host: app_host) + expect(page).to have_link("Notifications", + href: edit_subscriptions_url(token: user.subscriptions_token, + host: app_host)) end scenario "#reply" do diff --git a/spec/system/emails_spec.rb b/spec/system/emails_spec.rb index 9f9cb9398..87ccada2a 100644 --- a/spec/system/emails_spec.rb +++ b/spec/system/emails_spec.rb @@ -50,8 +50,10 @@ describe "Emails" do expect(email).to have_subject("Someone has commented on your citizen proposal") expect(email).to deliver_to(proposal.author) expect(email).to have_body_text(proposal_path(proposal)) - expect(email).to have_body_text("To stop receiving these emails change your settings in") - expect(email).to have_body_text(account_path) + expect(email).to have_body_text("To unsubscribe from these emails, visit") + expect(email).to have_body_text(edit_subscriptions_path(token: proposal.author.subscriptions_token)) + expect(email).to have_body_text( + 'and uncheck "Notify me by email when someone comments on my proposals or debates"') end scenario "Do not send email about own proposal comments" do @@ -77,8 +79,10 @@ describe "Emails" do expect(email).to have_subject("Someone has commented on your debate") expect(email).to deliver_to(debate.author) expect(email).to have_body_text(debate_path(debate)) - expect(email).to have_body_text("To stop receiving these emails change your settings in") - expect(email).to have_body_text(account_path) + expect(email).to have_body_text("To unsubscribe from these emails, visit") + expect(email).to have_body_text(edit_subscriptions_path(token: debate.author.subscriptions_token)) + expect(email).to have_body_text( + 'and uncheck "Notify me by email when someone comments on my proposals or debates"') end scenario "Do not send email about own debate comments" do @@ -104,8 +108,10 @@ describe "Emails" do expect(email).to have_subject("Someone has commented on your investment") expect(email).to deliver_to(investment.author) expect(email).to have_body_text(budget_investment_path(investment, budget_id: investment.budget_id)) - expect(email).to have_body_text("To stop receiving these emails change your settings in") - expect(email).to have_body_text(account_path) + expect(email).to have_body_text("To unsubscribe from these emails, visit") + expect(email).to have_body_text(edit_subscriptions_path(token: investment.author.subscriptions_token)) + expect(email).to have_body_text( + 'and uncheck "Notify me by email when someone comments on my proposals or debates"') end scenario "Do not send email about own budget investments comments" do @@ -132,8 +138,10 @@ describe "Emails" do expect(email).to have_subject("Someone has commented on your topic") expect(email).to deliver_to(topic.author) expect(email).to have_body_text(community_topic_path(topic, community_id: topic.community_id)) - expect(email).to have_body_text("To stop receiving these emails change your settings in") - expect(email).to have_body_text(account_path) + expect(email).to have_body_text("To unsubscribe from these emails, visit") + expect(email).to have_body_text(edit_subscriptions_path(token: topic.author.subscriptions_token)) + expect(email).to have_body_text( + 'and uncheck "Notify me by email when someone comments on my proposals or debates"') end scenario "Do not send email about own topic comments" do @@ -159,8 +167,10 @@ describe "Emails" do expect(email).to have_subject("Someone has commented on your poll") expect(email).to deliver_to(poll.author) expect(email).to have_body_text(poll_path(poll)) - expect(email).to have_body_text("To stop receiving these emails change your settings in") - expect(email).to have_body_text(account_path) + expect(email).to have_body_text("To unsubscribe from these emails, visit") + expect(email).to have_body_text(edit_subscriptions_path(token: poll.author.subscriptions_token)) + expect(email).to have_body_text( + 'and uncheck "Notify me by email when someone comments on my proposals or debates"') end scenario "Do not send email about own poll comments" do