mirror of
https://github.com/discourse/discourse.git
synced 2025-03-23 03:16:50 +08:00
FEATURE: primary group class on avatars in topic list
This commit is contained in:
parent
aa2c527c60
commit
476ae57af3
@ -98,13 +98,22 @@ TopicList.reopenClass({
|
|||||||
if (!result) { return; }
|
if (!result) { return; }
|
||||||
|
|
||||||
// Stitch together our side loaded data
|
// Stitch together our side loaded data
|
||||||
|
|
||||||
const categories = Discourse.Category.list(),
|
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) {
|
return result.topic_list.topics.map(function (t) {
|
||||||
t.category = categories.findBy('id', t.category_id);
|
t.category = categories.findBy('id', t.category_id);
|
||||||
t.posters.forEach(function(p) {
|
t.posters.forEach(function(p) {
|
||||||
p.user = users[p.user_id];
|
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) {
|
if (t.participants) {
|
||||||
t.participants.forEach(function(p) {
|
t.participants.forEach(function(p) {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
<td class='posters'>
|
<td class='posters'>
|
||||||
{{#each posters as |poster|}}
|
{{#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}}
|
{{/each}}
|
||||||
</td>
|
</td>
|
||||||
|
@ -816,7 +816,7 @@ SQL
|
|||||||
update_column(:like_count, Post.where(topic_id: id).sum(:like_count))
|
update_column(:like_count, Post.where(topic_id: id).sum(:like_count))
|
||||||
end
|
end
|
||||||
|
|
||||||
def posters_summary(options = {})
|
def posters_summary(options = {}) # avatar lookup in options
|
||||||
@posters_summary ||= TopicPostersSummary.new(self, options).summary
|
@posters_summary ||= TopicPostersSummary.new(self, options).summary
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
require_dependency 'avatar_lookup'
|
require_dependency 'avatar_lookup'
|
||||||
|
require_dependency 'primary_group_lookup'
|
||||||
|
|
||||||
class TopicList
|
class TopicList
|
||||||
include ActiveModel::Serialization
|
include ActiveModel::Serialization
|
||||||
@ -108,7 +109,11 @@ class TopicList
|
|||||||
ft.user_data.post_action_data = {post_action_type => actions}
|
ft.user_data.post_action_data = {post_action_type => actions}
|
||||||
end
|
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.participants = ft.participants_summary(avatar_lookup: avatar_lookup, user: @current_user)
|
||||||
ft.topic_list = self
|
ft.topic_list = self
|
||||||
end
|
end
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# This is used on a topic page
|
||||||
class TopicParticipantsSummary
|
class TopicParticipantsSummary
|
||||||
attr_reader :topic, :options
|
attr_reader :topic, :options
|
||||||
|
|
||||||
|
@ -1,14 +1,15 @@
|
|||||||
class TopicPoster < OpenStruct
|
class TopicPoster < OpenStruct
|
||||||
include ActiveModel::Serialization
|
include ActiveModel::Serialization
|
||||||
|
|
||||||
attr_accessor :user, :description, :extras, :id
|
attr_accessor :user, :description, :extras, :id, :primary_group
|
||||||
|
|
||||||
def attributes
|
def attributes
|
||||||
{
|
{
|
||||||
'user' => user,
|
'user' => user,
|
||||||
'description' => description,
|
'description' => description,
|
||||||
'extras' => extras,
|
'extras' => extras,
|
||||||
'id' => id
|
'id' => id,
|
||||||
|
'primary_group' => primary_group
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
# This is used in topic lists
|
||||||
class TopicPostersSummary
|
class TopicPostersSummary
|
||||||
attr_reader :topic, :options
|
attr_reader :topic, :options
|
||||||
|
|
||||||
@ -16,6 +17,7 @@ class TopicPostersSummary
|
|||||||
TopicPoster.new.tap do |topic_poster|
|
TopicPoster.new.tap do |topic_poster|
|
||||||
topic_poster.user = user
|
topic_poster.user = user
|
||||||
topic_poster.description = descriptions_for(user)
|
topic_poster.description = descriptions_for(user)
|
||||||
|
topic_poster.primary_group = primary_group_lookup[user.id]
|
||||||
if topic.last_post_user_id == user.id
|
if topic.last_post_user_id == user.id
|
||||||
topic_poster.extras = 'latest'
|
topic_poster.extras = 'latest'
|
||||||
topic_poster.extras << ' single' if user_ids.uniq.size == 1
|
topic_poster.extras << ' single' if user_ids.uniq.size == 1
|
||||||
@ -74,4 +76,8 @@ class TopicPostersSummary
|
|||||||
def avatar_lookup
|
def avatar_lookup
|
||||||
@avatar_lookup ||= options[:avatar_lookup] || AvatarLookup.new(user_ids)
|
@avatar_lookup ||= options[:avatar_lookup] || AvatarLookup.new(user_ids)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def primary_group_lookup
|
||||||
|
@primary_group_lookup ||= options[:primary_group_lookup] || PrimaryGroupLookup.new(user_ids)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
3
app/serializers/primary_group_serializer.rb
Normal file
3
app/serializers/primary_group_serializer.rb
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
class PrimaryGroupSerializer < ApplicationSerializer
|
||||||
|
attributes :id, :name, :flair_url, :flair_bg_color, :flair_color
|
||||||
|
end
|
@ -1,4 +1,5 @@
|
|||||||
class TopicPosterSerializer < ApplicationSerializer
|
class TopicPosterSerializer < ApplicationSerializer
|
||||||
attributes :extras, :description
|
attributes :extras, :description
|
||||||
has_one :user, serializer: BasicUserSerializer
|
has_one :user, serializer: BasicUserSerializer
|
||||||
|
has_one :primary_group, serializer: PrimaryGroupSerializer
|
||||||
end
|
end
|
||||||
|
37
lib/primary_group_lookup.rb
Normal file
37
lib/primary_group_lookup.rb
Normal 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
|
Loading…
x
Reference in New Issue
Block a user