diff --git a/app/models/trends/links.rb b/app/models/trends/links.rb index 57ad486631c..35ccf7744c3 100644 --- a/app/models/trends/links.rb +++ b/app/models/trends/links.rb @@ -16,7 +16,7 @@ class Trends::Links < Trends::Base class Query < Trends::Query def to_arel scope = PreviewCard.joins(:trend).reorder(score: :desc) - scope = scope.merge(language_order_clause) if preferred_languages.present? + scope = scope.reorder(language_order_clause, score: :desc) if preferred_languages.present? scope = scope.merge(PreviewCardTrend.allowed) if @allowed scope = scope.offset(@offset) if @offset.present? scope = scope.limit(@limit) if @limit.present? @@ -25,8 +25,8 @@ class Trends::Links < Trends::Base private - def language_order_clause - language_order_for(PreviewCardTrend) + def trend_class + PreviewCardTrend end end diff --git a/app/models/trends/query.rb b/app/models/trends/query.rb index abed64042ec..670390ae3cf 100644 --- a/app/models/trends/query.rb +++ b/app/models/trends/query.rb @@ -94,11 +94,14 @@ class Trends::Query to_arel.to_a end - def language_order_for(trend_class) + def language_order_clause + Arel::Nodes::Case.new.when(language_is_preferred).then(1).else(0).desc + end + + def language_is_preferred trend_class - .reorder(nil) - .in_order_of(:language, [preferred_languages], filter: false) - .order(score: :desc) + .arel_table[:language] + .in(preferred_languages) end def preferred_languages diff --git a/app/models/trends/statuses.rb b/app/models/trends/statuses.rb index 9c47dd486b9..1a41eb9e9a3 100644 --- a/app/models/trends/statuses.rb +++ b/app/models/trends/statuses.rb @@ -15,7 +15,7 @@ class Trends::Statuses < Trends::Base class Query < Trends::Query def to_arel scope = Status.joins(:trend).reorder(score: :desc) - scope = scope.merge(language_order_clause) if preferred_languages.present? + scope = scope.reorder(language_order_clause, score: :desc) if preferred_languages.present? scope = scope.merge(StatusTrend.allowed) if @allowed scope = scope.not_excluded_by_account(@account).not_domain_blocked_by_account(@account) if @account.present? scope = scope.offset(@offset) if @offset.present? @@ -25,8 +25,8 @@ class Trends::Statuses < Trends::Base private - def language_order_clause - language_order_for(StatusTrend) + def trend_class + StatusTrend end end diff --git a/app/models/trends/tags.rb b/app/models/trends/tags.rb index 84e8dde11a5..63897f4f930 100644 --- a/app/models/trends/tags.rb +++ b/app/models/trends/tags.rb @@ -16,7 +16,7 @@ class Trends::Tags < Trends::Base class Query < Trends::Query def to_arel scope = Tag.joins(:trend).reorder(score: :desc) - scope = scope.merge(language_order_clause) if preferred_languages.present? + scope = scope.reorder(language_order_clause, score: :desc) if preferred_languages.present? scope = scope.merge(TagTrend.allowed) if @allowed scope = scope.offset(@offset) if @offset.present? scope = scope.limit(@limit) if @limit.present? @@ -25,8 +25,8 @@ class Trends::Tags < Trends::Base private - def language_order_clause - language_order_for(TagTrend) + def trend_class + TagTrend end end diff --git a/spec/models/trends/links_spec.rb b/spec/models/trends/links_spec.rb index b0d41d4613a..81a4270c386 100644 --- a/spec/models/trends/links_spec.rb +++ b/spec/models/trends/links_spec.rb @@ -24,6 +24,20 @@ RSpec.describe Trends::Links do .to eq([lower_score.preview_card, higher_score.preview_card]) end end + + context 'when account has chosen languages' do + let!(:lang_match_higher_score) { Fabricate :preview_card_trend, score: 10, language: 'is' } + let!(:lang_match_lower_score) { Fabricate :preview_card_trend, score: 1, language: 'da' } + let(:user) { Fabricate :user, chosen_languages: %w(da is) } + let(:account) { Fabricate :account, user: user } + + before { subject.filtered_for!(account) } + + it 'returns results' do + expect(subject.records) + .to eq([lang_match_higher_score.preview_card, lang_match_lower_score.preview_card, higher_score.preview_card, lower_score.preview_card]) + end + end end end end diff --git a/spec/models/trends/statuses_spec.rb b/spec/models/trends/statuses_spec.rb index abb1535d04a..39839010427 100644 --- a/spec/models/trends/statuses_spec.rb +++ b/spec/models/trends/statuses_spec.rb @@ -66,6 +66,20 @@ RSpec.describe Trends::Statuses do .to eq([lower_score.status, higher_score.status]) end end + + context 'when account has chosen languages' do + let!(:lang_match_higher_score) { Fabricate :status_trend, score: 10, language: 'is' } + let!(:lang_match_lower_score) { Fabricate :status_trend, score: 1, language: 'da' } + let(:user) { Fabricate :user, chosen_languages: %w(da is) } + let(:account) { Fabricate :account, user: user } + + before { subject.filtered_for!(account) } + + it 'returns results' do + expect(subject.records) + .to eq([lang_match_higher_score.status, lang_match_lower_score.status, higher_score.status, lower_score.status]) + end + end end end end diff --git a/spec/models/trends/tags_spec.rb b/spec/models/trends/tags_spec.rb index 8f36b4a50d3..dacae602acb 100644 --- a/spec/models/trends/tags_spec.rb +++ b/spec/models/trends/tags_spec.rb @@ -50,6 +50,20 @@ RSpec.describe Trends::Tags do .to eq([lower_score.tag, higher_score.tag]) end end + + context 'when account has chosen languages' do + let!(:lang_match_higher_score) { Fabricate :tag_trend, score: 10, language: 'is' } + let!(:lang_match_lower_score) { Fabricate :tag_trend, score: 1, language: 'da' } + let(:user) { Fabricate :user, chosen_languages: %w(da is) } + let(:account) { Fabricate :account, user: user } + + before { subject.filtered_for!(account) } + + it 'returns results' do + expect(subject.records) + .to eq([lang_match_higher_score.tag, lang_match_lower_score.tag, higher_score.tag, lower_score.tag]) + end + end end end end