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-01-20 12:11:52 +08:00
|
|
|
.includes(:user, :category)
|
2016-04-14 05:02:51 +08:00
|
|
|
.limit(MAX_SUMMARY_RESULTS)
|
2016-01-20 12:11:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def replies
|
|
|
|
Post
|
|
|
|
.secured(@guardian)
|
2016-03-31 00:05:16 +08:00
|
|
|
.includes(:user, topic: :category)
|
2016-01-28 08:12:12 +08:00
|
|
|
.references(:topic)
|
|
|
|
.merge(Topic.listable_topics.visible.secured(@guardian))
|
2016-01-20 12:11:52 +08:00
|
|
|
.where(user: @user)
|
|
|
|
.where('post_number > 1')
|
|
|
|
.where('topics.archetype <> ?', Archetype.private_message)
|
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)
|
|
|
|
.where('topics.archetype <> ?', Archetype.private_message)
|
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-14 05:02:51 +08:00
|
|
|
.order('clicks DESC, created_at ASC')
|
|
|
|
.limit(MAX_SUMMARY_RESULTS)
|
|
|
|
end
|
|
|
|
|
|
|
|
class LikedByUser < OpenStruct
|
|
|
|
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)
|
|
|
|
.where('topics.archetype <> ?', Archetype.private_message)
|
2016-04-17 18:21:27 +08:00
|
|
|
.where(user: @user)
|
2016-04-14 05:02:51 +08:00
|
|
|
.where(action_type: UserAction::WAS_LIKED)
|
|
|
|
.group(:acting_user_id)
|
2016-04-20 00:15:53 +08:00
|
|
|
.order('COUNT(*) DESC')
|
2016-04-14 05:02:51 +08:00
|
|
|
.limit(MAX_SUMMARY_RESULTS)
|
2016-04-20 00:15:53 +08:00
|
|
|
.pluck('acting_user_id, COUNT(*)')
|
2016-04-19 00:03:33 +08:00
|
|
|
.each { |l| likers[l[0].to_s] = l[1] }
|
2016-04-14 05:02:51 +08:00
|
|
|
|
2016-04-19 00:03:33 +08:00
|
|
|
User.where(id: likers.keys)
|
2016-04-14 05:02:51 +08:00
|
|
|
.pluck(:id, :username, :name, :uploaded_avatar_id)
|
2016-04-19 00:03:33 +08:00
|
|
|
.map do |u|
|
2016-04-14 05:02:51 +08:00
|
|
|
LikedByUser.new(
|
|
|
|
id: u[0],
|
|
|
|
username: u[1],
|
|
|
|
name: u[2],
|
|
|
|
avatar_template: User.avatar_template(u[1], u[3]),
|
2016-04-19 00:03:33 +08:00
|
|
|
likes: likers[u[0].to_s]
|
2016-04-14 05:02:51 +08:00
|
|
|
)
|
2016-04-19 00:03:33 +08:00
|
|
|
end.sort_by { |u| -u[:likes] }
|
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
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2016-03-31 00:05:16 +08:00
|
|
|
delegate :likes_given,
|
|
|
|
:likes_received,
|
|
|
|
:days_visited,
|
|
|
|
:posts_read_count,
|
|
|
|
:topic_count,
|
|
|
|
:post_count,
|
|
|
|
:time_read,
|
2016-01-20 12:11:52 +08:00
|
|
|
to: :user_stat
|
|
|
|
|
|
|
|
end
|