diff --git a/lib/topic_view.rb b/lib/topic_view.rb index a45e36f6ab1..c40f82ff0b5 100644 --- a/lib/topic_view.rb +++ b/lib/topic_view.rb @@ -603,9 +603,12 @@ class TopicView @filtered_posts = unfiltered_posts if SiteSetting.ignore_user_enabled - @filtered_posts = @filtered_posts.where.not("user_id IN (?) AND id <> ?", - IgnoredUser.where(user_id: @user.id).select(:ignored_user_id), - first_post_id) + ignored_user_ids = IgnoredUser.where(user_id: @user&.id).pluck(:ignored_user_id) + + if ignored_user_ids.present? + @filtered_posts = @filtered_posts.where.not("user_id IN (?) AND id <> ?", ignored_user_ids, first_post_id) + @contains_gaps = true + end end # Filters diff --git a/spec/components/topic_view_spec.rb b/spec/components/topic_view_spec.rb index 44ed4df41d9..a45b9815a72 100644 --- a/spec/components/topic_view_spec.rb +++ b/spec/components/topic_view_spec.rb @@ -31,6 +31,71 @@ describe TopicView do expect { TopicView.new(topic.id, admin) }.not_to raise_error end + context "setup_filtered_posts" do + describe "filters posts with ignored users" do + let!(:user) { Fabricate(:user) } + let!(:ignored_user) { Fabricate(:ignored_user, user: evil_trout, ignored_user: user) } + let!(:post) { Fabricate(:post, topic: topic, user: first_poster) } + let!(:post2) { Fabricate(:post, topic: topic, user: evil_trout) } + let!(:post3) { Fabricate(:post, topic: topic, user: user) } + + describe "when SiteSetting.ignore_user_enabled is false" do + it "does not filter out ignored user posts" do + SiteSetting.ignore_user_enabled = false + + tv = TopicView.new(topic.id, evil_trout) + expect(tv.filtered_post_ids.size).to eq(3) + expect(tv.filtered_post_ids).to match_array([post.id, post2.id, post3.id]) + end + end + + describe "when SiteSetting.ignore_user_enabled is true" do + + before do + SiteSetting.ignore_user_enabled = true + end + + it "filters out ignored user posts" do + tv = TopicView.new(topic.id, evil_trout) + expect(tv.filtered_post_ids.size).to eq(2) + expect(tv.filtered_post_ids).to match_array([post.id, post2.id]) + end + + describe "when an ignored user made the original post" do + let!(:post) { Fabricate(:post, topic: topic, user: user) } + + it "filters out ignored user posts only" do + tv = TopicView.new(topic.id, evil_trout) + expect(tv.filtered_post_ids.size).to eq(2) + expect(tv.filtered_post_ids).to match_array([post.id, post2.id]) + end + end + + describe "when an anonymous user made a post" do + let(:anonymous) { Fabricate(:anonymous) } + let!(:post4) { Fabricate(:post, topic: topic, user: anonymous) } + + it "filters out ignored user posts only" do + tv = TopicView.new(topic.id, evil_trout) + expect(tv.filtered_post_ids.size).to eq(3) + expect(tv.filtered_post_ids).to match_array([post.id, post2.id, post4.id]) + end + end + + describe "when an anonymous (non signed-in) user is viewing a Topic" do + let(:anonymous) { Fabricate(:anonymous) } + let!(:post4) { Fabricate(:post, topic: topic, user: anonymous) } + + it "filters out ignored user posts only" do + tv = TopicView.new(topic.id, nil) + expect(tv.filtered_post_ids.size).to eq(4) + expect(tv.filtered_post_ids).to match_array([post.id, post2.id, post3.id, post4.id]) + end + end + end + end + end + context "chunk_size" do it "returns `chunk_size` by default" do expect(TopicView.new(topic.id, evil_trout).chunk_size).to eq(TopicView.chunk_size)