FEATURE: primary group class on avatars in topic list

This commit is contained in:
Neil Lalonde 2017-02-17 17:54:43 -05:00
parent aa2c527c60
commit 476ae57af3
10 changed files with 69 additions and 6 deletions

View File

@ -98,13 +98,22 @@ TopicList.reopenClass({
if (!result) { return; }
// Stitch together our side loaded data
const categories = Discourse.Category.list(),
users = Model.extractByKey(result.users, Discourse.User);
users = Model.extractByKey(result.users, Discourse.User),
groups = Model.extractByKey(result.primary_groups, Ember.Object);
return result.topic_list.topics.map(function (t) {
t.category = categories.findBy('id', t.category_id);
t.posters.forEach(function(p) {
p.user = users[p.user_id];
p.extraClasses = p.extras;
if (p.primary_group_id) {
p.primary_group = groups[p.primary_group_id];
if (p.primary_group) {
p.extraClasses += ` group-${p.primary_group.name}`;
}
}
});
if (t.participants) {
t.participants.forEach(function(p) {

View File

@ -1,5 +1,5 @@
<td class='posters'>
{{#each posters as |poster|}}
<a href="{{poster.user.path}}" data-user-card="{{poster.user.username}}" class="{{poster.extras}}">{{avatar poster avatarTemplatePath="user.avatar_template" usernamePath="user.username" imageSize="small"}}</a>
<a href="{{poster.user.path}}" data-user-card="{{poster.user.username}}" class="{{poster.extraClasses}}">{{avatar poster avatarTemplatePath="user.avatar_template" usernamePath="user.username" imageSize="small"}}</a>
{{/each}}
</td>

View File

@ -816,7 +816,7 @@ SQL
update_column(:like_count, Post.where(topic_id: id).sum(:like_count))
end
def posters_summary(options = {})
def posters_summary(options = {}) # avatar lookup in options
@posters_summary ||= TopicPostersSummary.new(self, options).summary
end

View File

@ -1,4 +1,5 @@
require_dependency 'avatar_lookup'
require_dependency 'primary_group_lookup'
class TopicList
include ActiveModel::Serialization
@ -108,7 +109,11 @@ class TopicList
ft.user_data.post_action_data = {post_action_type => actions}
end
ft.posters = ft.posters_summary(avatar_lookup: avatar_lookup)
ft.posters = ft.posters_summary(
avatar_lookup: avatar_lookup,
primary_group_lookup: PrimaryGroupLookup.new(user_ids)
)
ft.participants = ft.participants_summary(avatar_lookup: avatar_lookup, user: @current_user)
ft.topic_list = self
end

View File

@ -1,3 +1,4 @@
# This is used on a topic page
class TopicParticipantsSummary
attr_reader :topic, :options

View File

@ -1,14 +1,15 @@
class TopicPoster < OpenStruct
include ActiveModel::Serialization
attr_accessor :user, :description, :extras, :id
attr_accessor :user, :description, :extras, :id, :primary_group
def attributes
{
'user' => user,
'description' => description,
'extras' => extras,
'id' => id
'id' => id,
'primary_group' => primary_group
}
end

View File

@ -1,3 +1,4 @@
# This is used in topic lists
class TopicPostersSummary
attr_reader :topic, :options
@ -16,6 +17,7 @@ class TopicPostersSummary
TopicPoster.new.tap do |topic_poster|
topic_poster.user = user
topic_poster.description = descriptions_for(user)
topic_poster.primary_group = primary_group_lookup[user.id]
if topic.last_post_user_id == user.id
topic_poster.extras = 'latest'
topic_poster.extras << ' single' if user_ids.uniq.size == 1
@ -74,4 +76,8 @@ class TopicPostersSummary
def avatar_lookup
@avatar_lookup ||= options[:avatar_lookup] || AvatarLookup.new(user_ids)
end
def primary_group_lookup
@primary_group_lookup ||= options[:primary_group_lookup] || PrimaryGroupLookup.new(user_ids)
end
end

View File

@ -0,0 +1,3 @@
class PrimaryGroupSerializer < ApplicationSerializer
attributes :id, :name, :flair_url, :flair_bg_color, :flair_color
end

View File

@ -1,4 +1,5 @@
class TopicPosterSerializer < ApplicationSerializer
attributes :extras, :description
has_one :user, serializer: BasicUserSerializer
has_one :primary_group, serializer: PrimaryGroupSerializer
end

View File

@ -0,0 +1,37 @@
class PrimaryGroupLookup
def initialize(user_ids=[])
@user_ids = user_ids.tap(&:compact!).tap(&:uniq!).tap(&:flatten!)
end
# Lookup primary group for a given user id
def [](user_id)
users[user_id]
end
private
def self.lookup_columns
@lookup_columns ||= %i{id name flair_url flair_bg_color flair_color}
end
def users
@users ||= user_lookup_hash
end
def user_lookup_hash
users_with_primary_group = User.where(id: @user_ids)
.where.not(primary_group_id: nil)
.select(:id, :primary_group_id)
group_lookup = {}
group_ids = users_with_primary_group.map(&:primary_group_id).compact
Group.where(id: group_ids).select(self.class.lookup_columns)
.each { |g| group_lookup[g.id] = g }
hash = {}
users_with_primary_group.each do |u|
hash[u.id] = group_lookup[u.primary_group_id]
end
hash
end
end