diff --git a/app/models/category_list.rb b/app/models/category_list.rb index 4f7d2c0610d..32efd7fafd7 100644 --- a/app/models/category_list.rb +++ b/app/models/category_list.rb @@ -66,6 +66,7 @@ class CategoryList < DraftableList @all_topics.each do |t| # hint for the serializer t.include_last_poster = true if @options[:include_topics] + t.dismissed = dismissed_topic?(t) @topics_by_id[t.id] = t end @@ -75,6 +76,15 @@ class CategoryList < DraftableList end end + def dismissed_topic?(topic) + if @guardian.current_user + @dismissed_topic_users_lookup ||= DismissedTopicUser.lookup_for(@guardian.current_user, @all_topics) + @dismissed_topic_users_lookup.include?(topic.id) + else + false + end + end + def find_categories @categories = Category.includes( :uploaded_background, diff --git a/spec/models/category_list_spec.rb b/spec/models/category_list_spec.rb index d18abbe177b..ea5c839aa8e 100644 --- a/spec/models/category_list_spec.rb +++ b/spec/models/category_list_spec.rb @@ -95,18 +95,22 @@ describe CategoryList do 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!(:dismissed_topic_user) { Fabricate(:dismissed_topic_user, topic: topic2, user: user) } def displayable_topics category_list = CategoryList.new(Guardian.new(user), include_topics: true) - category_list.categories.find { |c| c.id == topic_category.id }.displayable_topics.map(&:id) + category_list.categories.find { |c| c.id == topic_category.id }.displayable_topics end it "returns pinned topic first" do - expect(displayable_topics).to eq([pinned.id, topic3.id]) + expect(displayable_topics.map(&:id)).to eq([pinned.id, topic3.id]) TopicUser.change(user.id, pinned.id, cleared_pinned_at: pinned.pinned_at + 10) - expect(displayable_topics).to eq([topic3.id, topic2.id]) + expect(displayable_topics[0].dismissed).to eq(false) + expect(displayable_topics[1].dismissed).to eq(true) + + expect(displayable_topics.map(&:id)).to eq([topic3.id, topic2.id]) end end