Adjust ActiveThreadsQuery to use home_feed_minimum_score (#20004)

This commit is contained in:
Ben Halpern 2023-08-29 09:35:09 -04:00 committed by GitHub
parent d227e1d285
commit 944991a432
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 18 deletions

View file

@ -7,8 +7,6 @@ module Articles
count: 10
}.with_indifferent_access.freeze
MINIMUM_SCORE = -4
# Get the "plucked" attribute information for the article thread.
#
# @param relation [ActiveRecord::Relation] the original Article scope
@ -28,19 +26,20 @@ module Articles
# @see `./app/views/articles/_widget_list_item.html.erb` for the
# importance of maintaining position of these parameters.
def self.call(relation: Article.published, **options)
minimum_score = Settings::UserExperience.home_feed_minimum_score.to_i
options = DEFAULT_OPTIONS.merge(options)
tags, time_ago, count = options.values_at(:tags, :time_ago, :count)
relation = relation.limit(count)
relation = relation.cached_tagged_with(tags)
relation = if time_ago == "latest"
relation = relation.where(score: MINIMUM_SCORE..).presence || relation
relation = relation.where(score: minimum_score..).presence || relation
relation.order(published_at: :desc)
elsif time_ago
relation = relation.where(published_at: time_ago.., score: MINIMUM_SCORE..).presence || relation
relation = relation.where(published_at: time_ago.., score: minimum_score..).presence || relation
relation.order(comments_count: :desc)
else
relation = relation.where(published_at: 3.days.ago.., score: MINIMUM_SCORE..).presence || relation
relation = relation.where(published_at: 3.days.ago.., score: minimum_score..).presence || relation
relation.order("last_comment_at DESC NULLS LAST")
end
relation.pluck(:path, :title, :comments_count, :created_at)

View file

@ -1,16 +1,18 @@
require "rails_helper"
RSpec.describe Articles::ActiveThreadsQuery, type: :query do
let(:min_score) { Settings::UserExperience.home_feed_minimum_score }
before do
create(:article, score: described_class::MINIMUM_SCORE - 1, tags: "watercooler")
create(:article, score: min_score - 1, tags: "watercooler")
end
describe "::call" do
context "when time_ago is latest" do
it "returns the latest article with a good score", :aggregate_failures do
article = create(:article, tags: "discuss", score: described_class::MINIMUM_SCORE + 1)
article = create(:article, tags: "discuss", score: min_score + 1)
create(:article, :past, past_published_at: 2.days.ago, tags: "discuss",
score: described_class::MINIMUM_SCORE + 1)
score: min_score + 1)
result = described_class.call(tags: "discuss", time_ago: "latest", count: 10)
expect(result.length).to eq(2)
@ -18,7 +20,7 @@ RSpec.describe Articles::ActiveThreadsQuery, type: :query do
end
it "returns any article if no higher-quality article is found", :aggregate_failures do
article = create(:article, tags: "discuss", score: described_class::MINIMUM_SCORE - 10)
article = create(:article, tags: "discuss", score: min_score - 10)
result = described_class.call(tags: "discuss", time_ago: "latest", count: 10)
expect(result.length).to eq(1)
@ -30,10 +32,10 @@ RSpec.describe Articles::ActiveThreadsQuery, type: :query do
it "returns article ordered_by comment_count based on time", :aggregate_failures do
time = 2.days.ago
article = create(:article, :past, comments_count: 20, past_published_at: time, tags: "discuss",
score: described_class::MINIMUM_SCORE)
create(:article, comments_count: 10, tags: "discuss", score: described_class::MINIMUM_SCORE)
score: min_score)
create(:article, comments_count: 10, tags: "discuss", score: min_score)
create(:article, :past, past_published_at: time - 2.days, comments_count: 30, tags: "discuss",
score: described_class::MINIMUM_SCORE)
score: min_score)
result = described_class.call(tags: "discuss", time_ago: time, count: 10)
expect(result.length).to eq(2)
@ -43,8 +45,8 @@ RSpec.describe Articles::ActiveThreadsQuery, type: :query do
it "returns any article when no higher-quality article could be found", :aggregate_failures do
time = 2.days.ago
article = create(:article, :past, comments_count: 20, past_published_at: time - 5.days, tags: "discuss",
score: described_class::MINIMUM_SCORE)
create(:article, comments_count: 10, tags: "discuss", score: described_class::MINIMUM_SCORE - 10)
score: min_score)
create(:article, comments_count: 10, tags: "discuss", score: min_score - 10)
result = described_class.call(tags: "discuss", time_ago: time, count: 10)
expect(result.length).to eq(2)
@ -55,8 +57,8 @@ RSpec.describe Articles::ActiveThreadsQuery, type: :query do
context "when time_ago is not given" do
it "returns articles ordered by last_comment_at, not based on time", :aggregate_failures do
article = create(:article, last_comment_at: Time.zone.now, tags: "discuss",
score: described_class::MINIMUM_SCORE)
create(:article, last_comment_at: nil, tags: "discuss", score: described_class::MINIMUM_SCORE)
score: min_score)
create(:article, last_comment_at: nil, tags: "discuss", score: min_score)
result = described_class.call(tags: "discuss", time_ago: nil, count: 10)
expect(result.length).to eq(2)
@ -65,8 +67,8 @@ RSpec.describe Articles::ActiveThreadsQuery, type: :query do
it "returns any article, with nil articles last, if no higher-quality articles exist", :aggregate_failures do
article = create(:article, last_comment_at: Time.zone.now, tags: "discuss",
score: described_class::MINIMUM_SCORE - 10)
create(:article, last_comment_at: 2.days.ago, tags: "discuss", score: described_class::MINIMUM_SCORE - 10)
score: min_score - 10)
create(:article, last_comment_at: 2.days.ago, tags: "discuss", score: min_score - 10)
result = described_class.call(tags: "discuss", time_ago: nil, count: 10)
expect(result.length).to eq(2)