From df68d11c388e405b21c285c17ba633b1f731e512 Mon Sep 17 00:00:00 2001 From: Martin Brennan Date: Thu, 21 May 2020 13:19:48 +1000 Subject: [PATCH] FEATURE: Add topic excerpt max length site setting (#9847) Adds a new topic_excerpt_maxlength site setting. * When topic excerpt is requested for a post, use the new topic_excerpt_maxlength site setting to limit the size of the excerpt * Remove code for getting/setting Post.excerpt_size as it is not used anywhere --- app/models/post.rb | 10 +--------- config/locales/server.en.yml | 1 + config/site_settings.yml | 6 ++++++ spec/components/search_spec.rb | 2 +- spec/fabricators/post_fabricator.rb | 2 +- spec/models/post_spec.rb | 19 +++++++++++++++++++ 6 files changed, 29 insertions(+), 11 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index 89dbbcfebba..30ebaf01630 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -159,14 +159,6 @@ class Post < ActiveRecord::Base includes(:post_details).find_by(post_details: { key: key, value: value }) end - def self.excerpt_size=(sz) - @excerpt_size = sz - end - - def self.excerpt_size - @excerpt_size || 220 - end - def whisper? post_type == Post.types[:whisper] end @@ -482,7 +474,7 @@ class Post < ActiveRecord::Base end def excerpt_for_topic - Post.excerpt(cooked, Post.excerpt_size, strip_links: true, strip_images: true, post: self) + Post.excerpt(cooked, SiteSetting.topic_excerpt_maxlength, strip_links: true, strip_images: true, post: self) end def is_first_post? diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 996fd4040b4..d44d4678ed4 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -1467,6 +1467,7 @@ en: exclude_rel_nofollow_domains: "A list of domains where nofollow should not be added to links. example.com will automatically allow sub.example.com as well. As a minimum, you should add the domain of this site to help web crawlers find all content. If other parts of your website are at other domains, add those too." post_excerpt_maxlength: "Maximum length of a post excerpt / summary." + topic_excerpt_maxlength: "Maximum length of a topic excerpt / summary, generated from the first post in a topic." show_pinned_excerpt_mobile: "Show excerpt on pinned topics in mobile view." show_pinned_excerpt_desktop: "Show excerpt on pinned topics in desktop view." post_onebox_maxlength: "Maximum length of a oneboxed Discourse post in characters." diff --git a/config/site_settings.yml b/config/site_settings.yml index ea4bbd4b665..88387952d73 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -819,6 +819,12 @@ posting: ja: 120 zh_CN: 120 zh_TW: 120 + topic_excerpt_maxlength: + default: 220 + locale_default: + ja: 120 + zh_CN: 120 + zh_TW: 120 show_pinned_excerpt_mobile: client: true default: true diff --git a/spec/components/search_spec.rb b/spec/components/search_spec.rb index a455d81ec13..599ea3c5aa5 100644 --- a/spec/components/search_spec.rb +++ b/spec/components/search_spec.rb @@ -444,7 +444,7 @@ describe Search do end let(:expected_blurb) do - "...to satisfy any test conditions that require content longer than the typical test post raw content. elephant" + "...quire content longer than the typical test post raw content. It really is some long content, folks. elephant" end it 'returns the post' do diff --git a/spec/fabricators/post_fabricator.rb b/spec/fabricators/post_fabricator.rb index 42235cb94fc..77a234d9814 100644 --- a/spec/fabricators/post_fabricator.rb +++ b/spec/fabricators/post_fabricator.rb @@ -10,7 +10,7 @@ end Fabricator(:post_with_long_raw_content, from: :post) do raw 'This is a sample post with semi-long raw content. The raw content is also more than two hundred characters to satisfy any test conditions that require content longer - than the typical test post raw content.' + than the typical test post raw content. It really is some long content, folks.' end Fabricator(:post_with_youtube, from: :post) do diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index 4cc716d6916..d0e2aba6319 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -1158,6 +1158,25 @@ describe Post do expect(post.custom_fields).to eq("Tommy" => "Hanks", "Vincent" => "Vega") end + describe "#excerpt_for_topic" do + it "returns a topic excerpt, defaulting to 220 chars" do + expected_excerpt = "This is a sample post with semi-long raw content. The raw content is also more than \ntwo hundred characters to satisfy any test conditions that require content longer \nthan the typical test post raw content. It really is…" + post = Fabricate(:post_with_long_raw_content) + post.rebake! + excerpt = post.excerpt_for_topic + expect(excerpt).to eq(expected_excerpt) + end + + it "respects the site setting for topic excerpt" do + SiteSetting.topic_excerpt_maxlength = 10 + expected_excerpt = "This is a …" + post = Fabricate(:post_with_long_raw_content) + post.rebake! + excerpt = post.excerpt_for_topic + expect(excerpt).to eq(expected_excerpt) + end + end + describe "#rebake!" do it "will rebake a post correctly" do post = create_post