FIX: exclude moderator_action post for reply count in user summary. (#14991)

Previously, incorrect reply counts are displayed in the "top categories" section of the user summary page since we included the `moderator_action` and `small_action` post types.

Co-authored-by: Alan Guo Xiang Tan <gxtan1990@gmail.com>
This commit is contained in:
Vinoth Kannan 2021-11-18 13:42:03 +05:30 committed by GitHub
parent 20f5474be9
commit fc1c76cfcc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 21 deletions

View File

@ -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

View File

@ -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

View File

@ -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