2016-01-19 23:11:52 -05:00
|
|
|
# ViewModel used on Summary tab on User page
|
|
|
|
|
|
|
|
class UserSummary
|
|
|
|
|
2016-04-13 17:02:51 -04:00
|
|
|
MAX_SUMMARY_RESULTS = 6
|
2016-03-30 12:05:16 -04:00
|
|
|
MAX_BADGES = 6
|
2016-01-19 23:11:52 -05: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-27 19:12:12 -05:00
|
|
|
.visible
|
2016-01-19 23:11:52 -05:00
|
|
|
.where(user: @user)
|
2016-03-30 12:05:16 -04:00
|
|
|
.order('like_count DESC, created_at ASC')
|
2016-04-13 17:02:51 -04:00
|
|
|
.limit(MAX_SUMMARY_RESULTS)
|
2016-01-19 23:11:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def replies
|
|
|
|
Post
|
2016-04-20 16:58:30 -04:00
|
|
|
.joins(:topic)
|
|
|
|
.includes(:topic)
|
2016-01-19 23:11:52 -05:00
|
|
|
.secured(@guardian)
|
2016-01-27 19:12:12 -05:00
|
|
|
.merge(Topic.listable_topics.visible.secured(@guardian))
|
2016-01-19 23:11:52 -05:00
|
|
|
.where(user: @user)
|
|
|
|
.where('post_number > 1')
|
2016-03-30 12:05:16 -04:00
|
|
|
.order('posts.like_count DESC, posts.created_at ASC')
|
2016-04-13 17:02:51 -04:00
|
|
|
.limit(MAX_SUMMARY_RESULTS)
|
|
|
|
end
|
|
|
|
|
|
|
|
def links
|
|
|
|
TopicLink
|
2016-04-19 12:15:53 -04:00
|
|
|
.joins(:topic, :post)
|
2016-04-20 16:58:30 -04:00
|
|
|
.includes(:topic, :post)
|
2016-05-02 09:26:23 -04:00
|
|
|
.where('posts.post_type IN (?)', Topic.visible_post_types(@guardian && @guardian.user))
|
|
|
|
.merge(Topic.listable_topics.visible.secured(@guardian))
|
2016-04-13 17:02:51 -04:00
|
|
|
.where(user: @user)
|
2016-04-19 12:15:53 -04:00
|
|
|
.where(internal: false, reflection: false, quote: false)
|
2016-04-20 16:58:30 -04:00
|
|
|
.order('clicks DESC, topic_links.created_at ASC')
|
2016-04-13 17:02:51 -04:00
|
|
|
.limit(MAX_SUMMARY_RESULTS)
|
|
|
|
end
|
|
|
|
|
2016-05-04 16:47:48 -04:00
|
|
|
class UserWithCount < OpenStruct
|
2016-04-13 17:02:51 -04:00
|
|
|
include ActiveModel::SerializerSupport
|
|
|
|
end
|
|
|
|
|
|
|
|
def most_liked_by_users
|
2016-04-18 12:03:33 -04:00
|
|
|
likers = {}
|
2016-04-19 12:15:53 -04:00
|
|
|
UserAction.joins(:target_topic, :target_post)
|
2016-05-30 13:48:46 -04:00
|
|
|
.merge(Topic.listable_topics.visible.secured(@guardian))
|
2016-04-17 06:21:27 -04:00
|
|
|
.where(user: @user)
|
2016-04-13 17:02:51 -04:00
|
|
|
.where(action_type: UserAction::WAS_LIKED)
|
|
|
|
.group(:acting_user_id)
|
2016-04-19 12:15:53 -04:00
|
|
|
.order('COUNT(*) DESC')
|
2016-04-13 17:02:51 -04:00
|
|
|
.limit(MAX_SUMMARY_RESULTS)
|
2016-04-19 12:15:53 -04:00
|
|
|
.pluck('acting_user_id, COUNT(*)')
|
2016-04-18 12:03:33 -04:00
|
|
|
.each { |l| likers[l[0].to_s] = l[1] }
|
2016-04-13 17:02:51 -04:00
|
|
|
|
2016-04-18 12:03:33 -04:00
|
|
|
User.where(id: likers.keys)
|
2016-04-13 17:02:51 -04:00
|
|
|
.pluck(:id, :username, :name, :uploaded_avatar_id)
|
2016-04-18 12:03:33 -04:00
|
|
|
.map do |u|
|
2016-05-04 16:47:48 -04:00
|
|
|
UserWithCount.new(
|
2016-04-13 17:02:51 -04:00
|
|
|
id: u[0],
|
|
|
|
username: u[1],
|
|
|
|
name: u[2],
|
|
|
|
avatar_template: User.avatar_template(u[1], u[3]),
|
2016-05-04 16:47:48 -04:00
|
|
|
count: likers[u[0].to_s]
|
2016-04-13 17:02:51 -04:00
|
|
|
)
|
2016-05-04 16:47:48 -04:00
|
|
|
end.sort_by { |u| -u[:count] }
|
|
|
|
end
|
|
|
|
|
|
|
|
def most_liked_users
|
|
|
|
liked_users = {}
|
|
|
|
UserAction.joins(:target_topic, :target_post)
|
2016-05-30 13:48:46 -04:00
|
|
|
.merge(Topic.listable_topics.visible.secured(@guardian))
|
2016-05-04 16:47:48 -04:00
|
|
|
.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(*)')
|
|
|
|
.each { |l| liked_users[l[0].to_s] = l[1] }
|
|
|
|
|
|
|
|
User.where(id: liked_users.keys)
|
|
|
|
.pluck(:id, :username, :name, :uploaded_avatar_id)
|
|
|
|
.map do |u|
|
|
|
|
UserWithCount.new(
|
|
|
|
id: u[0],
|
|
|
|
username: u[1],
|
|
|
|
name: u[2],
|
|
|
|
avatar_template: User.avatar_template(u[1], u[3]),
|
|
|
|
count: liked_users[u[0].to_s]
|
|
|
|
)
|
|
|
|
end.sort_by { |u| -u[:count] }
|
|
|
|
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(*)')
|
|
|
|
.each { |r| replied_users[r[0].to_s] = r[1] }
|
|
|
|
|
|
|
|
User.where(id: replied_users.keys)
|
|
|
|
.pluck(:id, :username, :name, :uploaded_avatar_id)
|
|
|
|
.map do |u|
|
|
|
|
UserWithCount.new(
|
|
|
|
id: u[0],
|
|
|
|
username: u[1],
|
|
|
|
name: u[2],
|
|
|
|
avatar_template: User.avatar_template(u[1], u[3]),
|
|
|
|
count: replied_users[u[0].to_s]
|
|
|
|
)
|
|
|
|
end.sort_by { |u| -u[:count] }
|
2016-01-19 23:11:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def badges
|
2016-03-30 12:05:16 -04:00
|
|
|
@user.featured_user_badges(MAX_BADGES)
|
2016-01-19 23:11:52 -05:00
|
|
|
end
|
|
|
|
|
2016-05-09 09:51:43 -04:00
|
|
|
def user_id
|
|
|
|
@user.id
|
|
|
|
end
|
|
|
|
|
2016-01-19 23:11:52 -05:00
|
|
|
def user_stat
|
|
|
|
@user.user_stat
|
|
|
|
end
|
|
|
|
|
2016-04-13 17:02:51 -04:00
|
|
|
def bookmark_count
|
|
|
|
UserAction
|
|
|
|
.where(user: @user)
|
|
|
|
.where(action_type: UserAction::BOOKMARK)
|
|
|
|
.count
|
|
|
|
end
|
|
|
|
|
2016-03-30 12:05:16 -04:00
|
|
|
delegate :likes_given,
|
|
|
|
:likes_received,
|
|
|
|
:days_visited,
|
|
|
|
:posts_read_count,
|
|
|
|
:topic_count,
|
|
|
|
:post_count,
|
|
|
|
:time_read,
|
2016-01-19 23:11:52 -05:00
|
|
|
to: :user_stat
|
|
|
|
|
|
|
|
end
|