Skip to content

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

FIX: topic filter should respect whisper rights (#35555)

Samc21a602974da
diff --git a/lib/topics_filter.rb b/lib/topics_filter.rb
index fbdb9ef5a20..8a01f8ed2c4 100644
--- a/lib/topics_filter.rb
+++ b/lib/topics_filter.rb
@@ -495,7 +495,7 @@ class TopicsFilter
topics.id NOT IN (
SELECT p1.topic_id
FROM posts p1
- WHERE p1.user_id IN (:user_ids) AND p1.deleted_at IS NULL
+ WHERE p1.user_id IN (:user_ids) AND p1.deleted_at IS NULL #{whisper_condition("p1")}
GROUP BY p1.topic_id
HAVING COUNT(DISTINCT p1.user_id) = :user_count
)
@@ -505,7 +505,7 @@ class TopicsFilter
EXISTS (
SELECT 1
FROM posts p#{idx}
- WHERE p#{idx}.topic_id = topics.id AND p#{idx}.user_id = #{uid} AND p#{idx}.deleted_at IS NULL
+ WHERE p#{idx}.topic_id = topics.id AND p#{idx}.user_id = #{uid} AND p#{idx}.deleted_at IS NULL #{whisper_condition("p#{idx}")}
LIMIT 1
)
SQL
@@ -518,6 +518,7 @@ class TopicsFilter
FROM posts p
WHERE p.user_id IN (:user_ids)
AND p.deleted_at IS NULL
+ #{whisper_condition("p")}
)
SQL
end
@@ -558,7 +559,7 @@ class TopicsFilter
SELECT 1
FROM posts pg#{idx}
JOIN group_users gu#{idx} ON gu#{idx}.user_id = pg#{idx}.user_id
- WHERE pg#{idx}.topic_id = topics.id AND gu#{idx}.group_id = #{gid}
+ WHERE pg#{idx}.topic_id = topics.id AND gu#{idx}.group_id = #{gid} #{whisper_condition("pg#{idx}")}
)
SQL
else
@@ -568,6 +569,7 @@ class TopicsFilter
FROM posts p
JOIN group_users gu ON gu.user_id = p.user_id
WHERE gu.group_id IN (:group_ids)
+ #{whisper_condition("p")}
)
SQL
end
@@ -1037,4 +1039,12 @@ class TopicsFilter
"INNER JOIN posts AS first_posts ON first_posts.topic_id = topics.id AND first_posts.post_number = 1",
)
end
+
+ def whisper_condition(table_alias)
+ if @guardian.can_see_whispers?
+ ""
+ else
+ "AND #{table_alias}.post_type != #{Post.types[:whisper]}"
+ end
+ end
end
diff --git a/spec/lib/topics_filter_spec.rb b/spec/lib/topics_filter_spec.rb
index a10a37b506e..120983f6de7 100644
--- a/spec/lib/topics_filter_spec.rb
+++ b/spec/lib/topics_filter_spec.rb
@@ -192,6 +192,37 @@ RSpec.describe TopicsFilter do
.pluck(:id)
expect(ids).to contain_exactly(topic_by_u1_and_u2.id)
end
+
+ context "with whispers" do
+ fab!(:whisperer_group, :group)
+ fab!(:whisperer_user) { Fabricate(:user).tap { |u| whisperer_group.add(u) } }
+ fab!(:regular_user, :user)
+ fab!(:topic_with_whisper_only) do
+ Fabricate(:post, user: u1, post_type: Post.types[:whisper]).topic
+ end
+
+ before { SiteSetting.whispers_allowed_groups = "#{whisperer_group.id}" }
+
+ it "users:alice should not return topics where alice only whispered when viewed by non-whisperer" do
+ ids =
+ TopicsFilter
+ .new(guardian: Guardian.new(regular_user))
+ .filter_from_query_string("users:alice")
+ .pluck(:id)
+ expect(ids).not_to include(topic_with_whisper_only.id)
+ expect(ids).to include(topic_by_u1.id, topic_by_u1_and_u2.id)
+ end
+
+ it "group:group1 should not return topics where group members only whispered when viewed by non-whisperer" do
+ ids =
+ TopicsFilter
+ .new(guardian: Guardian.new(regular_user))
+ .filter_from_query_string("group:group1")
+ .pluck(:id)
+ expect(ids).not_to include(topic_with_whisper_only.id)
+ expect(ids).to include(topic_by_u1.id, topic_by_u1_and_u2.id)
+ end
+ end
end
describe "ordering by hot score" do