mirror of
https://github.com/discourse/discourse.git
synced 2025-06-10 19:59:12 +08:00
FIX: categories page would sometimes show no topics, even if there are some visible topics to show
This commit is contained in:
@ -19,14 +19,21 @@ class CategoryFeaturedTopic < ActiveRecord::Base
|
|||||||
def self.feature_topics_for(c, existing=nil)
|
def self.feature_topics_for(c, existing=nil)
|
||||||
return if c.blank?
|
return if c.blank?
|
||||||
|
|
||||||
query = TopicQuery.new(CategoryFeaturedTopic.fake_admin,
|
query_opts = {
|
||||||
per_page: SiteSetting.category_featured_topics,
|
per_page: SiteSetting.category_featured_topics,
|
||||||
except_topic_ids: [c.topic_id],
|
except_topic_ids: [c.topic_id],
|
||||||
visible: true,
|
visible: true,
|
||||||
no_definitions: true)
|
no_definitions: true
|
||||||
|
}
|
||||||
|
|
||||||
|
# Add topics, even if they're in secured categories:
|
||||||
|
query = TopicQuery.new(CategoryFeaturedTopic.fake_admin, query_opts)
|
||||||
results = query.list_category_topic_ids(c).uniq
|
results = query.list_category_topic_ids(c).uniq
|
||||||
|
|
||||||
|
# Add some topics that are visible to everyone:
|
||||||
|
anon_query = TopicQuery.new(nil, query_opts.merge({except_topic_ids: [c.topic_id] + results}))
|
||||||
|
results += anon_query.list_category_topic_ids(c).uniq
|
||||||
|
|
||||||
return if results == existing
|
return if results == existing
|
||||||
|
|
||||||
CategoryFeaturedTopic.transaction do
|
CategoryFeaturedTopic.transaction do
|
||||||
|
@ -44,7 +44,7 @@ class TopicQuery
|
|||||||
|
|
||||||
def initialize(user=nil, options={})
|
def initialize(user=nil, options={})
|
||||||
options.assert_valid_keys(VALID_OPTIONS)
|
options.assert_valid_keys(VALID_OPTIONS)
|
||||||
@options = options
|
@options = options.dup
|
||||||
@user = user
|
@user = user
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -34,9 +34,10 @@ describe CategoryFeaturedTopic do
|
|||||||
|
|
||||||
|
|
||||||
it 'should feature stuff in the correct order' do
|
it 'should feature stuff in the correct order' do
|
||||||
SiteSetting.stubs(:category_featured_topics).returns(3)
|
SiteSetting.stubs(:category_featured_topics).returns(2)
|
||||||
|
|
||||||
category = Fabricate(:category)
|
category = Fabricate(:category)
|
||||||
|
t5 = Fabricate(:topic, category_id: category.id, bumped_at: 12.minutes.ago)
|
||||||
t4 = Fabricate(:topic, category_id: category.id, bumped_at: 10.minutes.ago)
|
t4 = Fabricate(:topic, category_id: category.id, bumped_at: 10.minutes.ago)
|
||||||
t3 = Fabricate(:topic, category_id: category.id, bumped_at: 7.minutes.ago)
|
t3 = Fabricate(:topic, category_id: category.id, bumped_at: 7.minutes.ago)
|
||||||
t2 = Fabricate(:topic, category_id: category.id, bumped_at: 4.minutes.ago)
|
t2 = Fabricate(:topic, category_id: category.id, bumped_at: 4.minutes.ago)
|
||||||
@ -45,9 +46,10 @@ describe CategoryFeaturedTopic do
|
|||||||
|
|
||||||
CategoryFeaturedTopic.feature_topics_for(category)
|
CategoryFeaturedTopic.feature_topics_for(category)
|
||||||
|
|
||||||
|
# Should find more than we need: pinned topics first, then category_featured_topics * 2
|
||||||
expect(
|
expect(
|
||||||
CategoryFeaturedTopic.where(category_id: category.id).pluck(:topic_id)
|
CategoryFeaturedTopic.where(category_id: category.id).pluck(:topic_id)
|
||||||
).to eq([pinned.id, t2.id, t1.id, t3.id])
|
).to eq([pinned.id, t2.id, t1.id, t3.id, t4.id])
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user