discourse/app/models/topic_posters_summary.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

100 lines
2.4 KiB
Ruby
Raw Permalink Normal View History

# frozen_string_literal: true
# This is used in topic lists
2013-05-23 02:21:19 -04:00
class TopicPostersSummary
# 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),
Fix i18n issues reported on Crowdin (#11747) * Pluralize `groups.errors.adding_too_many_users` This fixes https://discourse.crowdin.com/translate/f3230e7607a36bb0a2f97fd90605a44e/248/en-ar#53882 * Pluralize `js.composer.error.title_too_short` This fixes https://discourse.crowdin.com/translate/f3230e7607a36bb0a2f97fd90605a44e/246/en-ar#41172 * Pluralize `js.composer.error.title_too_long` This fixes https://discourse.crowdin.com/translate/f3230e7607a36bb0a2f97fd90605a44e/246/en-ar#41174 * Pluralize `js.composer.error.post_length` This fixes https://discourse.crowdin.com/translate/f3230e7607a36bb0a2f97fd90605a44e/246/en-ar#41178 * Pluralize `js.topic.progress.jump_prompt_of` This fixes https://discourse.crowdin.com/translate/f3230e7607a36bb0a2f97fd90605a44e/246/en-ar#41958 * Use translations to join strings about posters This fixes https://discourse.crowdin.com/translate/f3230e7607a36bb0a2f97fd90605a44e/248/en-ar#49334 It also makes some changes to the crawler view: * Removes `poster.moreCount` which is only available on the client for PMs * CSS class names are actually stored in `poster.extras` instead of `poster.extraClasses` * Stop concatenating category stats This fixes https://discourse.crowdin.com/translate/f3230e7607a36bb0a2f97fd90605a44e/246/en-ar#40740 * Pluralize `js.summary.description` This fixes https://discourse.crowdin.com/translate/f3230e7607a36bb0a2f97fd90605a44e/246/en-ar#40782 * Pluralize `js.summary.description_time_MF` This fixes https://discourse.crowdin.com/translate/f3230e7607a36bb0a2f97fd90605a44e/246/en-ar#40784 * Use translation to join list of tags This fixes https://discourse.crowdin.com/translate/f3230e7607a36bb0a2f97fd90605a44e/246/en-ar#43372 * Pluralize `admin_js.admin.groups.manage.membership.automatic_membership_user_count` This fixes https://discourse.crowdin.com/translate/f3230e7607a36bb0a2f97fd90605a44e/246/en-ar#43720 * Pluralize `js.post.controls.delete_topic_confirm_modal` This fixes https://discourse.crowdin.com/translate/f3230e7607a36bb0a2f97fd90605a44e/246/en-ar#54804 * Stop concatenating `js.post.last_edited_on` This fixes https://discourse.crowdin.com/translate/f3230e7607a36bb0a2f97fd90605a44e/246/en-ar#42358 * Stop concatenating `js.post.wiki_last_edited_on` This fixes https://discourse.crowdin.com/translate/f3230e7607a36bb0a2f97fd90605a44e/246/en-ar#42356 It also fixes a regression because `js.post.wiki_last_edited_on` wasn't used anymore since 2017.
2021-02-02 04:50:04 -05:00
frequent_poster: I18n.t(:frequent_poster),
joiner: I18n.t(:poster_description_joiner),
}
end
2013-05-23 02:21:19 -04:00
attr_reader :topic, :options
def initialize(topic, options = {})
@topic = topic
@options = options
@translations = options[:translations] || TopicPostersSummary.translations
2013-05-23 02:21:19 -04:00
end
def summary
2014-02-18 23:36:07 -05:00
sorted_top_posters.compact.map(&method(:new_topic_poster_for))
2013-05-23 02:21:19 -04:00
end
private
def new_topic_poster_for(user)
topic_poster = TopicPoster.new
topic_poster.user = user
topic_poster.description = descriptions_for(user)
topic_poster.primary_group = user_lookup.primary_groups[user.id]
topic_poster.flair_group = user_lookup.flair_groups[user.id]
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 02:21:19 -04:00
end
topic_poster
2013-05-23 02:21:19 -04:00
end
def descriptions_by_id(ids: nil)
2013-05-23 02:21:19 -04:00
@descriptions_by_id ||=
begin
result = {}
ids = ids || user_ids
if id = ids.shift
result[id] ||= []
result[id] << @translations[:original_poster]
end
if id = ids.shift
result[id] ||= []
result[id] << @translations[:most_recent_poster]
end
while id = ids.shift
result[id] ||= []
result[id] << @translations[:frequent_poster]
end
result
end
2013-05-23 02:21:19 -04:00
end
def descriptions_for(user)
Fix i18n issues reported on Crowdin (#11747) * Pluralize `groups.errors.adding_too_many_users` This fixes https://discourse.crowdin.com/translate/f3230e7607a36bb0a2f97fd90605a44e/248/en-ar#53882 * Pluralize `js.composer.error.title_too_short` This fixes https://discourse.crowdin.com/translate/f3230e7607a36bb0a2f97fd90605a44e/246/en-ar#41172 * Pluralize `js.composer.error.title_too_long` This fixes https://discourse.crowdin.com/translate/f3230e7607a36bb0a2f97fd90605a44e/246/en-ar#41174 * Pluralize `js.composer.error.post_length` This fixes https://discourse.crowdin.com/translate/f3230e7607a36bb0a2f97fd90605a44e/246/en-ar#41178 * Pluralize `js.topic.progress.jump_prompt_of` This fixes https://discourse.crowdin.com/translate/f3230e7607a36bb0a2f97fd90605a44e/246/en-ar#41958 * Use translations to join strings about posters This fixes https://discourse.crowdin.com/translate/f3230e7607a36bb0a2f97fd90605a44e/248/en-ar#49334 It also makes some changes to the crawler view: * Removes `poster.moreCount` which is only available on the client for PMs * CSS class names are actually stored in `poster.extras` instead of `poster.extraClasses` * Stop concatenating category stats This fixes https://discourse.crowdin.com/translate/f3230e7607a36bb0a2f97fd90605a44e/246/en-ar#40740 * Pluralize `js.summary.description` This fixes https://discourse.crowdin.com/translate/f3230e7607a36bb0a2f97fd90605a44e/246/en-ar#40782 * Pluralize `js.summary.description_time_MF` This fixes https://discourse.crowdin.com/translate/f3230e7607a36bb0a2f97fd90605a44e/246/en-ar#40784 * Use translation to join list of tags This fixes https://discourse.crowdin.com/translate/f3230e7607a36bb0a2f97fd90605a44e/246/en-ar#43372 * Pluralize `admin_js.admin.groups.manage.membership.automatic_membership_user_count` This fixes https://discourse.crowdin.com/translate/f3230e7607a36bb0a2f97fd90605a44e/246/en-ar#43720 * Pluralize `js.post.controls.delete_topic_confirm_modal` This fixes https://discourse.crowdin.com/translate/f3230e7607a36bb0a2f97fd90605a44e/246/en-ar#54804 * Stop concatenating `js.post.last_edited_on` This fixes https://discourse.crowdin.com/translate/f3230e7607a36bb0a2f97fd90605a44e/246/en-ar#42358 * Stop concatenating `js.post.wiki_last_edited_on` This fixes https://discourse.crowdin.com/translate/f3230e7607a36bb0a2f97fd90605a44e/246/en-ar#42356 It also fixes a regression because `js.post.wiki_last_edited_on` wasn't used anymore since 2017.
2021-02-02 04:50:04 -05:00
descriptions_by_id[user.id].join(@translations[:joiner])
2013-05-23 02:21:19 -04: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 }
summary << user_lookup[topic.last_post_user_id]
2013-05-23 02:21:19 -04: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
user_ids.map { |id| user_lookup[id] }.compact.uniq.take(5)
2013-05-23 02:21:19 -04:00
end
def user_ids
[topic.user_id, topic.last_post_user_id, *topic.featured_user_ids]
end
def user_lookup
@user_lookup ||= options[:user_lookup] || UserLookup.new(user_ids)
end
2013-05-23 02:21:19 -04:00
end