mirror of
https://github.com/discourse/discourse.git
synced 2025-06-06 03:06:53 +08:00
FEATURE: add a Top Categories section to the user summary page, showing the categories in which a user has the most activity
This commit is contained in:
@ -155,6 +155,56 @@ class UserSummary
|
||||
@user.recent_time_read
|
||||
end
|
||||
|
||||
class CategoryWithCounts < OpenStruct
|
||||
include ActiveModel::SerializerSupport
|
||||
KEYS = [:id, :name, :color, :text_color, :slug, :read_restricted, :parent_category_id]
|
||||
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')
|
||||
.order('COUNT(*) DESC')
|
||||
|
||||
top_categories = {}
|
||||
|
||||
Category.where(id: post_count_query.limit(MAX_SUMMARY_RESULTS).pluck('category_id'))
|
||||
.pluck(:id, :name, :color, :text_color, :slug, :read_restricted, :parent_category_id)
|
||||
.each do |c|
|
||||
top_categories[c[0].to_i] = CategoryWithCounts.new(
|
||||
Hash[CategoryWithCounts::KEYS.zip(c)].merge(
|
||||
topic_count: 0,
|
||||
post_count: 0
|
||||
)
|
||||
)
|
||||
end
|
||||
|
||||
post_count_query.where('post_number > 1')
|
||||
.where('topics.category_id in (?)', top_categories.keys)
|
||||
.pluck('category_id, COUNT(*)')
|
||||
.each do |r|
|
||||
top_categories[r[0].to_i].post_count = r[1]
|
||||
end
|
||||
|
||||
Topic.listable_topics.visible.secured(@guardian)
|
||||
.where('topics.category_id in (?)', top_categories.keys)
|
||||
.where(user: @user)
|
||||
.group('topics.category_id')
|
||||
.order('COUNT(*) DESC')
|
||||
.pluck('category_id, COUNT(*)')
|
||||
.each do |r|
|
||||
top_categories[r[0].to_i].topic_count = r[1]
|
||||
end
|
||||
|
||||
top_categories.values.sort_by do |r|
|
||||
-(r[:post_count] + r[:topic_count])
|
||||
end
|
||||
end
|
||||
|
||||
delegate :likes_given,
|
||||
:likes_received,
|
||||
:days_visited,
|
||||
|
Reference in New Issue
Block a user