A platform for community discussion. Free, open, simple.
PERF: topic_view participant post count: don't send back ID list (#10210)
Kane York8ddd45d52461
diff --git a/lib/guardian/topic_guardian.rb b/lib/guardian/topic_guardian.rbindex 842f44c0cf0..387e7c855b6 100644--- a/lib/guardian/topic_guardian.rb+++ b/lib/guardian/topic_guardian.rb@@ -31,6 +31,10 @@ module TopicGuardianis_staff? && SiteSetting.enable_whispers?end+ def can_see_whispers?(_topic)+ is_staff?+ end+def can_publish_topic?(topic, category)is_staff? && can_see?(topic) && can_create_topic?(category)enddiff --git a/lib/topic_view.rb b/lib/topic_view.rbindex aa0c46aa1a9..81b4449c4ed 100644--- a/lib/topic_view.rb+++ b/lib/topic_view.rb@@ -366,21 +366,24 @@ class TopicViewif is_mega_topic?{}else- post_ids = unfiltered_post_ids-- return {} if post_ids.blank?+ post_types = [Post.types[:regular], Post.types[:moderator_action]]+ if @guardian.can_see_whispers?(@topic)+ post_types << Post.types[:whisper]+ endsql = <<~SQLSELECT user_id, count(*) AS count_allFROM posts- WHERE id in (:post_ids)+ WHERE topic_id = :topic_id+ AND post_type IN (:post_types)AND user_id IS NOT NULL+ AND posts.deleted_at IS NULLGROUP BY user_idORDER BY count_all DESCLIMIT #{MAX_PARTICIPANTS}SQL- Hash[*DB.query_single(sql, post_ids: post_ids)]+ Hash[*DB.query_single(sql, topic_id: @topic.id, post_types: post_types)]endendenddiff --git a/spec/components/topic_view_spec.rb b/spec/components/topic_view_spec.rbindex 624e9a3b4a1..0d51b1ef3fa 100644--- a/spec/components/topic_view_spec.rb+++ b/spec/components/topic_view_spec.rb@@ -287,7 +287,7 @@ describe TopicView doit 'returns the two posters with their appropriate counts' doFabricate(:post, topic: topic, user: evil_trout, post_type: Post.types[:whisper])- expect(topic_view.post_counts_by_user.to_a).to match_array([[first_poster.id, 2], [evil_trout.id, 2]])+ expect(TopicView.new(topic.id, admin).post_counts_by_user.to_a).to match_array([[first_poster.id, 2], [evil_trout.id, 2]])expect(TopicView.new(topic.id, first_poster).post_counts_by_user.to_a).to match_array([[first_poster.id, 2], [evil_trout.id, 1]])end