mirror of
https://github.com/discourse/discourse.git
synced 2025-09-06 10:50:21 +08:00
FIX: unpinned topics shouldn't remain pinned on categories page
This commit is contained in:
parent
e54125b5dc
commit
611b5f996e
4 changed files with 54 additions and 3 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
require_dependency 'pinned_check'
|
||||||
|
|
||||||
class CategoryList
|
class CategoryList
|
||||||
include ActiveModel::Serialization
|
include ActiveModel::Serialization
|
||||||
|
|
||||||
|
@ -17,6 +19,8 @@ class CategoryList
|
||||||
|
|
||||||
prune_empty
|
prune_empty
|
||||||
find_user_data
|
find_user_data
|
||||||
|
sort_unpinned
|
||||||
|
trim_results
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
@ -151,4 +155,27 @@ class CategoryList
|
||||||
@all_topics.each { |ft| ft.user_data = topic_lookup[ft.id] }
|
@all_topics.each { |ft| ft.user_data = topic_lookup[ft.id] }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def sort_unpinned
|
||||||
|
if @guardian.current_user && @all_topics.present?
|
||||||
|
# Put unpinned topics at the end of the list
|
||||||
|
@categories.each do |c|
|
||||||
|
next if c.displayable_topics.blank? || c.displayable_topics.size <= latest_posts_count
|
||||||
|
unpinned = []
|
||||||
|
c.displayable_topics.each do |t|
|
||||||
|
unpinned << t if t.pinned_at && PinnedCheck.unpinned?(t, t.user_data)
|
||||||
|
end
|
||||||
|
unless unpinned.empty?
|
||||||
|
c.displayable_topics = (c.displayable_topics - unpinned) + unpinned
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def trim_results
|
||||||
|
@categories.each do |c|
|
||||||
|
next if c.displayable_topics.blank?
|
||||||
|
c.displayable_topics = c.displayable_topics[0,latest_posts_count]
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -135,9 +135,10 @@ class TopicQuery
|
||||||
def list_category_topic_ids(category)
|
def list_category_topic_ids(category)
|
||||||
query = default_results(category: category.id)
|
query = default_results(category: category.id)
|
||||||
pinned_ids = query.where('pinned_at IS NOT NULL AND category_id = ?', category.id)
|
pinned_ids = query.where('pinned_at IS NOT NULL AND category_id = ?', category.id)
|
||||||
|
.limit(nil)
|
||||||
.order('pinned_at DESC').pluck(:id)
|
.order('pinned_at DESC').pluck(:id)
|
||||||
non_pinned_ids = query.where('pinned_at IS NULL OR category_id <> ?', category.id).pluck(:id)
|
non_pinned_ids = query.where('pinned_at IS NULL OR category_id <> ?', category.id).pluck(:id)
|
||||||
(pinned_ids + non_pinned_ids)[0...@options[:per_page]]
|
(pinned_ids + non_pinned_ids)
|
||||||
end
|
end
|
||||||
|
|
||||||
def list_new_in_category(category)
|
def list_new_in_category(category)
|
||||||
|
|
|
@ -102,6 +102,27 @@ describe CategoryList do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context "with pinned topics in a category" do
|
||||||
|
let!(:topic1) { Fabricate(:topic, category: topic_category, bumped_at: 8.minutes.ago) }
|
||||||
|
let!(:topic2) { Fabricate(:topic, category: topic_category, bumped_at: 5.minutes.ago) }
|
||||||
|
let!(:topic3) { Fabricate(:topic, category: topic_category, bumped_at: 2.minutes.ago) }
|
||||||
|
let!(:pinned) { Fabricate(:topic, category: topic_category, pinned_at: 10.minutes.ago, bumped_at: 10.minutes.ago) }
|
||||||
|
let(:category) { category_list.categories.first }
|
||||||
|
|
||||||
|
before do
|
||||||
|
SiteSetting.stubs(:category_featured_topics).returns(2)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns pinned topic first" do
|
||||||
|
expect(category.displayable_topics.map(&:id)).to eq([pinned.id, topic3.id])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns topics in bumped_at order if pinned was unpinned" do
|
||||||
|
PinnedCheck.stubs(:unpinned?).returns(true)
|
||||||
|
expect(category.displayable_topics.map(&:id)).to eq([topic3.id, topic2.id])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'category order' do
|
describe 'category order' do
|
||||||
|
|
|
@ -34,9 +34,11 @@ 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)
|
||||||
|
|
||||||
category = Fabricate(:category)
|
category = Fabricate(:category)
|
||||||
_t3 = Fabricate(:topic, category_id: category.id, bumped_at: 7.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)
|
||||||
t2 = Fabricate(:topic, category_id: category.id, bumped_at: 4.minutes.ago)
|
t2 = Fabricate(:topic, category_id: category.id, bumped_at: 4.minutes.ago)
|
||||||
t1 = Fabricate(:topic, category_id: category.id, bumped_at: 5.minutes.ago)
|
t1 = Fabricate(:topic, category_id: category.id, bumped_at: 5.minutes.ago)
|
||||||
pinned = Fabricate(:topic, category_id: category.id, pinned_at: 10.minutes.ago, bumped_at: 10.minutes.ago)
|
pinned = Fabricate(:topic, category_id: category.id, pinned_at: 10.minutes.ago, bumped_at: 10.minutes.ago)
|
||||||
|
@ -45,7 +47,7 @@ describe CategoryFeaturedTopic do
|
||||||
|
|
||||||
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])
|
).to eq([pinned.id, t2.id, t1.id, t3.id])
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue