From e40218d1c887af994a850b54edb97c5d0c37c39e Mon Sep 17 00:00:00 2001 From: evazion Date: Sun, 2 Jan 2022 16:33:45 -0600 Subject: [PATCH] Fix #4515: Set List-Unsubscribe header on notification emails. --- app/helpers/users_helper.rb | 6 ++++-- app/mailers/application_mailer.rb | 5 +++++ app/mailers/user_mailer.rb | 3 --- app/views/user_mailer/dmail_notice.html.erb | 2 +- test/unit/user_mailer_test.rb | 1 + 5 files changed, 11 insertions(+), 6 deletions(-) diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb index 6aab63a81..c4d94758f 100644 --- a/app/helpers/users_helper.rb +++ b/app/helpers/users_helper.rb @@ -13,9 +13,11 @@ module UsersHelper user.dmails.active.unread.first end - def email_sig(user) + def disable_email_notifications_url(user) verifier = ActiveSupport::MessageVerifier.new(Danbooru.config.email_key, serializer: JSON, digest: "SHA256") - verifier.generate(user.id.to_s) + sig = verifier.generate(user.id.to_s) + + maintenance_user_email_notification_url(user_id: user.id, sig: sig) end def email_verification_url(user) diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index 47a1d4ecd..547d7f6cc 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -4,9 +4,14 @@ # # @see https://guides.rubyonrails.org/action_mailer_basics.html class ApplicationMailer < ActionMailer::Base + helper :application + helper :users + include UsersHelper + default from: "#{Danbooru.config.canonical_app_name} <#{Danbooru.config.contact_email}>", content_type: "text/html" def mail(user, require_verified_email:, **options) + headers["List-Unsubscribe"] = disable_email_notifications_url(user) message = super(to: user.email_address&.address, **options) message.perform_deliveries = user.can_receive_email?(require_verified_email: require_verified_email) message diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb index 0e176df0f..b97b0518e 100644 --- a/app/mailers/user_mailer.rb +++ b/app/mailers/user_mailer.rb @@ -1,9 +1,6 @@ # frozen_string_literal: true class UserMailer < ApplicationMailer - helper :application - helper :users - # The email sent when a user receives a DMail. def dmail_notice(dmail) @dmail = dmail diff --git a/app/views/user_mailer/dmail_notice.html.erb b/app/views/user_mailer/dmail_notice.html.erb index 2256b87ef..2776e795f 100644 --- a/app/views/user_mailer/dmail_notice.html.erb +++ b/app/views/user_mailer/dmail_notice.html.erb @@ -7,6 +7,6 @@ <%= format_text(@dmail.body, base_url: root_url) %> -

<%= link_to "View message", dmail_url(@dmail, :host => Danbooru.config.hostname, :only_path => false) %> | <%= link_to "Unsubscribe", maintenance_user_email_notification_url(:user_id => @dmail.owner.id, :sig => email_sig(@dmail.owner), :host => Danbooru.config.hostname, :only_path => false) %>

+

<%= link_to "View message", dmail_url(@dmail) %> | <%= link_to "Disable notifications", disable_email_notifications_url(@dmail.to) %>

diff --git a/test/unit/user_mailer_test.rb b/test/unit/user_mailer_test.rb index 4242d60fb..2a2612e3b 100644 --- a/test/unit/user_mailer_test.rb +++ b/test/unit/user_mailer_test.rb @@ -10,6 +10,7 @@ class UserMailerTest < ActionMailer::TestCase should "work" do @dmail = create(:dmail, owner: @user, to: @user) mail = UserMailer.dmail_notice(@dmail) + assert_not_nil(mail.header["List-Unsubscribe"]) assert_emails(1) { mail.deliver_now } end