2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
class CategoryList
|
|
|
|
include ActiveModel::Serialization
|
|
|
|
|
2017-08-01 04:26:03 -04:00
|
|
|
cattr_accessor :preloaded_topic_custom_fields
|
|
|
|
self.preloaded_topic_custom_fields = Set.new
|
|
|
|
|
2013-05-28 21:15:30 -04:00
|
|
|
attr_accessor :categories,
|
|
|
|
:uncategorized,
|
|
|
|
:draft,
|
|
|
|
:draft_key,
|
|
|
|
:draft_sequence
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def initialize(guardian = nil, options = {})
|
2013-05-28 14:54:00 -04:00
|
|
|
@guardian = guardian || Guardian.new
|
2013-10-17 02:44:56 -04:00
|
|
|
@options = options
|
2013-05-28 14:54:00 -04:00
|
|
|
|
2016-08-18 19:47:00 -04:00
|
|
|
find_relevant_topics if options[:include_topics]
|
2013-05-28 14:54:00 -04:00
|
|
|
find_categories
|
2016-08-18 19:47:00 -04:00
|
|
|
|
|
|
|
prune_empty
|
|
|
|
find_user_data
|
|
|
|
sort_unpinned
|
|
|
|
trim_results
|
2020-03-17 16:33:15 -04:00
|
|
|
demote_muted
|
2017-08-01 04:26:03 -04:00
|
|
|
|
|
|
|
if preloaded_topic_custom_fields.present?
|
2017-08-01 04:57:26 -04:00
|
|
|
displayable_topics = @categories.map(&:displayable_topics)
|
|
|
|
displayable_topics.flatten!
|
|
|
|
displayable_topics.compact!
|
|
|
|
|
|
|
|
if displayable_topics.present?
|
|
|
|
Topic.preload_custom_fields(
|
|
|
|
displayable_topics,
|
|
|
|
preloaded_topic_custom_fields
|
|
|
|
)
|
|
|
|
end
|
2017-08-01 04:26:03 -04:00
|
|
|
end
|
2016-08-17 17:23:16 -04:00
|
|
|
end
|
2013-05-28 15:56:46 -04:00
|
|
|
|
2016-08-17 17:23:16 -04:00
|
|
|
def preload_key
|
2020-04-30 02:48:34 -04:00
|
|
|
"categories_list"
|
2013-05-28 14:54:00 -04:00
|
|
|
end
|
|
|
|
|
2019-04-25 05:07:19 -04:00
|
|
|
def self.order_categories(categories)
|
|
|
|
if SiteSetting.fixed_category_positions
|
|
|
|
categories.order(:position, :id)
|
|
|
|
else
|
|
|
|
allowed_category_ids = categories.pluck(:id) << nil # `nil` is necessary to include categories without any associated topics
|
|
|
|
categories.left_outer_joins(:featured_topics)
|
|
|
|
.where(topics: { category_id: allowed_category_ids })
|
|
|
|
.group('categories.id')
|
|
|
|
.order("max(topics.bumped_at) DESC NULLS LAST")
|
|
|
|
.order('categories.id ASC')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-28 14:54:00 -04:00
|
|
|
private
|
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def find_relevant_topics
|
|
|
|
@topics_by_id = {}
|
|
|
|
@topics_by_category_id = {}
|
|
|
|
|
|
|
|
category_featured_topics = CategoryFeaturedTopic.select([:category_id, :topic_id]).order(:rank)
|
|
|
|
|
|
|
|
@all_topics = Topic.where(id: category_featured_topics.map(&:topic_id))
|
|
|
|
@all_topics = @all_topics.includes(:last_poster) if @options[:include_topics]
|
|
|
|
@all_topics.each do |t|
|
|
|
|
# hint for the serializer
|
|
|
|
t.include_last_poster = true if @options[:include_topics]
|
|
|
|
@topics_by_id[t.id] = t
|
|
|
|
end
|
2016-08-18 19:47:00 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
category_featured_topics.each do |cft|
|
|
|
|
@topics_by_category_id[cft.category_id] ||= []
|
|
|
|
@topics_by_category_id[cft.category_id] << cft.topic_id
|
2016-08-18 19:47:00 -04:00
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2016-08-18 19:47:00 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def find_categories
|
|
|
|
@categories = Category.includes(
|
|
|
|
:uploaded_background,
|
|
|
|
:uploaded_logo,
|
|
|
|
:topic_only_relative_url,
|
|
|
|
subcategories: [:topic_only_relative_url]
|
|
|
|
).secured(@guardian)
|
|
|
|
|
|
|
|
@categories = @categories.where("categories.parent_category_id = ?", @options[:parent_category_id].to_i) if @options[:parent_category_id].present?
|
|
|
|
|
2019-04-25 05:07:19 -04:00
|
|
|
@categories = self.class.order_categories(@categories)
|
2013-05-28 14:54:00 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
@categories = @categories.to_a
|
2015-09-07 12:52:53 -04:00
|
|
|
|
2019-11-07 21:58:11 -05:00
|
|
|
notification_levels = CategoryUser.notification_levels_for(@guardian)
|
|
|
|
default_notification_level = CategoryUser.default_notification_level
|
2015-09-15 08:58:22 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
allowed_topic_create = Set.new(Category.topic_create_allowed(@guardian).pluck(:id))
|
|
|
|
@categories.each do |category|
|
2019-11-07 21:58:11 -05:00
|
|
|
category.notification_level = notification_levels[category.id] || default_notification_level
|
2018-06-07 01:28:18 -04:00
|
|
|
category.permission = CategoryGroup.permission_types[:full] if allowed_topic_create.include?(category.id)
|
|
|
|
category.has_children = category.subcategories.present?
|
|
|
|
end
|
2015-09-07 12:52:53 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
if @options[:parent_category_id].blank?
|
|
|
|
subcategories = {}
|
|
|
|
to_delete = Set.new
|
|
|
|
@categories.each do |c|
|
|
|
|
if c.parent_category_id.present?
|
|
|
|
subcategories[c.parent_category_id] ||= []
|
|
|
|
subcategories[c.parent_category_id] << c.id
|
|
|
|
to_delete << c
|
2014-01-15 14:11:19 -05:00
|
|
|
end
|
2013-05-28 14:54:00 -04:00
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
@categories.each { |c| c.subcategory_ids = subcategories[c.id] }
|
|
|
|
@categories.delete_if { |c| to_delete.include?(c) }
|
|
|
|
end
|
2013-05-28 14:54:00 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
if @topics_by_category_id
|
|
|
|
@categories.each do |c|
|
|
|
|
topics_in_cat = @topics_by_category_id[c.id]
|
|
|
|
if topics_in_cat.present?
|
|
|
|
c.displayable_topics = []
|
|
|
|
topics_in_cat.each do |topic_id|
|
|
|
|
topic = @topics_by_id[topic_id]
|
|
|
|
if topic.present? && @guardian.can_see?(topic)
|
|
|
|
# topic.category is very slow under rails 4.2
|
|
|
|
topic.association(:category).target = c
|
|
|
|
c.displayable_topics << topic
|
2016-08-18 19:47:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-06-10 14:36:47 -04:00
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2016-08-18 19:47:00 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def prune_empty
|
|
|
|
return if SiteSetting.allow_uncategorized_topics
|
|
|
|
@categories.delete_if { |c| c.uncategorized? && c.displayable_topics.blank? }
|
|
|
|
end
|
2016-08-18 19:47:00 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
# Attach some data for serialization to each topic
|
|
|
|
def find_user_data
|
|
|
|
if @guardian.current_user && @all_topics.present?
|
|
|
|
topic_lookup = TopicUser.lookup_for(@guardian.current_user, @all_topics)
|
|
|
|
@all_topics.each { |ft| ft.user_data = topic_lookup[ft.id] }
|
2016-08-18 19:47:00 -04:00
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2016-08-18 19:47:00 -04:00
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
# Put unpinned topics at the end of the list
|
|
|
|
def sort_unpinned
|
|
|
|
if @guardian.current_user && @all_topics.present?
|
|
|
|
@categories.each do |c|
|
|
|
|
next if c.displayable_topics.blank? || c.displayable_topics.size <= c.num_featured_topics
|
|
|
|
unpinned = []
|
|
|
|
c.displayable_topics.each do |t|
|
|
|
|
unpinned << t if t.pinned_at && PinnedCheck.unpinned?(t, t.user_data)
|
|
|
|
end
|
|
|
|
unless unpinned.empty?
|
|
|
|
c.displayable_topics = (c.displayable_topics - unpinned) + unpinned
|
2016-08-18 19:47:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2016-08-18 19:47:00 -04:00
|
|
|
|
2020-03-17 16:33:15 -04:00
|
|
|
def demote_muted
|
|
|
|
muted_categories = @categories.select { |category| category.notification_level == 0 }
|
|
|
|
@categories = @categories.reject { |category| category.notification_level == 0 }
|
|
|
|
@categories.concat muted_categories
|
|
|
|
end
|
|
|
|
|
2018-06-07 01:28:18 -04:00
|
|
|
def trim_results
|
|
|
|
@categories.each do |c|
|
|
|
|
next if c.displayable_topics.blank?
|
|
|
|
c.displayable_topics = c.displayable_topics[0, c.num_featured_topics]
|
2016-08-18 19:47:00 -04:00
|
|
|
end
|
2018-06-07 01:28:18 -04:00
|
|
|
end
|
2016-08-18 19:47:00 -04:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|