2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-17 16:54:43 -06:00
|
|
|
# This is used in topic lists
|
2013-05-23 01:21:19 -05:00
|
|
|
class TopicPostersSummary
|
2019-06-05 03:22:47 -05:00
|
|
|
# localization is fast, but this allows us to avoid
|
|
|
|
# calling it in a loop which adds up
|
|
|
|
def self.translations
|
|
|
|
{
|
|
|
|
original_poster: I18n.t(:original_poster),
|
|
|
|
most_recent_poster: I18n.t(:most_recent_poster),
|
2021-02-02 03:50:04 -06:00
|
|
|
frequent_poster: I18n.t(:frequent_poster),
|
|
|
|
joiner: I18n.t(:poster_description_joiner),
|
2019-06-05 03:22:47 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-05-23 01:21:19 -05:00
|
|
|
attr_reader :topic, :options
|
|
|
|
|
|
|
|
def initialize(topic, options = {})
|
|
|
|
@topic = topic
|
|
|
|
@options = options
|
2019-06-05 03:22:47 -05:00
|
|
|
@translations = options[:translations] || TopicPostersSummary.translations
|
2013-05-23 01:21:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def summary
|
2014-02-18 22:36:07 -06:00
|
|
|
sorted_top_posters.compact.map(&method(:new_topic_poster_for))
|
2013-05-23 01:21:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def new_topic_poster_for(user)
|
2019-06-05 03:22:47 -05:00
|
|
|
topic_poster = TopicPoster.new
|
|
|
|
topic_poster.user = user
|
|
|
|
topic_poster.description = descriptions_for(user)
|
2020-07-17 04:48:08 -05:00
|
|
|
topic_poster.primary_group = user_lookup.primary_groups[user.id]
|
2021-07-08 02:46:21 -05:00
|
|
|
topic_poster.flair_group = user_lookup.flair_groups[user.id]
|
2019-06-05 03:22:47 -05:00
|
|
|
if topic.last_post_user_id == user.id
|
|
|
|
topic_poster.extras = +"latest"
|
|
|
|
topic_poster.extras << " single" if user_ids.uniq.size == 1
|
2013-05-23 01:21:19 -05:00
|
|
|
end
|
2019-06-05 03:22:47 -05:00
|
|
|
topic_poster
|
2013-05-23 01:21:19 -05:00
|
|
|
end
|
|
|
|
|
2021-08-25 05:16:08 -05:00
|
|
|
def descriptions_by_id(ids: nil)
|
2013-05-23 01:21:19 -05:00
|
|
|
@descriptions_by_id ||=
|
|
|
|
begin
|
2019-06-05 03:22:47 -05:00
|
|
|
result = {}
|
2021-08-25 05:16:08 -05:00
|
|
|
ids = ids || user_ids
|
2023-01-09 06:20:10 -06:00
|
|
|
|
2019-06-05 03:22:47 -05:00
|
|
|
if id = ids.shift
|
|
|
|
result[id] ||= []
|
|
|
|
result[id] << @translations[:original_poster]
|
|
|
|
end
|
2023-01-09 06:20:10 -06:00
|
|
|
|
2019-06-05 03:22:47 -05:00
|
|
|
if id = ids.shift
|
|
|
|
result[id] ||= []
|
|
|
|
result[id] << @translations[:most_recent_poster]
|
2023-01-09 06:20:10 -06:00
|
|
|
end
|
|
|
|
|
2019-06-05 03:22:47 -05:00
|
|
|
while id = ids.shift
|
|
|
|
result[id] ||= []
|
|
|
|
result[id] << @translations[:frequent_poster]
|
2023-01-09 06:20:10 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
result
|
2019-06-05 03:22:47 -05:00
|
|
|
end
|
2013-05-23 01:21:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def descriptions_for(user)
|
2021-02-02 03:50:04 -06:00
|
|
|
descriptions_by_id[user.id].join(@translations[:joiner])
|
2013-05-23 01:21:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def shuffle_last_poster_to_back_in(summary)
|
|
|
|
unless last_poster_is_topic_creator?
|
|
|
|
summary.reject! { |u| u.id == topic.last_post_user_id }
|
2020-07-17 04:48:08 -05:00
|
|
|
summary << user_lookup[topic.last_post_user_id]
|
2013-05-23 01:21:19 -05:00
|
|
|
end
|
|
|
|
summary
|
|
|
|
end
|
|
|
|
|
|
|
|
def last_poster_is_topic_creator?
|
|
|
|
topic.user_id == topic.last_post_user_id
|
|
|
|
end
|
|
|
|
|
|
|
|
def sorted_top_posters
|
|
|
|
shuffle_last_poster_to_back_in top_posters
|
|
|
|
end
|
|
|
|
|
|
|
|
def top_posters
|
2020-07-17 04:48:08 -05:00
|
|
|
user_ids.map { |id| user_lookup[id] }.compact.uniq.take(5)
|
2013-05-23 01:21:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def user_ids
|
|
|
|
[topic.user_id, topic.last_post_user_id, *topic.featured_user_ids]
|
|
|
|
end
|
|
|
|
|
2020-07-17 04:48:08 -05:00
|
|
|
def user_lookup
|
|
|
|
@user_lookup ||= options[:user_lookup] || UserLookup.new(user_ids)
|
2017-02-17 16:54:43 -06:00
|
|
|
end
|
2013-05-23 01:21:19 -05:00
|
|
|
end
|