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:
Neil Lalonde
2018-07-18 16:37:50 -04:00
parent 2dc3a50dac
commit afc94ac9e4
8 changed files with 130 additions and 3 deletions

View File

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