Ensuring that established tags don't respond 404 (#17058)

Prior to this commit, we determined the 404 status based on the final
scoped query of the articles.  That scoped query would limit to a
timeframe.

However, what we want is to not render a 404 status if we have an
"established" tag.  An established tag means that we have at least one
published article (regardless of when we published).

Fixes forem/forem#16932
This commit is contained in:
Jeremy Friesen 2022-04-01 10:25:28 -04:00 committed by GitHub
parent 43c20d314f
commit a8ec0c3058
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 9 deletions

View file

@ -21,7 +21,6 @@ module Stories
set_number_of_articles(tag: @tag)
set_stories(number_of_articles: @number_of_articles, tag: @tag, page: @page)
not_found_if_not_established(tag: @tag, stories: @stories)
set_surrogate_key_header "articles-#{@tag}"
set_cache_control_headers(600,
@ -45,11 +44,16 @@ module Stories
@number_of_articles = user_signed_in? ? 5 : SIGNED_OUT_RECORD_COUNT
end
# @raise [ActiveRecord::NotFound] if we don't have an "established" tag
def set_stories(number_of_articles:, page:, tag:)
stories = Articles::Feeds::Tag.call(tag.name, number_of_articles: number_of_articles, page: page)
stories = stories.approved if tag.requires_approval?
# NOTE: We want to check if there are any stories regardless of timeframe.
not_found unless established?(stories: stories, tag: tag)
# Now, apply the filter.
stories = stories_by_timeframe(stories: stories)
@stories = stories.decorate
end
@ -58,10 +62,17 @@ module Stories
tag.articles.published.where(score: Settings::UserExperience.tag_feed_minimum_score..).count
end
def not_found_if_not_established(stories:, tag:)
return if tag.supported?
# Do we have an established tag? That means it's supported OR we have at least one published story.
#
# @param stories [ActiveRecord::Relation<Article>]
# @param tag [Tag]
# @return [TrueClass] if we have published stories for this tag
# @return [FalseClass] if we do not
def established?(stories:, tag:)
return true if tag.supported?
return true if stories.published.exists?
not_found unless stories.any?(&:published?)
false
end
def stories_by_timeframe(stories:)

View file

@ -63,16 +63,20 @@ RSpec.describe "Stories::TaggedArticlesIndex", type: :request do
it "renders page when tag is not supported but has at least one approved article" do
unsupported_tag = create(:tag, supported: false)
create(:article, published: true, approved: true, tags: unsupported_tag)
create(:article, published: true, approved: true, tags: unsupported_tag, published_at: 5.years.ago)
get "/t/#{unsupported_tag.name}/top/week"
expect(response.body).to include(unsupported_tag.name)
expect(response).to be_successful
get "/t/#{unsupported_tag.name}/top/month"
expect(response.body).to include(unsupported_tag.name)
expect(response).to be_successful
get "/t/#{unsupported_tag.name}/top/year"
expect(response.body).to include(unsupported_tag.name)
expect(response).to be_successful
get "/t/#{unsupported_tag.name}/top/infinity"
expect(response.body).to include(unsupported_tag.name)
expect(response).to be_successful
end
it "returns not found if no published posts and tag not supported" do