mirror of
https://github.com/discourse/discourse.git
synced 2024-11-24 11:12:23 +08:00
30990006a9
This reduces chances of errors where consumers of strings mutate inputs and reduces memory usage of the app. Test suite passes now, but there may be some stuff left, so we will run a few sites on a branch prior to merging
41 lines
895 B
Ruby
41 lines
895 B
Ruby
# frozen_string_literal: true
|
|
|
|
# This is used on a topic page
|
|
class TopicParticipantsSummary
|
|
attr_reader :topic, :options
|
|
|
|
def initialize(topic, options = {})
|
|
@topic = topic
|
|
@options = options
|
|
@user = options[:user]
|
|
end
|
|
|
|
def summary
|
|
top_participants.compact.map(&method(:new_topic_poster_for))
|
|
end
|
|
|
|
def new_topic_poster_for(user)
|
|
TopicPoster.new.tap do |topic_poster|
|
|
topic_poster.user = user
|
|
topic_poster.extras = 'latest' if is_latest_poster?(user)
|
|
end
|
|
end
|
|
|
|
def is_latest_poster?(user)
|
|
topic.last_post_user_id == user.id
|
|
end
|
|
|
|
def top_participants
|
|
user_ids.map { |id| avatar_lookup[id] }.compact.uniq.take(4)
|
|
end
|
|
|
|
def user_ids
|
|
return [] unless @user
|
|
[topic.user_id] + topic.allowed_user_ids - [@user.id]
|
|
end
|
|
|
|
def avatar_lookup
|
|
@avatar_lookup ||= options[:avatar_lookup] || AvatarLookup.new(user_ids)
|
|
end
|
|
end
|