FIX: Notification email CTA by system user (#31726)

Followup https://github.com/discourse/discourse/pull/31505/

When sending notification emails for system user responses for PMs,
we removed the part of the CTA where it says "to respond to xyz" in a
previous commit.

This commit takes it slightly further -- we now only show a "Visit
Topic"
or "Visit Message" button if the PM notification is from a system user,
it's a bit cleaner.

This commit also adds more in-depth tests, and refactors the message
builder a little.
This commit is contained in:
Martin Brennan
2025-03-12 13:49:12 +10:00
committed by GitHub
parent 6e1954aa41
commit cf4d80d0b3
3 changed files with 211 additions and 50 deletions

View File

@ -7,6 +7,7 @@ module Email
attr_reader :template_args, :reply_by_email_key
ALLOW_REPLY_BY_EMAIL_HEADER = "X-Discourse-Allow-Reply-By-Email"
INSTRUCTIONS_SEPARATOR = "---\n"
def initialize(to, opts = nil)
@to = to
@ -18,65 +19,83 @@ module Email
user_preferences_url: "#{Discourse.base_url}/my/preferences",
hostname: Discourse.current_hostname,
}.merge!(@opts)
if @template_args[:url].present?
@template_args[:header_instructions] ||= I18n.t(
"user_notifications.header_instructions",
@template_args,
)
@visit_link_to_respond_key =
DiscoursePluginRegistry.apply_modifier(
:message_builder_visit_link_to_respond,
"user_notifications.visit_link_to_respond",
@opts,
@to,
)
@reply_by_email_key =
DiscoursePluginRegistry.apply_modifier(
:message_builder_reply_by_email,
"user_notifications.reply_by_email",
@opts,
@to,
)
if @opts[:include_respond_instructions] == false
@template_args[:respond_instructions] = ""
return if @template_args[:url].blank?
@template_args[:header_instructions] ||= I18n.t(
"user_notifications.header_instructions",
@template_args,
)
@visit_link_to_respond_key =
DiscoursePluginRegistry.apply_modifier(
:message_builder_visit_link_to_respond,
"user_notifications.visit_link_to_respond",
@opts,
@to,
)
@reply_by_email_key =
DiscoursePluginRegistry.apply_modifier(
:message_builder_reply_by_email,
"user_notifications.reply_by_email",
@opts,
@to,
)
if @opts[:include_respond_instructions] == false
if @opts[:private_reply]
@template_args[:respond_instructions] = I18n.t(
"user_notifications.pm_participants",
@template_args,
) if @opts[:private_reply]
)
else
if @opts[:only_reply_by_email]
string = +"user_notifications.only_reply_by_email"
if @opts[:private_reply] && @opts[:username] != Discourse.system_user.username
string << "_pm"
end
else
string =
(
if allow_reply_by_email?
+@reply_by_email_key
else
+@visit_link_to_respond_key
end
)
if @opts[:private_reply] && @opts[:username] != Discourse.system_user.username
string << "_pm"
@template_args[:respond_instructions] = ""
end
else
if @opts[:only_reply_by_email]
respond_instructions_key = +"user_notifications.only_reply_by_email"
if @opts[:private_reply]
if @opts[:username] == Discourse.system_user.username
respond_instructions_key << "_pm_button_only"
else
respond_instructions_key << "_pm"
end
end
@template_args[:respond_instructions] = "---\n" + I18n.t(string, @template_args)
end
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
respond_instructions_key =
(
if allow_reply_by_email?
+@reply_by_email_key
else
+@visit_link_to_respond_key
end
)
if @opts[:private_reply]
if @opts[:username] == Discourse.system_user.username
respond_instructions_key << "_pm_button_only"
else
"unsubscribe_link"
respond_instructions_key << "_pm"
end
@template_args[:unsubscribe_instructions] = I18n.t(unsubscribe_string, @template_args)
end
end
@template_args[:respond_instructions] = (
if respond_instructions_key != ""
INSTRUCTIONS_SEPARATOR + I18n.t(respond_instructions_key, @template_args)
else
""
end
)
end
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
end