add 'Most Liked' and 'Most Replied To' columns in user summary

This commit is contained in:
Régis Hanol 2016-05-04 22:47:48 +02:00
parent b1c4c8a5d0
commit c775ea7b5e
4 changed files with 106 additions and 8 deletions

View File

@ -93,6 +93,26 @@
{{/if}}
</div>
<div class='top-sub-section likes-section pull-right'>
<h3 class='stats-title'>{{i18n "user.summary.most_replied_to_users"}}</h3>
{{#if model.most_replied_to_users.length}}
<ul>
{{#each user in model.most_replied_to_users}}
<li>
{{#user-info user=user}}
{{fa-icon "reply"}}
<span class='replies'>{{user.count}}</span>
{{/user-info}}
</li>
{{/each}}
</ul>
{{else}}
<p>{{i18n "user.summary.no_likes"}}</p>
{{/if}}
</div>
</div>
<div class='top-section'>
<div class='top-sub-section likes-section pull-left'>
<h3 class='stats-title'>{{i18n "user.summary.most_liked_by"}}</h3>
{{#if model.most_liked_by_users.length}}
<ul>
@ -100,7 +120,24 @@
<li>
{{#user-info user=user}}
{{fa-icon "heart"}}
<span class='likes'>{{user.likes}}</span>
<span class='likes'>{{user.count}}</span>
{{/user-info}}
</li>
{{/each}}
</ul>
{{else}}
<p>{{i18n "user.summary.no_likes"}}</p>
{{/if}}
</div>
<div class='top-sub-section likes-section pull-right'>
<h3 class='stats-title'>{{i18n "user.summary.most_liked_users"}}</h3>
{{#if model.most_liked_users.length}}
<ul>
{{#each user in model.most_liked_users}}
<li>
{{#user-info user=user}}
{{fa-icon "heart"}}
<span class='likes'>{{user.count}}</span>
{{/user-info}}
</li>
{{/each}}

View File

@ -46,7 +46,7 @@ class UserSummary
.limit(MAX_SUMMARY_RESULTS)
end
class LikedByUser < OpenStruct
class UserWithCount < OpenStruct
include ActiveModel::SerializerSupport
end
@ -65,14 +65,71 @@ class UserSummary
User.where(id: likers.keys)
.pluck(:id, :username, :name, :uploaded_avatar_id)
.map do |u|
LikedByUser.new(
UserWithCount.new(
id: u[0],
username: u[1],
name: u[2],
avatar_template: User.avatar_template(u[1], u[3]),
likes: likers[u[0].to_s]
count: likers[u[0].to_s]
)
end.sort_by { |u| -u[:likes] }
end.sort_by { |u| -u[:count] }
end
def most_liked_users
liked_users = {}
UserAction.joins(:target_topic, :target_post)
.where('topics.archetype <> ?', Archetype.private_message)
.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] }
end
def badges

View File

@ -18,14 +18,16 @@ class UserSummarySerializer < ApplicationSerializer
end
end
class MostLikedByUserSerializer < BasicUserSerializer
attributes :likes, :name
class UserWithCountSerializer < BasicUserSerializer
attributes :count, :name
end
has_many :topics, serializer: TopicSerializer
has_many :replies, serializer: ReplySerializer, embed: :object
has_many :links, serializer: LinkSerializer, embed: :object
has_many :most_liked_by_users, serializer: MostLikedByUserSerializer, embed: :object
has_many :most_liked_by_users, serializer: UserWithCountSerializer, embed: :object
has_many :most_liked_users, serializer: UserWithCountSerializer, embed: :object
has_many :most_replied_to_users, serializer: UserWithCountSerializer, embed: :object
has_many :badges, serializer: UserBadgeSerializer, embed: :object
attributes :likes_given,

View File

@ -773,6 +773,8 @@ en:
top_links: "Top Links"
no_links: "No links yet."
most_liked_by: "Most Liked By"
most_liked_users: "Most Liked"
most_replied_to_users: "Most Replied To"
no_likes: "No likes yet."