2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-17 16:54:43 -06:00
|
|
|
# This is used on a topic page
|
2014-05-12 02:32:49 -05:00
|
|
|
class TopicParticipantsSummary
|
|
|
|
attr_reader :topic, :options
|
2019-11-15 14:11:09 -06:00
|
|
|
PARTICIPANT_COUNT = 5 # should match maxUserCount in topic list
|
2014-05-12 02:32:49 -05:00
|
|
|
|
|
|
|
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
|
2023-01-09 06:20:10 -06:00
|
|
|
topic_poster.extras = "latest" if is_latest_poster?(user)
|
2014-05-12 02:32:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def is_latest_poster?(user)
|
|
|
|
topic.last_post_user_id == user.id
|
|
|
|
end
|
|
|
|
|
|
|
|
def top_participants
|
2020-07-17 04:48:08 -05:00
|
|
|
user_ids.map { |id| user_lookup[id] }.compact.uniq.take(PARTICIPANT_COUNT)
|
2014-05-12 02:32:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def user_ids
|
|
|
|
return [] unless @user
|
|
|
|
[topic.user_id] + topic.allowed_user_ids - [@user.id]
|
|
|
|
end
|
|
|
|
|
2020-07-17 04:48:08 -05:00
|
|
|
def user_lookup
|
|
|
|
@user_lookup ||= options[:user_lookup] || UserLookup.new(user_ids)
|
2014-05-12 02:32:49 -05:00
|
|
|
end
|
|
|
|
end
|