From 2129ae145b983e053e39baca758ffffc895cd866 Mon Sep 17 00:00:00 2001 From: Philip How Date: Fri, 19 Apr 2024 13:56:16 +0100 Subject: [PATCH] Update style for new reply email (#20861) * update style for new reply email * Truncate and sanitize comment * rubocop * fix links, fix stop send when no content --- app/mailers/notify_mailer.rb | 9 ++ app/sanitizers/comment_email_scrubber.rb | 17 ++++ app/services/markdown_processor.rb | 4 + .../notify_mailer/new_reply_email.html.erb | 89 +++++++++++++------ 4 files changed, 92 insertions(+), 27 deletions(-) create mode 100644 app/sanitizers/comment_email_scrubber.rb diff --git a/app/mailers/notify_mailer.rb b/app/mailers/notify_mailer.rb index b3d0b5917..a2985d3d5 100644 --- a/app/mailers/notify_mailer.rb +++ b/app/mailers/notify_mailer.rb @@ -8,11 +8,20 @@ class NotifyMailer < ApplicationMailer def new_reply_email @comment = params[:comment] + sanitized_comment = ApplicationController.helpers.sanitize(@comment.processed_html, + scrubber: CommentEmailScrubber.new) + @truncated_comment = ApplicationController.helpers.truncate(sanitized_comment, length: 500, separator: " ", + omission: "...", escape: false) + @user = @comment.parent_user return if RateLimitChecker.new.limit_by_email_recipient_address(@user.email) @unsubscribe = generate_unsubscribe_token(@user.id, :email_comment_notifications) + # Don't send the email if there's no visible contents + # Placed here to allow the preview to continue to work + return if @truncated_comment.blank? + mail(to: @user.email, subject: I18n.t("mailers.notify_mailer.new_reply", name: @comment.user.name, type: @comment.parent_type)) end diff --git a/app/sanitizers/comment_email_scrubber.rb b/app/sanitizers/comment_email_scrubber.rb new file mode 100644 index 000000000..9a247ee0b --- /dev/null +++ b/app/sanitizers/comment_email_scrubber.rb @@ -0,0 +1,17 @@ +class CommentEmailScrubber < Rails::Html::PermitScrubber + def initialize + super + self.tags = MarkdownProcessor::AllowedTags::EMAIL_COMMENT + self.attributes = MarkdownProcessor::AllowedAttributes::EMAIL_COMMENT + end + + def allowed_node?(node) + tags.include?(node.name) && node.children.present? + end + + # The default behavior of PermitScrubber removes the