diff --git a/app/models/topic.rb b/app/models/topic.rb index db9dd21d3c6..c9713bef3e2 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -392,9 +392,10 @@ class Topic < ActiveRecord::Base end end - def self.visible_post_types(viewed_by = nil) + def self.visible_post_types(viewed_by = nil, include_moderator_actions: true) types = Post.types - result = [types[:regular], types[:moderator_action], types[:small_action]] + result = [types[:regular]] + result += [types[:moderator_action], types[:small_action]] if include_moderator_actions result << types[:whisper] if viewed_by&.staff? result end diff --git a/app/models/user_summary.rb b/app/models/user_summary.rb index 1cd0b020684..f30507117d0 100644 --- a/app/models/user_summary.rb +++ b/app/models/user_summary.rb @@ -25,12 +25,7 @@ class UserSummary end def replies - Post - .joins(:topic) - .includes(:topic) - .secured(@guardian) - .merge(Topic.listable_topics.visible.secured(@guardian)) - .where(user: @user) + post_query .where('post_number > 1') .order('posts.like_count DESC, posts.created_at DESC') .limit(MAX_SUMMARY_RESULTS) @@ -88,13 +83,8 @@ class UserSummary def most_replied_to_users replied_users = {} - Post - .joins(:topic) + post_query .joins('JOIN posts replies ON posts.topic_id = replies.topic_id AND posts.reply_to_post_number = replies.post_number') - .includes(:topic) - .secured(@guardian) - .merge(Topic.listable_topics.visible.secured(@guardian)) - .where(user: @user) .where('replies.user_id <> ?', @user.id) .group('replies.user_id') .order('COUNT(*) DESC') @@ -135,13 +125,7 @@ class UserSummary end def top_categories - post_count_query = Post - .joins(:topic) - .includes(:topic) - .secured(@guardian) - .merge(Topic.listable_topics.visible.secured(@guardian)) - .where(user: @user) - .group('topics.category_id') + post_count_query = post_query.group('topics.category_id') top_categories = {} @@ -211,4 +195,13 @@ protected end.compact.sort_by { |u| -u[:count] } end + def post_query + Post + .joins(:topic) + .includes(:topic) + .where('posts.post_type IN (?)', Topic.visible_post_types(@guardian&.user, include_moderator_actions: false)) + .merge(Topic.listable_topics.visible.secured(@guardian)) + .where(user: @user) + end + end diff --git a/spec/models/user_summary_spec.rb b/spec/models/user_summary_spec.rb index 8847fc2a3a0..4126632075f 100644 --- a/spec/models/user_summary_spec.rb +++ b/spec/models/user_summary_spec.rb @@ -78,4 +78,19 @@ describe UserSummary do expect(summary.top_categories.length).to eq(UserSummary::MAX_SUMMARY_RESULTS) expect(summary.top_categories.first[:id]).to eq(top_category.id) end + + it "excludes moderator action posts" do + topic = create_post.topic + user = topic.user + create_post(user: user, topic: topic) + Fabricate(:small_action, topic: topic, user: user) + + summary = UserSummary.new(user, Guardian.new) + + expect(summary.topics.length).to eq(1) + expect(summary.replies.length).to eq(1) + expect(summary.top_categories.length).to eq(1) + expect(summary.top_categories.first[:topic_count]).to eq(1) + expect(summary.top_categories.first[:post_count]).to eq(1) + end end