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