Skip to content

A platform for community discussion. Free, open, simple.

FIX: Do not use private categories for Rewind best posts or topics (#36712)

Martin Brennan5ec959c72c5c
diff --git a/plugins/discourse-rewind/app/services/discourse_rewind/action/best_posts.rb b/plugins/discourse-rewind/app/services/discourse_rewind/action/best_posts.rb
index 0eb56c31f37..8f8bf717609 100644
--- a/plugins/discourse-rewind/app/services/discourse_rewind/action/best_posts.rb
+++ b/plugins/discourse-rewind/app/services/discourse_rewind/action/best_posts.rb
@@ -36,11 +36,12 @@ module DiscourseRewind
return FakeData if should_use_fake_data?
best_posts =
Post
- .joins(:topic)
+ .public_posts
+ .joins(topic: :category)
.where(user_id: user.id)
.where(posts: { created_at: date, deleted_at: nil })
.where("post_number > 1")
- .where.not(topics: { archetype: Archetype.private_message })
+ .where("NOT categories.read_restricted")
.order("like_count DESC NULLS LAST, posts.created_at ASC")
.limit(3)
.select(:post_number, :topic_id, :like_count, :reply_count, :raw, :cooked)
diff --git a/plugins/discourse-rewind/app/services/discourse_rewind/action/best_topics.rb b/plugins/discourse-rewind/app/services/discourse_rewind/action/best_topics.rb
index 632c402fa69..8429e142844 100644
--- a/plugins/discourse-rewind/app/services/discourse_rewind/action/best_topics.rb
+++ b/plugins/discourse-rewind/app/services/discourse_rewind/action/best_topics.rb
@@ -33,8 +33,10 @@ module DiscourseRewind
TopTopic
.includes(:topic)
.references(:topic)
+ .joins(topic: :category)
.where(topic: { deleted_at: nil, created_at: date, user_id: user.id })
.where.not(topic: { archetype: Archetype.private_message })
+ .where("NOT categories.read_restricted")
.order("yearly_score DESC NULLS LAST")
.limit(3)
.pluck(:topic_id, :title, :excerpt, :yearly_score)
diff --git a/plugins/discourse-rewind/spec/actions/best_posts_spec.rb b/plugins/discourse-rewind/spec/actions/best_posts_spec.rb
index 5bc0999ed46..9286b57015d 100644
--- a/plugins/discourse-rewind/spec/actions/best_posts_spec.rb
+++ b/plugins/discourse-rewind/spec/actions/best_posts_spec.rb
@@ -80,5 +80,24 @@ RSpec.describe DiscourseRewind::Action::BestPosts do
expect(call_report[:data].map { |d| d[:topic_id] }).not_to include(pm_topic.id)
end
end
+
+ context "when a post is in a private category" do
+ fab!(:private_category) { Fabricate(:category, read_restricted: true) }
+ fab!(:private_topic) { Fabricate(:topic, category: private_category, user: user) }
+ fab!(:private_category_post) do
+ Fabricate(
+ :post,
+ created_at: random_datetime,
+ user: user,
+ post_number: 2,
+ topic: private_topic,
+ like_count: 99,
+ )
+ end
+
+ it "is not included" do
+ expect(call_report[:data].map { |d| d[:topic_id] }).not_to include(private_topic.id)
+ end
+ end
end
end
diff --git a/plugins/discourse-rewind/spec/actions/best_topics_spec.rb b/plugins/discourse-rewind/spec/actions/best_topics_spec.rb
index 5243b8f6cb5..4d2fca9d72a 100644
--- a/plugins/discourse-rewind/spec/actions/best_topics_spec.rb
+++ b/plugins/discourse-rewind/spec/actions/best_topics_spec.rb
@@ -75,5 +75,21 @@ RSpec.describe DiscourseRewind::Action::BestTopics do
expect(call_report[:data].map { |d| d[:topic_id] }).not_to include(pm_topic.id)
end
end
+
+ context "when topic is in a private category" do
+ fab!(:private_category) { Fabricate(:category, read_restricted: true) }
+ fab!(:private_topic) do
+ Fabricate(:topic, category: private_category, user: user, created_at: random_datetime)
+ end
+
+ before do
+ TopTopic.refresh!
+ TopTopic.find_by(topic_id: private_topic.id)&.update!(yearly_score: 99)
+ end
+
+ it "is not included" do
+ expect(call_report[:data].map { |d| d[:topic_id] }).not_to include(private_topic.id)
+ end
+ end
end
end