Fix #4515: Set List-Unsubscribe header on notification emails.
This commit is contained in:
@@ -13,9 +13,11 @@ module UsersHelper
|
|||||||
user.dmails.active.unread.first
|
user.dmails.active.unread.first
|
||||||
end
|
end
|
||||||
|
|
||||||
def email_sig(user)
|
def disable_email_notifications_url(user)
|
||||||
verifier = ActiveSupport::MessageVerifier.new(Danbooru.config.email_key, serializer: JSON, digest: "SHA256")
|
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
|
end
|
||||||
|
|
||||||
def email_verification_url(user)
|
def email_verification_url(user)
|
||||||
|
|||||||
@@ -4,9 +4,14 @@
|
|||||||
#
|
#
|
||||||
# @see https://guides.rubyonrails.org/action_mailer_basics.html
|
# @see https://guides.rubyonrails.org/action_mailer_basics.html
|
||||||
class ApplicationMailer < ActionMailer::Base
|
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"
|
default from: "#{Danbooru.config.canonical_app_name} <#{Danbooru.config.contact_email}>", content_type: "text/html"
|
||||||
|
|
||||||
def mail(user, require_verified_email:, **options)
|
def mail(user, require_verified_email:, **options)
|
||||||
|
headers["List-Unsubscribe"] = disable_email_notifications_url(user)
|
||||||
message = super(to: user.email_address&.address, **options)
|
message = super(to: user.email_address&.address, **options)
|
||||||
message.perform_deliveries = user.can_receive_email?(require_verified_email: require_verified_email)
|
message.perform_deliveries = user.can_receive_email?(require_verified_email: require_verified_email)
|
||||||
message
|
message
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class UserMailer < ApplicationMailer
|
class UserMailer < ApplicationMailer
|
||||||
helper :application
|
|
||||||
helper :users
|
|
||||||
|
|
||||||
# The email sent when a user receives a DMail.
|
# The email sent when a user receives a DMail.
|
||||||
def dmail_notice(dmail)
|
def dmail_notice(dmail)
|
||||||
@dmail = dmail
|
@dmail = dmail
|
||||||
|
|||||||
@@ -7,6 +7,6 @@
|
|||||||
<%= format_text(@dmail.body, base_url: root_url) %>
|
<%= format_text(@dmail.body, base_url: root_url) %>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<p><%= 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) %></p>
|
<p><%= link_to "View message", dmail_url(@dmail) %> | <%= link_to "Disable notifications", disable_email_notifications_url(@dmail.to) %></p>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ class UserMailerTest < ActionMailer::TestCase
|
|||||||
should "work" do
|
should "work" do
|
||||||
@dmail = create(:dmail, owner: @user, to: @user)
|
@dmail = create(:dmail, owner: @user, to: @user)
|
||||||
mail = UserMailer.dmail_notice(@dmail)
|
mail = UserMailer.dmail_notice(@dmail)
|
||||||
|
assert_not_nil(mail.header["List-Unsubscribe"])
|
||||||
assert_emails(1) { mail.deliver_now }
|
assert_emails(1) { mail.deliver_now }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user