Certainly! I’ll examine the message_builder.rb
file and explain how the unsubscribe_link
and unsubscribe_link_and_mail
work. Let’s start by retrieving the content of the file.
GitHub file content
Retrieved content of ["lib/email/message_builder.rb"]
2dae20c05434cdb96f59a996167f77a389169c4d
from discourse/discourse@2dae20c05434cdb96f59a996167f77a389169c4d
I apologize for the error in retrieving the file content. It seems there was an issue with the file path format. Let me try again with the correct format.
GitHub file content
Retrieved content of lib/email/message_builder.rb from discourse/discourse@2dae20c05434cdb96f59a996167f77a389169c4d
Thank you for providing the file content. I’ll explain how the unsubscribe_link
and unsubscribe_link_and_mail
work in the message_builder.rb
file.
Both unsubscribe_link
and unsubscribe_link_and_mail
are used to generate unsubscribe instructions for email notifications. They are part of the email template system in Discourse. Here’s how they work:
-
The default text for both is:
To unsubscribe from these emails, [click here](%{unsubscribe_url}).
-
The choice between unsubscribe_link
and unsubscribe_link_and_mail
is determined in the initialize
method of the MessageBuilder
class:
if @opts[:add_unsubscribe_link]
unsubscribe_string =
if @opts[:mailing_list_mode]
"unsubscribe_mailing_list"
elsif SiteSetting.unsubscribe_via_email_footer
"unsubscribe_link_and_mail"
else
"unsubscribe_link"
end
@template_args[:unsubscribe_instructions] = I18n.t(unsubscribe_string, @template_args)
end
-
The decision is based on the following conditions:
- If
mailing_list_mode
is enabled, it uses unsubscribe_mailing_list
.
- If
SiteSetting.unsubscribe_via_email_footer
is true, it uses unsubscribe_link_and_mail
.
- Otherwise, it uses
unsubscribe_link
.
-
The chosen unsubscribe string is then translated using I18n.t()
and stored in @template_args[:unsubscribe_instructions]
.
-
The unsubscribe_url
is set earlier in the initialize
method:
@template_args = {
# ... other args ...
user_preferences_url: "#{Discourse.base_url}/my/preferences",
# ... other args ...
}.merge!(@opts)
- Later in the code, when building the email headers, the unsubscribe URL is set:
unsubscribe_url =
@template_args[:unsubscribe_url].presence || @template_args[:user_preferences_url]
result["List-Unsubscribe"] = "<#{unsubscribe_url}>"
-
The unsubscribe instructions are included in both the HTML and text versions of the email:
-
For HTML emails:
if @template_args[:unsubscribe_instructions].present?
unsubscribe_instructions =
PrettyText.cook(@template_args[:unsubscribe_instructions], sanitize: false).html_safe
html_override.gsub!("%{unsubscribe_instructions}", unsubscribe_instructions)
else
html_override.gsub!("%{unsubscribe_instructions}", "")
end
-
For text emails:
if @template_args[:unsubscribe_instructions].present?
body << "\n"
body << @template_args[:unsubscribe_instructions]
end
In summary, unsubscribe_link
and unsubscribe_link_and_mail
are translation keys that determine the content of the unsubscribe instructions in email notifications. The choice between them depends on the site settings and mailing list mode. The resulting instructions are included in both HTML and text versions of the email, and a proper unsubscribe URL is set in the email headers.