From 565a9671927724138b7e9e86806134785c852074 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Tue, 19 Nov 2019 12:31:00 -0500 Subject: [PATCH] FIX: email excerpts for posts starting with a quote were displaying a username If a post starts with a post quote and has no other text content, then the email excerpt was the name of the person quoted and nothing else. The intention was to show the contents of the first paragraph or div after the quote. With this change, a quote followed by an image will use the image as the excerpt. A quote followed by a onebox will use the onebox. --- app/helpers/user_notifications_helper.rb | 5 +-- .../helpers/user_notifications_helper_spec.rb | 31 +++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/app/helpers/user_notifications_helper.rb b/app/helpers/user_notifications_helper.rb index d7e619f0298..0df7aa6926c 100644 --- a/app/helpers/user_notifications_helper.rb +++ b/app/helpers/user_notifications_helper.rb @@ -47,8 +47,9 @@ module UserNotificationsHelper return result unless result.blank? - # If there is no first paragaph, return the first div (onebox) - doc.css('div').first + # If there is no first paragaph with text, return the first paragraph with + # something else (an image) or div (a onebox). + doc.css('body > p, body > div').first end def email_excerpt(html_arg, post = nil) diff --git a/spec/helpers/user_notifications_helper_spec.rb b/spec/helpers/user_notifications_helper_spec.rb index a0d266e2b10..7610495b258 100644 --- a/spec/helpers/user_notifications_helper_spec.rb +++ b/spec/helpers/user_notifications_helper_spec.rb @@ -13,6 +13,19 @@ describe UserNotificationsHelper do paragraphs.join("\n") end + let(:post_quote) do + <<~HTML + + HTML + end + it "can return the first paragraph" do SiteSetting.digest_min_excerpt_length = 50 expect(helper.email_excerpt(cooked)).to eq(paragraphs[0]) @@ -54,6 +67,24 @@ describe UserNotificationsHelper do expect(helper.email_excerpt(cooked)).to eq "

BEFORE

\n

This is a user quote

\n

AFTER

" end + + it "defaults to content after post quote (image w/ no text)" do + image_paragraph = '

' + cooked = <<~HTML + #{post_quote} + #{image_paragraph} + HTML + expect(helper.email_excerpt(cooked)).to eq(image_paragraph) + end + + it "defaults to content after post quote (onebox)" do + aside_onebox = '' + cooked = <<~HTML + #{post_quote} + #{aside_onebox} + HTML + expect(helper.email_excerpt(cooked)).to eq(aside_onebox) + end end describe '#logo_url' do