2016-01-20 12:11:52 +08:00
|
|
|
# ViewModel used on Summary tab on User page
|
|
|
|
|
|
|
|
class UserSummary
|
|
|
|
|
2016-04-14 05:02:51 +08:00
|
|
|
MAX_SUMMARY_RESULTS = 6
|
2016-03-31 00:05:16 +08:00
|
|
|
MAX_BADGES = 6
|
2016-01-20 12:11:52 +08:00
|
|
|
|
|
|
|
alias :read_attribute_for_serialization :send
|
|
|
|
|
|
|
|
def initialize(user, guardian)
|
|
|
|
@user = user
|
|
|
|
@guardian = guardian
|
|
|
|
end
|
|
|
|
|
|
|
|
def topics
|
|
|
|
Topic
|
|
|
|
.secured(@guardian)
|
|
|
|
.listable_topics
|
2016-01-28 08:12:12 +08:00
|
|
|
.visible
|
2016-01-20 12:11:52 +08:00
|
|
|
.where(user: @user)
|
2016-03-31 00:05:16 +08:00
|
|
|
.order('like_count DESC, created_at ASC')
|
2016-04-14 05:02:51 +08:00
|
|
|
.limit(MAX_SUMMARY_RESULTS)
|
2016-01-20 12:11:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def replies
|
|
|
|
Post
|
2016-04-21 04:58:30 +08:00
|
|
|
.joins(:topic)
|
|
|
|
.includes(:topic)
|
2016-01-20 12:11:52 +08:00
|
|
|
.secured(@guardian)
|
2016-01-28 08:12:12 +08:00
|
|
|
.merge(Topic.listable_topics.visible.secured(@guardian))
|
2016-01-20 12:11:52 +08:00
|
|
|
.where(user: @user)
|
|
|
|
.where('post_number > 1')
|
2016-03-31 00:05:16 +08:00
|
|
|
.order('posts.like_count DESC, posts.created_at ASC')
|
2016-04-14 05:02:51 +08:00
|
|
|
.limit(MAX_SUMMARY_RESULTS)
|
|
|
|
end
|
|
|
|
|
|
|
|
def links
|
|
|
|
TopicLink
|
2016-04-20 00:15:53 +08:00
|
|
|
.joins(:topic, :post)
|
2016-04-21 04:58:30 +08:00
|
|
|
.includes(:topic, :post)
|
2016-05-02 21:26:23 +08:00
|
|
|
.where('posts.post_type IN (?)', Topic.visible_post_types(@guardian && @guardian.user))
|
|
|
|
.merge(Topic.listable_topics.visible.secured(@guardian))
|
2016-04-14 05:02:51 +08:00
|
|
|
.where(user: @user)
|
2016-04-20 00:15:53 +08:00
|
|
|
.where(internal: false, reflection: false, quote: false)
|
2016-04-21 04:58:30 +08:00
|
|
|
.order('clicks DESC, topic_links.created_at ASC')
|
2016-04-14 05:02:51 +08:00
|
|
|
.limit(MAX_SUMMARY_RESULTS)
|
|
|
|
end
|
|
|
|
|
2016-05-05 04:47:48 +08:00
|
|
|
class UserWithCount < OpenStruct
|
2016-04-14 05:02:51 +08:00
|
|
|
include ActiveModel::SerializerSupport
|
|
|
|
end
|
|
|
|
|
|
|
|
def most_liked_by_users
|
2016-04-19 00:03:33 +08:00
|
|
|
likers = {}
|
2016-04-20 00:15:53 +08:00
|
|
|
UserAction.joins(:target_topic, :target_post)
|
2017-07-28 09:20:09 +08:00
|
|
|
.merge(Topic.listable_topics.visible.secured(@guardian))
|
|
|
|
.where(user: @user)
|
|
|
|
.where(action_type: UserAction::WAS_LIKED)
|
|
|
|
.group(:acting_user_id)
|
|
|
|
.order('COUNT(*) DESC')
|
|
|
|
.limit(MAX_SUMMARY_RESULTS)
|
|
|
|
.pluck('acting_user_id, COUNT(*)')
|
2018-10-19 03:49:34 +08:00
|
|
|
.each { |l| likers[l[0]] = l[1] }
|
2016-04-14 05:02:51 +08:00
|
|
|
|
2018-10-19 03:49:34 +08:00
|
|
|
user_counts(likers)
|
2016-05-05 04:47:48 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def most_liked_users
|
|
|
|
liked_users = {}
|
|
|
|
UserAction.joins(:target_topic, :target_post)
|
2017-07-28 09:20:09 +08:00
|
|
|
.merge(Topic.listable_topics.visible.secured(@guardian))
|
|
|
|
.where(action_type: UserAction::WAS_LIKED)
|
|
|
|
.where(acting_user_id: @user.id)
|
|
|
|
.group(:user_id)
|
|
|
|
.order('COUNT(*) DESC')
|
|
|
|
.limit(MAX_SUMMARY_RESULTS)
|
|
|
|
.pluck('user_actions.user_id, COUNT(*)')
|
2018-10-19 03:49:34 +08:00
|
|
|
.each { |l| liked_users[l[0]] = l[1] }
|
2016-05-05 04:47:48 +08:00
|
|
|
|
2018-10-19 03:49:34 +08:00
|
|
|
user_counts(liked_users)
|
2016-05-05 04:47:48 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
REPLY_ACTIONS ||= [UserAction::RESPONSE, UserAction::QUOTE, UserAction::MENTION]
|
|
|
|
|
|
|
|
def most_replied_to_users
|
|
|
|
replied_users = {}
|
|
|
|
|
|
|
|
Post
|
|
|
|
.joins(:topic)
|
|
|
|
.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')
|
|
|
|
.limit(MAX_SUMMARY_RESULTS)
|
|
|
|
.pluck('replies.user_id, COUNT(*)')
|
2018-10-19 03:49:34 +08:00
|
|
|
.each { |r| replied_users[r[0]] = r[1] }
|
2016-05-05 04:47:48 +08:00
|
|
|
|
2018-10-19 03:49:34 +08:00
|
|
|
user_counts(replied_users)
|
2016-01-20 12:11:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def badges
|
2016-03-31 00:05:16 +08:00
|
|
|
@user.featured_user_badges(MAX_BADGES)
|
2016-01-20 12:11:52 +08:00
|
|
|
end
|
|
|
|
|
2016-05-09 21:51:43 +08:00
|
|
|
def user_id
|
|
|
|
@user.id
|
|
|
|
end
|
|
|
|
|
2016-01-20 12:11:52 +08:00
|
|
|
def user_stat
|
|
|
|
@user.user_stat
|
|
|
|
end
|
|
|
|
|
2016-04-14 05:02:51 +08:00
|
|
|
def bookmark_count
|
|
|
|
UserAction
|
|
|
|
.where(user: @user)
|
|
|
|
.where(action_type: UserAction::BOOKMARK)
|
|
|
|
.count
|
|
|
|
end
|
|
|
|
|
2017-11-15 05:39:07 +08:00
|
|
|
def recent_time_read
|
|
|
|
@user.recent_time_read
|
|
|
|
end
|
|
|
|
|
2018-07-19 04:37:50 +08:00
|
|
|
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
|
|
|
|
|
2016-03-31 00:05:16 +08:00
|
|
|
delegate :likes_given,
|
|
|
|
:likes_received,
|
|
|
|
:days_visited,
|
2017-11-18 06:53:22 +08:00
|
|
|
:topics_entered,
|
2016-03-31 00:05:16 +08:00
|
|
|
:posts_read_count,
|
|
|
|
:topic_count,
|
|
|
|
:post_count,
|
|
|
|
:time_read,
|
2016-01-20 12:11:52 +08:00
|
|
|
to: :user_stat
|
|
|
|
|
2018-10-19 03:49:34 +08:00
|
|
|
protected
|
|
|
|
|
|
|
|
def user_counts(user_hash)
|
|
|
|
user_ids = user_hash.keys
|
|
|
|
|
|
|
|
lookup = AvatarLookup.new(user_ids)
|
|
|
|
user_ids.map do |user_id|
|
|
|
|
UserWithCount.new(
|
|
|
|
lookup[user_id].attributes.merge(count: user_hash[user_id])
|
|
|
|
)
|
|
|
|
end.sort_by { |u| -u[:count] }
|
|
|
|
end
|
|
|
|
|
2016-01-20 12:11:52 +08:00
|
|
|
end
|