2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-03-30 10:42:26 -05:00
|
|
|
class CategoryList
|
2023-12-11 09:58:45 -06:00
|
|
|
CATEGORIES_PER_PAGE = 25
|
|
|
|
|
2021-03-30 10:42:26 -05:00
|
|
|
include ActiveModel::Serialization
|
|
|
|
|
2017-08-01 03:26:03 -05:00
|
|
|
cattr_accessor :preloaded_topic_custom_fields
|
|
|
|
self.preloaded_topic_custom_fields = Set.new
|
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
attr_accessor :categories, :uncategorized
|
2013-02-05 13:16:51 -06:00
|
|
|
|
2023-03-23 12:39:38 -05:00
|
|
|
def self.register_included_association(association)
|
|
|
|
@included_assocations ||= []
|
|
|
|
@included_assocations << association if !@included_assocations.include?(association)
|
2023-03-22 15:12:08 -05:00
|
|
|
end
|
|
|
|
|
2023-03-23 12:39:38 -05:00
|
|
|
def self.included_associations
|
|
|
|
[
|
|
|
|
:uploaded_background,
|
2023-10-20 07:48:06 -05:00
|
|
|
:uploaded_background_dark,
|
2023-03-23 12:39:38 -05:00
|
|
|
:uploaded_logo,
|
|
|
|
:uploaded_logo_dark,
|
|
|
|
:topic_only_relative_url,
|
|
|
|
subcategories: [:topic_only_relative_url],
|
|
|
|
].concat(@included_assocations || [])
|
2023-03-22 15:12:08 -05:00
|
|
|
end
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def initialize(guardian = nil, options = {})
|
2013-05-28 13:54:00 -05:00
|
|
|
@guardian = guardian || Guardian.new
|
2013-10-17 01:44:56 -05:00
|
|
|
@options = options
|
2013-05-28 13:54:00 -05:00
|
|
|
|
2016-08-18 18:47:00 -05:00
|
|
|
find_relevant_topics if options[:include_topics]
|
2013-05-28 13:54:00 -05:00
|
|
|
find_categories
|
2016-08-18 18:47:00 -05:00
|
|
|
|
|
|
|
prune_empty
|
|
|
|
find_user_data
|
|
|
|
sort_unpinned
|
|
|
|
trim_results
|
2020-03-17 15:33:15 -05:00
|
|
|
demote_muted
|
2017-08-01 03:26:03 -05:00
|
|
|
|
|
|
|
if preloaded_topic_custom_fields.present?
|
2017-08-01 03:57:26 -05:00
|
|
|
displayable_topics = @categories.map(&:displayable_topics)
|
|
|
|
displayable_topics.flatten!
|
|
|
|
displayable_topics.compact!
|
|
|
|
|
|
|
|
if displayable_topics.present?
|
2023-01-09 06:20:10 -06:00
|
|
|
Topic.preload_custom_fields(displayable_topics, preloaded_topic_custom_fields)
|
2017-08-01 03:57:26 -05:00
|
|
|
end
|
2017-08-01 03:26:03 -05:00
|
|
|
end
|
2016-08-17 16:23:16 -05:00
|
|
|
end
|
2013-05-28 14:56:46 -05:00
|
|
|
|
2016-08-17 16:23:16 -05:00
|
|
|
def preload_key
|
2020-04-30 01:48:34 -05:00
|
|
|
"categories_list"
|
2013-05-28 13:54:00 -05:00
|
|
|
end
|
|
|
|
|
2019-04-25 04:07:19 -05:00
|
|
|
def self.order_categories(categories)
|
|
|
|
if SiteSetting.fixed_category_positions
|
|
|
|
categories.order(:position, :id)
|
|
|
|
else
|
2023-01-09 06:20:10 -06:00
|
|
|
categories
|
|
|
|
.left_outer_joins(:featured_topics)
|
2024-01-08 18:19:37 -06:00
|
|
|
.where("topics.category_id IS NULL OR topics.category_id IN (?)", categories.select(:id))
|
2023-01-09 06:20:10 -06:00
|
|
|
.group("categories.id")
|
2019-04-25 04:07:19 -05:00
|
|
|
.order("max(topics.bumped_at) DESC NULLS LAST")
|
2023-01-09 06:20:10 -06:00
|
|
|
.order("categories.id ASC")
|
2019-04-25 04:07:19 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-28 13:54:00 -05:00
|
|
|
private
|
|
|
|
|
2018-06-07 00:28:18 -05:00
|
|
|
def find_relevant_topics
|
|
|
|
@topics_by_id = {}
|
|
|
|
@topics_by_category_id = {}
|
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
category_featured_topics = CategoryFeaturedTopic.select(%i[category_id topic_id]).order(:rank)
|
2018-06-07 00:28:18 -05:00
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
@all_topics =
|
|
|
|
Topic.where(id: category_featured_topics.map(&:topic_id)).includes(
|
2022-12-22 14:29:49 -06:00
|
|
|
:shared_draft,
|
|
|
|
:category,
|
2023-01-09 06:20:10 -06:00
|
|
|
{ topic_thumbnails: %i[optimized_image upload] },
|
2022-12-22 14:29:49 -06:00
|
|
|
)
|
2021-10-29 09:52:23 -05:00
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
@all_topics = @all_topics.joins(:tags).where(tags: { name: @options[:tag] }) if @options[
|
|
|
|
:tag
|
|
|
|
].present?
|
2022-08-17 11:51:02 -05:00
|
|
|
|
2021-10-29 09:52:23 -05:00
|
|
|
if @guardian.authenticated?
|
2023-01-09 06:20:10 -06:00
|
|
|
@all_topics =
|
2023-06-28 20:25:58 -05:00
|
|
|
@all_topics
|
|
|
|
.joins(
|
|
|
|
"LEFT JOIN topic_users tu ON topics.id = tu.topic_id AND tu.user_id = #{@guardian.user.id.to_i}",
|
|
|
|
)
|
|
|
|
.joins(
|
|
|
|
"LEFT JOIN category_users ON category_users.category_id = topics.category_id AND category_users.user_id = #{@guardian.user.id}",
|
|
|
|
)
|
|
|
|
.where(
|
|
|
|
"COALESCE(tu.notification_level,1) > :muted",
|
|
|
|
muted: TopicUser.notification_levels[:muted],
|
|
|
|
)
|
2021-10-29 09:52:23 -05:00
|
|
|
end
|
|
|
|
|
2022-08-17 11:51:02 -05:00
|
|
|
@all_topics = TopicQuery.remove_muted_tags(@all_topics, @guardian.user).includes(:last_poster)
|
2018-06-07 00:28:18 -05:00
|
|
|
@all_topics.each do |t|
|
|
|
|
# hint for the serializer
|
2022-08-17 11:51:02 -05:00
|
|
|
t.include_last_poster = true
|
2021-03-04 16:04:19 -06:00
|
|
|
t.dismissed = dismissed_topic?(t)
|
2018-06-07 00:28:18 -05:00
|
|
|
@topics_by_id[t.id] = t
|
|
|
|
end
|
2016-08-18 18:47:00 -05:00
|
|
|
|
2018-06-07 00:28:18 -05: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 18:47:00 -05:00
|
|
|
end
|
2018-06-07 00:28:18 -05:00
|
|
|
end
|
2016-08-18 18:47:00 -05:00
|
|
|
|
2021-03-04 16:04:19 -06:00
|
|
|
def dismissed_topic?(topic)
|
|
|
|
if @guardian.current_user
|
2023-01-09 06:20:10 -06:00
|
|
|
@dismissed_topic_users_lookup ||=
|
|
|
|
DismissedTopicUser.lookup_for(@guardian.current_user, @all_topics)
|
2021-03-04 16:04:19 -06:00
|
|
|
@dismissed_topic_users_lookup.include?(topic.id)
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-07 00:28:18 -05:00
|
|
|
def find_categories
|
2023-05-30 16:41:50 -05:00
|
|
|
query = Category.includes(CategoryList.included_associations).secured(@guardian)
|
2023-01-09 06:20:10 -06:00
|
|
|
|
2023-05-30 16:41:50 -05:00
|
|
|
query =
|
|
|
|
query.where(
|
2023-01-09 06:20:10 -06:00
|
|
|
"categories.parent_category_id = ?",
|
|
|
|
@options[:parent_category_id].to_i,
|
|
|
|
) if @options[:parent_category_id].present?
|
2018-06-07 00:28:18 -05:00
|
|
|
|
2023-05-30 16:41:50 -05:00
|
|
|
query = self.class.order_categories(query)
|
2023-12-11 09:58:45 -06:00
|
|
|
|
|
|
|
if SiteSetting.lazy_load_categories
|
|
|
|
page = [1, @options[:page].to_i].max
|
|
|
|
query = query.limit(CATEGORIES_PER_PAGE).offset((page - 1) * CATEGORIES_PER_PAGE)
|
|
|
|
end
|
|
|
|
|
2023-05-30 16:41:50 -05:00
|
|
|
query =
|
|
|
|
DiscoursePluginRegistry.apply_modifier(:category_list_find_categories_query, query, self)
|
2013-05-28 13:54:00 -05:00
|
|
|
|
2023-05-30 16:41:50 -05:00
|
|
|
@categories = query.to_a
|
2015-09-07 11:52:53 -05:00
|
|
|
|
2023-11-09 10:23:24 -06:00
|
|
|
if Site.preloaded_category_custom_fields.any?
|
|
|
|
Category.preload_custom_fields(@categories, Site.preloaded_category_custom_fields)
|
2023-10-26 08:34:23 -05:00
|
|
|
end
|
|
|
|
|
2021-10-05 13:12:31 -05:00
|
|
|
include_subcategories = @options[:include_subcategories] == true
|
|
|
|
|
2021-05-13 18:45:14 -05:00
|
|
|
notification_levels = CategoryUser.notification_levels_for(@guardian.user)
|
2019-11-07 20:58:11 -06:00
|
|
|
default_notification_level = CategoryUser.default_notification_level
|
2015-09-15 07:58:22 -05:00
|
|
|
|
2023-12-18 08:46:09 -06:00
|
|
|
if SiteSetting.lazy_load_categories
|
|
|
|
subcategory_ids = {}
|
|
|
|
Category
|
|
|
|
.secured(@guardian)
|
|
|
|
.where(parent_category_id: @categories.map(&:id))
|
|
|
|
.pluck(:id, :parent_category_id)
|
|
|
|
.each { |id, parent_id| (subcategory_ids[parent_id] ||= []) << id }
|
|
|
|
@categories.each { |c| c.subcategory_ids = subcategory_ids[c.id] || [] }
|
|
|
|
elsif @options[:parent_category_id].blank?
|
2021-10-05 13:12:31 -05:00
|
|
|
subcategory_ids = {}
|
|
|
|
subcategory_list = {}
|
2018-06-07 00:28:18 -05:00
|
|
|
to_delete = Set.new
|
|
|
|
@categories.each do |c|
|
|
|
|
if c.parent_category_id.present?
|
2021-10-05 13:12:31 -05:00
|
|
|
subcategory_ids[c.parent_category_id] ||= []
|
|
|
|
subcategory_ids[c.parent_category_id] << c.id
|
|
|
|
if include_subcategories
|
|
|
|
subcategory_list[c.parent_category_id] ||= []
|
|
|
|
subcategory_list[c.parent_category_id] << c
|
|
|
|
end
|
2018-06-07 00:28:18 -05:00
|
|
|
to_delete << c
|
2014-01-15 13:11:19 -06:00
|
|
|
end
|
2013-05-28 13:54:00 -05:00
|
|
|
end
|
2021-10-05 13:12:31 -05:00
|
|
|
@categories.each do |c|
|
|
|
|
c.subcategory_ids = subcategory_ids[c.id] || []
|
2023-01-09 06:20:10 -06:00
|
|
|
c.subcategory_list = subcategory_list[c.id] || [] if include_subcategories
|
2021-10-05 13:12:31 -05:00
|
|
|
end
|
2018-06-07 00:28:18 -05:00
|
|
|
@categories.delete_if { |c| to_delete.include?(c) }
|
|
|
|
end
|
2013-05-28 13:54:00 -05:00
|
|
|
|
2022-03-04 15:11:59 -06:00
|
|
|
allowed_topic_create = Set.new(Category.topic_create_allowed(@guardian).pluck(:id))
|
2022-12-22 14:29:49 -06:00
|
|
|
|
2022-03-04 15:11:59 -06:00
|
|
|
categories_with_descendants.each do |category|
|
|
|
|
category.notification_level = notification_levels[category.id] || default_notification_level
|
2023-01-09 06:20:10 -06:00
|
|
|
category.permission = CategoryGroup.permission_types[:full] if allowed_topic_create.include?(
|
|
|
|
category.id,
|
|
|
|
)
|
2022-03-04 15:11:59 -06:00
|
|
|
category.has_children = category.subcategories.present?
|
|
|
|
end
|
|
|
|
|
2018-06-07 00:28:18 -05:00
|
|
|
if @topics_by_category_id
|
2022-03-04 15:11:59 -06:00
|
|
|
categories_with_descendants.each do |c|
|
2018-06-07 00:28:18 -05:00
|
|
|
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 18:47:00 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-06-10 13:36:47 -05:00
|
|
|
end
|
2018-06-07 00:28:18 -05:00
|
|
|
end
|
2016-08-18 18:47:00 -05:00
|
|
|
|
2018-06-07 00:28:18 -05:00
|
|
|
def prune_empty
|
|
|
|
return if SiteSetting.allow_uncategorized_topics
|
2021-05-03 22:05:08 -05:00
|
|
|
@categories.delete_if { |c| c.uncategorized? }
|
2018-06-07 00:28:18 -05:00
|
|
|
end
|
2016-08-18 18:47:00 -05:00
|
|
|
|
2018-06-07 00:28:18 -05: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 18:47:00 -05:00
|
|
|
end
|
2018-06-07 00:28:18 -05:00
|
|
|
end
|
2016-08-18 18:47:00 -05:00
|
|
|
|
2018-06-07 00:28:18 -05:00
|
|
|
# Put unpinned topics at the end of the list
|
|
|
|
def sort_unpinned
|
|
|
|
if @guardian.current_user && @all_topics.present?
|
2022-03-04 15:11:59 -06:00
|
|
|
categories_with_descendants.each do |c|
|
2018-06-07 00:28:18 -05:00
|
|
|
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
|
2023-01-09 06:20:10 -06:00
|
|
|
c.displayable_topics = (c.displayable_topics - unpinned) + unpinned unless unpinned.empty?
|
2016-08-18 18:47:00 -05:00
|
|
|
end
|
|
|
|
end
|
2018-06-07 00:28:18 -05:00
|
|
|
end
|
2016-08-18 18:47:00 -05:00
|
|
|
|
2020-03-17 15:33:15 -05: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 00:28:18 -05:00
|
|
|
def trim_results
|
2022-03-04 15:11:59 -06:00
|
|
|
categories_with_descendants.each do |c|
|
2018-06-07 00:28:18 -05:00
|
|
|
next if c.displayable_topics.blank?
|
|
|
|
c.displayable_topics = c.displayable_topics[0, c.num_featured_topics]
|
2016-08-18 18:47:00 -05:00
|
|
|
end
|
2018-06-07 00:28:18 -05:00
|
|
|
end
|
2016-08-18 18:47:00 -05:00
|
|
|
|
2022-03-04 15:11:59 -06:00
|
|
|
def categories_with_descendants(categories = @categories)
|
|
|
|
return @categories_with_children if @categories_with_children && (categories == @categories)
|
|
|
|
return nil if categories.nil?
|
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
result = categories.flat_map { |c| [c, *categories_with_descendants(c.subcategory_list)] }
|
2022-03-04 15:11:59 -06:00
|
|
|
|
|
|
|
@categories_with_children = result if categories == @categories
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|