2018-07-16 04:10:22 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
class Category < ActiveRecord::Base
|
2019-12-12 06:13:42 -05:00
|
|
|
RESERVED_SLUGS = ["none"]
|
|
|
|
|
2024-05-06 14:18:53 -04:00
|
|
|
self.ignored_columns = [
|
2024-05-06 23:06:31 -04:00
|
|
|
:suppress_from_latest, # TODO: Remove when 20240212034010_drop_deprecated_columns has been promoted to pre-deploy
|
|
|
|
:required_tag_group_id, # TODO: Remove when 20240212034010_drop_deprecated_columns has been promoted to pre-deploy
|
|
|
|
:min_tags_from_required_group, # TODO: Remove when 20240212034010_drop_deprecated_columns has been promoted to pre-deploy
|
2024-09-03 21:38:46 -04:00
|
|
|
:reviewable_by_group_id,
|
2024-05-06 14:18:53 -04:00
|
|
|
]
|
|
|
|
|
2017-08-15 11:46:57 -04:00
|
|
|
include Searchable
|
2014-04-19 00:00:40 -04:00
|
|
|
include Positionable
|
2014-04-28 04:31:51 -04:00
|
|
|
include HasCustomFields
|
2016-01-12 03:40:36 -05:00
|
|
|
include CategoryHashtag
|
2016-12-22 00:46:22 -05:00
|
|
|
include AnonCacheInvalidator
|
2018-10-05 04:53:59 -04:00
|
|
|
include HasDestroyedWebHook
|
2013-10-18 03:09:30 -04:00
|
|
|
|
2023-08-07 21:18:55 -04:00
|
|
|
SLUG_REF_SEPARATOR = ":"
|
2018-07-12 22:51:08 -04:00
|
|
|
|
2021-09-20 01:50:49 -04:00
|
|
|
belongs_to :topic
|
2014-02-17 11:44:28 -05:00
|
|
|
belongs_to :topic_only_relative_url,
|
|
|
|
-> { select "id, title, slug" },
|
|
|
|
class_name: "Topic",
|
|
|
|
foreign_key: "topic_id"
|
2013-08-15 11:52:18 -04:00
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
belongs_to :user
|
2013-10-17 02:44:56 -04:00
|
|
|
belongs_to :latest_post, class_name: "Post"
|
2016-12-02 02:15:34 -05:00
|
|
|
belongs_to :uploaded_logo, class_name: "Upload"
|
2022-10-07 11:00:44 -04:00
|
|
|
belongs_to :uploaded_logo_dark, class_name: "Upload"
|
2016-12-02 02:15:34 -05:00
|
|
|
belongs_to :uploaded_background, class_name: "Upload"
|
2023-10-20 08:48:06 -04:00
|
|
|
belongs_to :uploaded_background_dark, class_name: "Upload"
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
has_many :topics
|
2016-07-06 15:56:40 -04:00
|
|
|
has_many :category_users
|
2013-02-07 10:45:24 -05:00
|
|
|
has_many :category_featured_topics
|
2013-02-05 14:16:51 -05:00
|
|
|
has_many :featured_topics, through: :category_featured_topics, source: :topic
|
|
|
|
|
2014-08-31 16:10:38 -04:00
|
|
|
has_many :category_groups, dependent: :destroy
|
2024-09-03 21:38:46 -04:00
|
|
|
has_many :category_moderation_groups, dependent: :destroy
|
2013-04-29 02:33:24 -04:00
|
|
|
has_many :groups, through: :category_groups
|
2024-09-03 21:38:46 -04:00
|
|
|
has_many :moderating_groups, through: :category_moderation_groups, source: :group
|
2020-03-24 01:59:42 -04:00
|
|
|
has_many :topic_timers, dependent: :destroy
|
2022-06-08 19:24:30 -04:00
|
|
|
has_many :upload_references, as: :target, dependent: :destroy
|
2013-04-29 02:33:24 -04:00
|
|
|
|
2023-02-12 23:37:59 -05:00
|
|
|
has_one :category_setting, dependent: :destroy
|
|
|
|
|
2023-08-03 22:53:22 -04:00
|
|
|
delegate :auto_bump_cooldown_days,
|
|
|
|
:num_auto_bump_daily,
|
|
|
|
:num_auto_bump_daily=,
|
2023-09-11 21:51:49 -04:00
|
|
|
:require_reply_approval,
|
|
|
|
:require_reply_approval=,
|
|
|
|
:require_reply_approval?,
|
|
|
|
:require_topic_approval,
|
|
|
|
:require_topic_approval=,
|
|
|
|
:require_topic_approval?,
|
2023-08-03 22:53:22 -04:00
|
|
|
to: :category_setting,
|
|
|
|
allow_nil: true
|
2023-03-10 00:45:01 -05:00
|
|
|
|
2016-06-15 13:49:57 -04:00
|
|
|
has_and_belongs_to_many :web_hooks
|
|
|
|
|
2023-03-10 00:45:01 -05:00
|
|
|
accepts_nested_attributes_for :category_setting, update_only: true
|
|
|
|
|
2013-03-02 03:57:02 -05:00
|
|
|
validates :user_id, presence: true
|
2019-03-18 03:25:45 -04:00
|
|
|
|
2020-10-05 05:50:05 -04:00
|
|
|
validates :name,
|
|
|
|
if:
|
|
|
|
Proc.new { |c|
|
|
|
|
c.new_record? || c.will_save_change_to_name? ||
|
|
|
|
c.will_save_change_to_parent_category_id?
|
|
|
|
},
|
2014-08-11 16:55:26 -04:00
|
|
|
presence: true,
|
|
|
|
uniqueness: {
|
|
|
|
scope: :parent_category_id,
|
|
|
|
case_sensitive: false,
|
|
|
|
},
|
|
|
|
length: {
|
|
|
|
in: 1..50,
|
|
|
|
}
|
2019-03-18 03:25:45 -04:00
|
|
|
|
2017-03-01 12:03:12 -05:00
|
|
|
validates :num_featured_topics, numericality: { only_integer: true, greater_than: 0 }
|
2019-03-18 03:25:45 -04:00
|
|
|
validates :search_priority, inclusion: { in: Searchable::PRIORITIES.values }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2019-03-18 03:25:45 -04:00
|
|
|
validate :parent_category_validator
|
2016-02-24 13:47:58 -05:00
|
|
|
validate :email_in_validator
|
2014-12-03 19:23:59 -05:00
|
|
|
validate :ensure_slug
|
2019-02-14 00:38:52 -05:00
|
|
|
validate :permissions_compatibility_validator
|
|
|
|
|
2023-02-06 23:27:38 -05:00
|
|
|
validates :default_slow_mode_seconds,
|
|
|
|
numericality: {
|
|
|
|
only_integer: true,
|
|
|
|
greater_than: 0,
|
|
|
|
},
|
|
|
|
allow_nil: true
|
2018-09-26 11:04:49 -04:00
|
|
|
validates :auto_close_hours,
|
|
|
|
numericality: {
|
|
|
|
greater_than: 0,
|
|
|
|
less_than_or_equal_to: 87_600,
|
|
|
|
},
|
|
|
|
allow_nil: true
|
2019-12-12 06:13:42 -05:00
|
|
|
validates :slug, exclusion: { in: RESERVED_SLUGS }
|
2018-09-26 11:04:49 -04:00
|
|
|
|
2016-12-21 21:13:14 -05:00
|
|
|
after_create :create_category_definition
|
2021-09-20 01:50:49 -04:00
|
|
|
after_destroy :trash_category_definition
|
2022-10-12 13:09:45 -04:00
|
|
|
after_destroy :clear_related_site_settings
|
2016-12-21 21:13:14 -05:00
|
|
|
|
2013-07-13 21:24:16 -04:00
|
|
|
before_save :apply_permissions
|
2014-07-14 10:16:24 -04:00
|
|
|
before_save :downcase_email
|
2014-08-18 11:07:32 -04:00
|
|
|
before_save :downcase_name
|
2023-03-10 00:45:01 -05:00
|
|
|
before_save :ensure_category_setting
|
2015-07-09 22:09:43 -04:00
|
|
|
|
2016-12-21 21:13:14 -05:00
|
|
|
after_save :reset_topic_ids_cache
|
2020-04-09 08:42:24 -04:00
|
|
|
after_save :clear_subcategory_ids
|
2016-12-21 21:13:14 -05:00
|
|
|
after_save :clear_url_cache
|
2023-11-28 10:58:47 -05:00
|
|
|
after_save :publish_discourse_stylesheet
|
|
|
|
after_save :publish_category
|
2015-07-09 22:09:43 -04:00
|
|
|
|
2022-06-08 19:24:30 -04:00
|
|
|
after_save do
|
2022-10-07 11:00:44 -04:00
|
|
|
if saved_change_to_uploaded_logo_id? || saved_change_to_uploaded_logo_dark_id? ||
|
2023-10-20 08:48:06 -04:00
|
|
|
saved_change_to_uploaded_background_id? || saved_change_to_uploaded_background_dark_id?
|
|
|
|
upload_ids = [
|
|
|
|
self.uploaded_logo_id,
|
|
|
|
self.uploaded_logo_dark_id,
|
|
|
|
self.uploaded_background_id,
|
|
|
|
self.uploaded_background_dark_id,
|
|
|
|
]
|
2022-06-08 19:24:30 -04:00
|
|
|
UploadReference.ensure_exist!(upload_ids: upload_ids, target: self)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-12-21 21:13:14 -05:00
|
|
|
after_destroy :reset_topic_ids_cache
|
2023-11-28 10:58:47 -05:00
|
|
|
after_destroy :clear_subcategory_ids
|
2016-12-21 21:13:14 -05:00
|
|
|
after_destroy :publish_category_deletion
|
2018-03-13 15:59:12 -04:00
|
|
|
after_destroy :remove_site_settings
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2016-04-27 07:04:44 -04:00
|
|
|
after_create :delete_category_permalink
|
|
|
|
|
2017-08-31 00:06:56 -04:00
|
|
|
after_update :rename_category_definition, if: :saved_change_to_name?
|
|
|
|
after_update :create_category_permalink, if: :saved_change_to_slug?
|
2014-11-10 23:32:44 -05:00
|
|
|
|
2018-03-27 02:23:35 -04:00
|
|
|
after_commit :trigger_category_created_event, on: :create
|
2018-05-21 05:29:19 -04:00
|
|
|
after_commit :trigger_category_updated_event, on: :update
|
2018-03-27 02:23:35 -04:00
|
|
|
after_commit :trigger_category_destroyed_event, on: :destroy
|
2021-06-23 03:21:11 -04:00
|
|
|
after_commit :clear_site_cache
|
2018-03-27 02:23:35 -04:00
|
|
|
|
2020-11-16 12:18:25 -05:00
|
|
|
after_save_commit :index_search
|
|
|
|
|
2013-10-23 12:58:11 -04:00
|
|
|
belongs_to :parent_category, class_name: "Category"
|
2014-02-05 18:39:26 -05:00
|
|
|
has_many :subcategories, class_name: "Category", foreign_key: "parent_category_id"
|
2013-05-22 15:33:33 -04:00
|
|
|
|
2016-06-07 13:08:59 -04:00
|
|
|
has_many :category_tags, dependent: :destroy
|
2016-05-30 16:37:06 -04:00
|
|
|
has_many :tags, through: :category_tags
|
2023-01-29 19:53:17 -05:00
|
|
|
has_many :none_synonym_tags,
|
|
|
|
-> { where(target_tag_id: nil) },
|
|
|
|
through: :category_tags,
|
|
|
|
source: :tag
|
2016-06-07 13:08:59 -04:00
|
|
|
has_many :category_tag_groups, dependent: :destroy
|
|
|
|
has_many :tag_groups, through: :category_tag_groups
|
2022-04-06 09:08:06 -04:00
|
|
|
|
|
|
|
has_many :category_required_tag_groups, -> { order(order: :asc) }, dependent: :destroy
|
2022-06-30 02:54:20 -04:00
|
|
|
has_many :sidebar_section_links, as: :linkable, dependent: :delete_all
|
2023-01-19 21:29:49 -05:00
|
|
|
has_many :embeddable_hosts, dependent: :destroy
|
2019-10-30 14:49:00 -04:00
|
|
|
|
2023-02-23 14:18:14 -05:00
|
|
|
has_many :category_form_templates, dependent: :destroy
|
|
|
|
has_many :form_templates, through: :category_form_templates
|
|
|
|
|
2016-06-26 13:25:45 -04:00
|
|
|
scope :latest, -> { order("topic_count DESC") }
|
2013-03-02 03:57:02 -05:00
|
|
|
|
2016-06-26 13:25:45 -04:00
|
|
|
scope :secured,
|
2023-11-29 00:38:07 -05:00
|
|
|
->(guardian = nil) do
|
2013-05-13 04:04:03 -04:00
|
|
|
ids = guardian.secure_category_ids if guardian
|
2017-08-31 00:06:56 -04:00
|
|
|
|
2013-05-13 04:04:03 -04:00
|
|
|
if ids.present?
|
2016-06-26 13:25:45 -04:00
|
|
|
where(
|
|
|
|
"NOT categories.read_restricted OR categories.id IN (:cats)",
|
|
|
|
cats: ids,
|
|
|
|
).references(:categories)
|
2013-05-13 04:04:03 -04:00
|
|
|
else
|
2013-08-25 17:18:11 -04:00
|
|
|
where("NOT categories.read_restricted").references(:categories)
|
2013-05-13 04:04:03 -04:00
|
|
|
end
|
2023-11-29 00:38:07 -05:00
|
|
|
end
|
2013-05-13 04:04:03 -04:00
|
|
|
|
2024-11-05 17:27:49 -05:00
|
|
|
TOPIC_CREATION_PERMISSIONS = [:full]
|
|
|
|
POST_CREATION_PERMISSIONS = %i[create_post full]
|
2019-06-26 03:41:07 -04:00
|
|
|
|
|
|
|
scope :topic_create_allowed,
|
2023-11-29 00:38:07 -05:00
|
|
|
->(guardian) do
|
2019-06-26 03:41:07 -04:00
|
|
|
scoped = scoped_to_permissions(guardian, TOPIC_CREATION_PERMISSIONS)
|
|
|
|
|
|
|
|
if !SiteSetting.allow_uncategorized_topics && !guardian.is_staff?
|
|
|
|
scoped = scoped.where.not(id: SiteSetting.uncategorized_category_id)
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
2023-10-05 21:00:22 -04:00
|
|
|
|
2019-06-26 03:41:07 -04:00
|
|
|
scoped
|
2023-11-29 00:38:07 -05:00
|
|
|
end
|
2019-06-26 03:41:07 -04:00
|
|
|
|
2016-06-26 13:25:45 -04:00
|
|
|
scope :post_create_allowed,
|
|
|
|
->(guardian) { scoped_to_permissions(guardian, POST_CREATION_PERMISSIONS) }
|
2016-01-12 06:06:51 -05:00
|
|
|
|
2023-11-28 10:58:47 -05:00
|
|
|
scope :with_ancestors, ->(id) { where(<<~SQL, id) }
|
|
|
|
id IN (
|
|
|
|
WITH RECURSIVE ancestors(category_id) AS (
|
|
|
|
SELECT ?
|
|
|
|
UNION
|
|
|
|
SELECT parent_category_id
|
|
|
|
FROM categories, ancestors
|
|
|
|
WHERE id = ancestors.category_id
|
|
|
|
)
|
|
|
|
SELECT category_id FROM ancestors
|
|
|
|
)
|
|
|
|
SQL
|
|
|
|
|
2024-04-10 10:35:42 -04:00
|
|
|
scope :with_parents, ->(ids) { where(<<~SQL, ids: ids) }
|
|
|
|
id IN (:ids)
|
|
|
|
OR
|
|
|
|
id IN (SELECT DISTINCT parent_category_id FROM categories WHERE id IN (:ids))
|
|
|
|
SQL
|
|
|
|
|
2013-03-02 03:57:02 -05:00
|
|
|
delegate :post_template, to: "self.class"
|
|
|
|
|
2013-07-16 01:44:07 -04:00
|
|
|
# permission is just used by serialization
|
|
|
|
# we may consider wrapping this in another spot
|
2021-10-05 14:12:31 -04:00
|
|
|
attr_accessor :displayable_topics,
|
|
|
|
:permission,
|
|
|
|
:subcategory_ids,
|
|
|
|
:subcategory_list,
|
|
|
|
:notification_level,
|
2024-04-25 09:47:45 -04:00
|
|
|
:has_children,
|
|
|
|
:subcategory_count
|
2013-06-05 02:10:26 -04:00
|
|
|
|
2019-02-01 11:44:37 -05:00
|
|
|
# Allows us to skip creating the category definition topic in tests.
|
|
|
|
attr_accessor :skip_category_definition
|
|
|
|
|
2023-12-08 05:01:08 -05:00
|
|
|
def self.preload_user_fields!(guardian, categories)
|
|
|
|
category_ids = categories.map(&:id)
|
|
|
|
|
|
|
|
# Load notification levels
|
|
|
|
notification_levels = CategoryUser.notification_levels_for(guardian.user)
|
|
|
|
notification_levels.default = CategoryUser.default_notification_level
|
|
|
|
|
|
|
|
# Load permissions
|
|
|
|
allowed_topic_create_ids =
|
|
|
|
if !guardian.is_admin? && !guardian.is_anonymous?
|
|
|
|
Category.topic_create_allowed(guardian).where(id: category_ids).pluck(:id).to_set
|
|
|
|
end
|
|
|
|
|
2024-04-25 09:47:45 -04:00
|
|
|
# Load subcategory counts (used to fill has_children property)
|
|
|
|
subcategory_count =
|
|
|
|
Category.secured(guardian).where.not(parent_category_id: nil).group(:parent_category_id).count
|
2023-12-08 05:01:08 -05:00
|
|
|
|
|
|
|
# Update category attributes
|
|
|
|
categories.each do |category|
|
|
|
|
category.notification_level = notification_levels[category[:id]]
|
|
|
|
|
|
|
|
category.permission = CategoryGroup.permission_types[:full] if guardian.is_admin? ||
|
|
|
|
allowed_topic_create_ids&.include?(category[:id])
|
|
|
|
|
2024-04-25 09:47:45 -04:00
|
|
|
category.has_children = subcategory_count.key?(category[:id])
|
|
|
|
|
|
|
|
category.subcategory_count = subcategory_count[category[:id]] if category.has_children
|
2023-12-08 05:01:08 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-02-20 12:24:30 -05:00
|
|
|
def self.ancestors_of(category_ids)
|
|
|
|
ancestor_ids = []
|
|
|
|
|
|
|
|
SiteSetting.max_category_nesting.times do
|
|
|
|
category_ids =
|
|
|
|
where(id: category_ids)
|
|
|
|
.where.not(parent_category_id: nil)
|
|
|
|
.pluck("DISTINCT parent_category_id")
|
|
|
|
|
|
|
|
ancestor_ids.concat(category_ids)
|
|
|
|
|
|
|
|
break if category_ids.empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
where(id: ancestor_ids)
|
|
|
|
end
|
|
|
|
|
2024-06-14 12:37:32 -04:00
|
|
|
# Perform a search. If a category exists in the result, its ancestors do too.
|
|
|
|
# Also check for prefix matches. If a category has a prefix match, its
|
|
|
|
# ancestors report a match too.
|
|
|
|
scope :tree_search,
|
|
|
|
->(only, except, term) do
|
|
|
|
term = term.strip
|
|
|
|
escaped_term = ActiveRecord::Base.connection.quote(term.downcase)
|
|
|
|
prefix_match = "starts_with(LOWER(categories.name), #{escaped_term})"
|
|
|
|
|
|
|
|
word_match = <<~SQL
|
|
|
|
COALESCE(
|
|
|
|
(
|
|
|
|
SELECT BOOL_AND(position(pattern IN LOWER(categories.name)) <> 0)
|
|
|
|
FROM unnest(regexp_split_to_array(#{escaped_term}, '\s+')) AS pattern
|
|
|
|
),
|
|
|
|
true
|
|
|
|
)
|
|
|
|
SQL
|
|
|
|
|
|
|
|
if except
|
|
|
|
prefix_match =
|
|
|
|
"NOT categories.id IN (#{except.reselect(:id).to_sql}) AND #{prefix_match}"
|
|
|
|
word_match = "NOT categories.id IN (#{except.reselect(:id).to_sql}) AND #{word_match}"
|
|
|
|
end
|
|
|
|
|
|
|
|
if only
|
|
|
|
prefix_match = "categories.id IN (#{only.reselect(:id).to_sql}) AND #{prefix_match}"
|
|
|
|
word_match = "categories.id IN (#{only.reselect(:id).to_sql}) AND #{word_match}"
|
|
|
|
end
|
|
|
|
|
|
|
|
categories =
|
|
|
|
Category.select(
|
|
|
|
"categories.*",
|
|
|
|
"#{prefix_match} AS has_prefix_match",
|
|
|
|
"#{word_match} AS has_word_match",
|
|
|
|
)
|
|
|
|
|
|
|
|
(1...SiteSetting.max_category_nesting).each do
|
|
|
|
categories = Category.from("(#{categories.to_sql}) AS categories")
|
|
|
|
|
|
|
|
subcategory_matches =
|
|
|
|
categories
|
|
|
|
.where.not(parent_category_id: nil)
|
|
|
|
.group("categories.parent_category_id")
|
|
|
|
.select(
|
|
|
|
"categories.parent_category_id AS id",
|
|
|
|
"BOOL_OR(categories.has_prefix_match) AS has_prefix_match",
|
|
|
|
"BOOL_OR(categories.has_word_match) AS has_word_match",
|
|
|
|
)
|
|
|
|
|
|
|
|
categories =
|
|
|
|
Category.joins(
|
|
|
|
"LEFT JOIN (#{subcategory_matches.to_sql}) AS subcategory_matches ON categories.id = subcategory_matches.id",
|
|
|
|
).select(
|
|
|
|
"categories.*",
|
|
|
|
"#{prefix_match} OR COALESCE(subcategory_matches.has_prefix_match, false) AS has_prefix_match",
|
|
|
|
"#{word_match} OR COALESCE(subcategory_matches.has_word_match, false) AS has_word_match",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
categories =
|
|
|
|
Category.from("(#{categories.to_sql}) AS categories").where(has_word_match: true)
|
|
|
|
|
|
|
|
categories.select("has_prefix_match AS matches", :id)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Given a relation, 'matches', which contains category ids and a 'matches'
|
|
|
|
# boolean, and a limit (the maximum number of subcategories per category),
|
|
|
|
# produce a subset of the matches categories annotated with information about
|
|
|
|
# their ancestors.
|
|
|
|
scope :select_descendants,
|
|
|
|
->(matches, limit) do
|
|
|
|
max_nesting = SiteSetting.max_category_nesting
|
|
|
|
|
|
|
|
categories =
|
|
|
|
joins("INNER JOIN (#{matches.to_sql}) AS matches ON matches.id = categories.id").select(
|
|
|
|
"categories.id",
|
|
|
|
"categories.name",
|
|
|
|
"ARRAY[]::record[] AS ancestors",
|
|
|
|
"0 AS depth",
|
|
|
|
"matches.matches",
|
|
|
|
)
|
|
|
|
|
|
|
|
categories = Category.from("(#{categories.to_sql}) AS c1")
|
|
|
|
|
|
|
|
(1...max_nesting).each { |i| categories = categories.joins(<<~SQL) }
|
|
|
|
INNER JOIN LATERAL (
|
|
|
|
(SELECT c#{i}.id, c#{i}.name, c#{i}.ancestors, c#{i}.depth, c#{i}.matches)
|
|
|
|
UNION ALL
|
|
|
|
(SELECT
|
|
|
|
categories.id,
|
|
|
|
categories.name,
|
|
|
|
c#{i}.ancestors || ARRAY[ROW(NOT c#{i}.matches, c#{i}.name)] AS ancestors,
|
|
|
|
c#{i}.depth + 1 as depth,
|
|
|
|
matches.matches
|
|
|
|
FROM categories
|
|
|
|
INNER JOIN matches
|
|
|
|
ON matches.id = categories.id
|
|
|
|
WHERE categories.parent_category_id = c#{i}.id
|
|
|
|
AND c#{i}.depth = #{i - 1}
|
|
|
|
ORDER BY (NOT matches.matches, categories.name)
|
|
|
|
LIMIT #{limit})
|
|
|
|
) c#{i + 1} ON true
|
|
|
|
SQL
|
|
|
|
|
|
|
|
categories.select(
|
|
|
|
"c#{max_nesting}.id",
|
|
|
|
"c#{max_nesting}.ancestors",
|
|
|
|
"c#{max_nesting}.name",
|
|
|
|
"c#{max_nesting}.matches",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
scope :limited_categories_matching,
|
|
|
|
->(only, except, parent_id, term) do
|
|
|
|
joins(<<~SQL).order("c.ancestors || ARRAY[ROW(NOT c.matches, c.name)]")
|
|
|
|
INNER JOIN (
|
|
|
|
WITH matches AS (#{Category.tree_search(only, except, term).to_sql})
|
|
|
|
#{Category.where(parent_category_id: parent_id).select_descendants(Category.from("matches").select(:matches, :id), 5).to_sql}
|
|
|
|
) AS c
|
|
|
|
ON categories.id = c.id
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2023-08-18 11:59:11 -04:00
|
|
|
def self.topic_id_cache
|
|
|
|
@topic_id_cache ||= DistributedCache.new("category_topic_ids")
|
|
|
|
end
|
2016-07-27 20:20:14 -04:00
|
|
|
|
2016-07-18 22:34:54 -04:00
|
|
|
def self.topic_ids
|
2023-08-18 11:59:11 -04:00
|
|
|
topic_id_cache.defer_get_set("ids") { Set.new(Category.pluck(:topic_id).compact) }
|
2016-07-18 22:34:54 -04:00
|
|
|
end
|
|
|
|
|
2016-07-27 20:20:14 -04:00
|
|
|
def self.reset_topic_ids_cache
|
2023-08-18 11:59:11 -04:00
|
|
|
topic_id_cache.clear
|
2016-07-18 22:34:54 -04:00
|
|
|
end
|
|
|
|
|
2016-07-27 20:20:14 -04:00
|
|
|
def reset_topic_ids_cache
|
|
|
|
Category.reset_topic_ids_cache
|
2016-07-18 22:34:54 -04:00
|
|
|
end
|
|
|
|
|
2023-04-03 06:36:59 -04:00
|
|
|
# Accepts an array of slugs with each item in the array
|
|
|
|
# Returns the category ids of the last slug in the array. The slugs array has to follow the proper category
|
|
|
|
# nesting hierarchy. If any of the slug in the array is invalid or if the slugs array does not follow the proper
|
|
|
|
# category nesting hierarchy, nil is returned.
|
|
|
|
#
|
|
|
|
# When only a single slug is provided, the category id of all the categories with that slug is returned.
|
|
|
|
def self.ids_from_slugs(slugs)
|
|
|
|
return [] if slugs.blank?
|
|
|
|
|
|
|
|
params = {}
|
|
|
|
params_index = 0
|
|
|
|
|
|
|
|
sqls =
|
|
|
|
slugs.map do |slug|
|
2023-05-08 20:10:08 -04:00
|
|
|
category_slugs =
|
|
|
|
slug.split(":").first(SiteSetting.max_category_nesting).map { Slug.for(_1, "") }
|
|
|
|
|
2023-04-03 06:36:59 -04:00
|
|
|
sql = ""
|
|
|
|
|
|
|
|
if category_slugs.length == 1
|
|
|
|
params[:"slug_#{params_index}"] = category_slugs.first
|
|
|
|
sql = "SELECT id FROM categories WHERE slug = :slug_#{params_index}"
|
|
|
|
params_index += 1
|
|
|
|
else
|
|
|
|
category_slugs.each_with_index do |category_slug, index|
|
|
|
|
params[:"slug_#{params_index}"] = category_slug
|
|
|
|
|
|
|
|
sql =
|
|
|
|
if index == 0
|
|
|
|
"SELECT id FROM categories WHERE slug = :slug_#{params_index} AND parent_category_id IS NULL"
|
|
|
|
else
|
|
|
|
"SELECT id FROM categories WHERE parent_category_id = (#{sql}) AND slug = :slug_#{params_index}"
|
|
|
|
end
|
|
|
|
|
|
|
|
params_index += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
sql
|
|
|
|
end
|
|
|
|
|
|
|
|
DB.query_single(sqls.join("\nUNION ALL\n"), params)
|
|
|
|
end
|
|
|
|
|
2020-04-09 08:42:24 -04:00
|
|
|
@@subcategory_ids = DistributedCache.new("subcategory_ids")
|
|
|
|
|
2020-04-04 06:31:34 -04:00
|
|
|
def self.subcategory_ids(category_id)
|
2023-03-27 10:23:23 -04:00
|
|
|
@@subcategory_ids.defer_get_set(category_id.to_s) do
|
2020-04-09 08:42:24 -04:00
|
|
|
sql = <<~SQL
|
|
|
|
WITH RECURSIVE subcategories AS (
|
|
|
|
SELECT :category_id id, 1 depth
|
|
|
|
UNION
|
|
|
|
SELECT categories.id, (subcategories.depth + 1) depth
|
|
|
|
FROM categories
|
|
|
|
JOIN subcategories ON subcategories.id = categories.parent_category_id
|
|
|
|
WHERE subcategories.depth < :max_category_nesting
|
|
|
|
)
|
|
|
|
SELECT id FROM subcategories
|
|
|
|
SQL
|
|
|
|
DB.query_single(
|
|
|
|
sql,
|
|
|
|
category_id: category_id,
|
|
|
|
max_category_nesting: SiteSetting.max_category_nesting,
|
2020-04-04 06:31:34 -04:00
|
|
|
)
|
2020-04-09 08:42:24 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear_subcategory_ids
|
|
|
|
@@subcategory_ids.clear
|
|
|
|
end
|
|
|
|
|
|
|
|
def clear_subcategory_ids
|
|
|
|
Category.clear_subcategory_ids
|
2020-04-04 06:31:34 -04:00
|
|
|
end
|
|
|
|
|
2022-12-08 19:34:25 -05:00
|
|
|
def top_level?
|
|
|
|
self.parent_category_id.nil?
|
|
|
|
end
|
|
|
|
|
2013-07-13 21:24:16 -04:00
|
|
|
def self.scoped_to_permissions(guardian, permission_types)
|
2016-06-26 13:25:45 -04:00
|
|
|
if guardian.try(:is_admin?)
|
2014-02-17 11:44:28 -05:00
|
|
|
all
|
2015-05-13 22:19:22 -04:00
|
|
|
elsif !guardian || guardian.anonymous?
|
|
|
|
if permission_types.include?(:readonly)
|
|
|
|
where("NOT categories.read_restricted")
|
|
|
|
else
|
|
|
|
where("1 = 0")
|
|
|
|
end
|
2013-07-13 21:24:16 -04:00
|
|
|
else
|
2016-06-26 13:25:45 -04:00
|
|
|
permissions = permission_types.map { |p| CategoryGroup.permission_types[p] }
|
|
|
|
where(
|
|
|
|
"(:staged AND LENGTH(COALESCE(email_in, '')) > 0 AND email_in_allow_strangers)
|
|
|
|
OR categories.id NOT IN (SELECT category_id FROM category_groups)
|
|
|
|
OR categories.id IN (
|
|
|
|
SELECT category_id
|
|
|
|
FROM category_groups
|
|
|
|
WHERE permission_type IN (:permissions)
|
|
|
|
AND (group_id = :everyone OR group_id IN (SELECT group_id FROM group_users WHERE user_id = :user_id))
|
|
|
|
)",
|
|
|
|
staged: guardian.is_staged?,
|
|
|
|
permissions: permissions,
|
|
|
|
user_id: guardian.user.id,
|
2023-01-02 20:00:42 -05:00
|
|
|
everyone: Group::AUTO_GROUPS[:everyone],
|
|
|
|
)
|
2013-07-13 21:24:16 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-19 10:14:05 -04:00
|
|
|
def self.update_stats
|
|
|
|
topics_with_post_count =
|
|
|
|
Topic
|
|
|
|
.select("topics.category_id, COUNT(*) topic_count, SUM(topics.posts_count) post_count")
|
|
|
|
.where(
|
|
|
|
"topics.id NOT IN (select cc.topic_id from categories cc WHERE topic_id IS NOT NULL)",
|
|
|
|
)
|
|
|
|
.group("topics.category_id")
|
|
|
|
.visible
|
|
|
|
.to_sql
|
|
|
|
|
2018-06-19 02:13:14 -04:00
|
|
|
DB.exec <<~SQL
|
|
|
|
UPDATE categories c
|
2019-12-30 06:20:44 -05:00
|
|
|
SET topic_count = COALESCE(x.topic_count, 0),
|
|
|
|
post_count = COALESCE(x.post_count, 0)
|
|
|
|
FROM (
|
|
|
|
SELECT ccc.id as category_id, stats.topic_count, stats.post_count
|
|
|
|
FROM categories ccc
|
|
|
|
LEFT JOIN (#{topics_with_post_count}) stats
|
|
|
|
ON stats.category_id = ccc.id
|
|
|
|
) x
|
2018-06-19 02:13:14 -04:00
|
|
|
WHERE x.category_id = c.id
|
2019-12-30 06:20:44 -05:00
|
|
|
AND (c.topic_count <> COALESCE(x.topic_count, 0) OR c.post_count <> COALESCE(x.post_count, 0))
|
2018-06-19 02:13:14 -04:00
|
|
|
SQL
|
2014-03-19 10:14:05 -04:00
|
|
|
|
|
|
|
# Yes, there are a lot of queries happening below.
|
|
|
|
# Performing a lot of queries is actually faster than using one big update
|
|
|
|
# statement with sub-selects on large databases with many categories,
|
|
|
|
# topics, and posts.
|
|
|
|
#
|
|
|
|
# The old method with the one query is here:
|
|
|
|
# https://github.com/discourse/discourse/blob/5f34a621b5416a53a2e79a145e927fca7d5471e8/app/models/category.rb
|
|
|
|
#
|
|
|
|
# If you refactor this, test performance on a large database.
|
|
|
|
|
|
|
|
Category.all.each do |c|
|
2014-08-27 15:58:05 -04:00
|
|
|
topics = c.topics.visible
|
|
|
|
topics = topics.where(["topics.id <> ?", c.topic_id]) if c.topic_id
|
2014-03-19 10:14:05 -04:00
|
|
|
c.topics_year = topics.created_since(1.year.ago).count
|
|
|
|
c.topics_month = topics.created_since(1.month.ago).count
|
|
|
|
c.topics_week = topics.created_since(1.week.ago).count
|
|
|
|
c.topics_day = topics.created_since(1.day.ago).count
|
|
|
|
|
|
|
|
posts = c.visible_posts
|
|
|
|
c.posts_year = posts.created_since(1.year.ago).count
|
|
|
|
c.posts_month = posts.created_since(1.month.ago).count
|
|
|
|
c.posts_week = posts.created_since(1.week.ago).count
|
|
|
|
c.posts_day = posts.created_since(1.day.ago).count
|
|
|
|
|
|
|
|
c.save if c.changed?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-13 15:15:51 -05:00
|
|
|
def visible_posts
|
|
|
|
query =
|
|
|
|
Post
|
|
|
|
.joins(:topic)
|
|
|
|
.where(["topics.category_id = ?", self.id])
|
|
|
|
.where("topics.visible = true")
|
|
|
|
.where("posts.deleted_at IS NULL")
|
|
|
|
.where("posts.user_deleted = false")
|
|
|
|
self.topic_id ? query.where(["topics.id <> ?", self.topic_id]) : query
|
|
|
|
end
|
|
|
|
|
2016-08-17 17:23:16 -04:00
|
|
|
# Internal: Generate the text of post prompting to enter category description.
|
2013-04-29 02:33:24 -04:00
|
|
|
def self.post_template
|
|
|
|
I18n.t("category.post_template", replace_paragraph: I18n.t("category.replace_paragraph"))
|
|
|
|
end
|
|
|
|
|
2013-03-02 03:57:02 -05:00
|
|
|
def create_category_definition
|
2019-02-01 11:44:37 -05:00
|
|
|
return if skip_category_definition
|
|
|
|
|
2019-06-06 21:19:57 -04:00
|
|
|
Topic.transaction do
|
|
|
|
t =
|
|
|
|
Topic.new(
|
|
|
|
title: I18n.t("category.topic_prefix", category: name),
|
|
|
|
user: user,
|
|
|
|
pinned_at: Time.now,
|
|
|
|
category_id: id,
|
|
|
|
)
|
|
|
|
t.skip_callbacks = true
|
|
|
|
t.ignore_category_auto_close = true
|
|
|
|
t.delete_topic_timer(TopicTimer.types[:close])
|
|
|
|
t.save!(validate: false)
|
|
|
|
update_column(:topic_id, t.id)
|
|
|
|
post = t.posts.build(raw: description || post_template, user: user)
|
|
|
|
post.save!(validate: false)
|
2020-04-20 15:51:49 -04:00
|
|
|
update_column(:description, post.cooked) if description.present?
|
2019-06-06 21:19:57 -04:00
|
|
|
|
|
|
|
t
|
|
|
|
end
|
2013-03-02 03:57:02 -05:00
|
|
|
end
|
|
|
|
|
2021-09-20 01:50:49 -04:00
|
|
|
def trash_category_definition
|
|
|
|
self.topic&.trash!
|
|
|
|
end
|
|
|
|
|
2022-10-12 13:09:45 -04:00
|
|
|
def clear_related_site_settings
|
2023-05-12 10:09:40 -04:00
|
|
|
SiteSetting.general_category_id = -1 if self.id == SiteSetting.general_category_id
|
2022-10-12 13:09:45 -04:00
|
|
|
end
|
|
|
|
|
2013-03-02 03:57:02 -05:00
|
|
|
def topic_url
|
2015-09-28 02:43:38 -04:00
|
|
|
if has_attribute?("topic_slug")
|
2015-10-01 22:27:38 -04:00
|
|
|
Topic.relative_url(topic_id, read_attribute(:topic_slug))
|
2015-09-28 02:43:38 -04:00
|
|
|
else
|
|
|
|
topic_only_relative_url.try(:relative_url)
|
|
|
|
end
|
2013-03-02 03:57:02 -05:00
|
|
|
end
|
|
|
|
|
2014-10-22 00:48:18 -04:00
|
|
|
def description_text
|
2016-12-07 17:05:14 -05:00
|
|
|
return nil unless self.description
|
2014-10-22 00:48:18 -04:00
|
|
|
|
2019-12-19 10:09:45 -05:00
|
|
|
@@cache_text ||= LruRedux::ThreadSafeCache.new(1000)
|
|
|
|
@@cache_text.getset(self.description) do
|
2020-05-04 23:46:57 -04:00
|
|
|
text = Nokogiri::HTML5.fragment(self.description).text.strip
|
2023-02-26 20:48:48 -05:00
|
|
|
ERB::Util.html_escape(text).html_safe
|
2014-10-22 00:48:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-19 10:09:45 -05:00
|
|
|
def description_excerpt
|
|
|
|
return nil unless self.description
|
|
|
|
|
|
|
|
@@cache_excerpt ||= LruRedux::ThreadSafeCache.new(1000)
|
|
|
|
@@cache_excerpt.getset(self.description) { PrettyText.excerpt(description, 300) }
|
|
|
|
end
|
|
|
|
|
2019-10-08 07:15:08 -04:00
|
|
|
def access_category_via_group
|
|
|
|
Group
|
|
|
|
.joins(:category_groups)
|
|
|
|
.where("category_groups.category_id = ?", self.id)
|
|
|
|
.where("groups.public_admission OR groups.allow_membership_requests")
|
|
|
|
.order(:allow_membership_requests)
|
|
|
|
.first
|
|
|
|
end
|
|
|
|
|
2014-12-03 19:23:59 -05:00
|
|
|
def duplicate_slug?
|
|
|
|
Category.where(slug: self.slug, parent_category_id: parent_category_id).where.not(id: id).any?
|
|
|
|
end
|
|
|
|
|
2013-03-02 03:57:02 -05:00
|
|
|
def ensure_slug
|
2024-05-27 06:27:13 -04:00
|
|
|
return if name.blank?
|
2014-12-20 09:07:29 -05:00
|
|
|
|
|
|
|
self.name.strip!
|
|
|
|
|
|
|
|
if slug.present?
|
2019-10-21 13:35:19 -04:00
|
|
|
# if we don't unescape it first we strip the % from the encoded version
|
2019-10-21 13:33:19 -04:00
|
|
|
slug = SiteSetting.slug_generation_method == "encoded" ? CGI.unescape(self.slug) : self.slug
|
2021-01-12 10:28:33 -05:00
|
|
|
self.slug = Slug.for(slug, "", method: :encoded)
|
2020-04-20 21:50:50 -04:00
|
|
|
|
|
|
|
if self.slug.blank?
|
|
|
|
errors.add(:slug, :invalid)
|
2022-02-16 21:46:06 -05:00
|
|
|
elsif SiteSetting.slug_generation_method == "ascii" && !CGI.unescape(self.slug).ascii_only?
|
|
|
|
errors.add(:slug, I18n.t("category.errors.slug_contains_non_ascii_chars"))
|
2020-04-20 21:50:50 -04:00
|
|
|
elsif duplicate_slug?
|
2022-11-08 19:28:31 -05:00
|
|
|
errors.add(:slug, I18n.t("category.errors.is_already_in_use"))
|
2020-04-20 21:50:50 -04:00
|
|
|
end
|
2014-12-20 09:07:29 -05:00
|
|
|
else
|
|
|
|
# auto slug
|
2015-04-13 10:50:41 -04:00
|
|
|
self.slug = Slug.for(name, "")
|
2014-12-20 09:07:29 -05:00
|
|
|
self.slug = "" if duplicate_slug?
|
2013-04-01 12:26:51 -04:00
|
|
|
end
|
2019-10-30 10:45:34 -04:00
|
|
|
|
|
|
|
# only allow to use category itself id.
|
2023-01-20 13:52:49 -05:00
|
|
|
match_id = /\A(\d+)-category/.match(self.slug)
|
2019-10-30 10:45:34 -04:00
|
|
|
if match_id.present?
|
|
|
|
errors.add(:slug, :invalid) if new_record? || (match_id[1] != self.id.to_s)
|
2015-04-13 10:50:41 -04:00
|
|
|
end
|
2013-03-02 03:57:02 -05:00
|
|
|
end
|
|
|
|
|
2014-03-24 13:36:23 -04:00
|
|
|
def slug_for_url
|
|
|
|
slug.present? ? self.slug : "#{self.id}-category"
|
|
|
|
end
|
|
|
|
|
2015-07-09 22:09:43 -04:00
|
|
|
def publish_category
|
2020-09-15 02:15:42 -04:00
|
|
|
if self.read_restricted
|
|
|
|
group_ids = self.groups.pluck(:id)
|
|
|
|
|
|
|
|
if group_ids.present?
|
|
|
|
MessageBus.publish(
|
|
|
|
"/categories",
|
|
|
|
{ categories: ActiveModel::ArraySerializer.new([self]).as_json },
|
|
|
|
group_ids: group_ids,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
MessageBus.publish(
|
|
|
|
"/categories",
|
|
|
|
{ categories: ActiveModel::ArraySerializer.new([self]).as_json },
|
|
|
|
)
|
|
|
|
end
|
2015-07-09 22:09:43 -04:00
|
|
|
end
|
|
|
|
|
2018-03-13 15:59:12 -04:00
|
|
|
def remove_site_settings
|
|
|
|
SiteSetting.all_settings.each do |s|
|
|
|
|
SiteSetting.set(s[:setting], "") if s[:type] == "category" && s[:value].to_i == self.id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-07-09 22:09:43 -04:00
|
|
|
def publish_category_deletion
|
|
|
|
MessageBus.publish("/categories", deleted_categories: [self.id])
|
2013-04-10 15:53:36 -04:00
|
|
|
end
|
|
|
|
|
2019-10-11 08:31:58 -04:00
|
|
|
# This is used in a validation so has to produce accurate results before the
|
|
|
|
# record has been saved
|
2019-12-04 07:05:41 -05:00
|
|
|
def height_of_ancestors(max_height = SiteSetting.max_category_nesting)
|
2019-10-11 08:31:58 -04:00
|
|
|
parent_id = self.parent_category_id
|
|
|
|
|
|
|
|
return max_height if parent_id == id
|
|
|
|
|
|
|
|
DB.query(<<~SQL, id: id, parent_id: parent_id, max_height: max_height)[0].max
|
|
|
|
WITH RECURSIVE ancestors(parent_category_id, height) AS (
|
|
|
|
SELECT :parent_id :: integer, 0
|
|
|
|
|
|
|
|
UNION ALL
|
|
|
|
|
|
|
|
SELECT
|
|
|
|
categories.parent_category_id,
|
|
|
|
CASE
|
|
|
|
WHEN categories.parent_category_id = :id THEN :max_height
|
|
|
|
ELSE ancestors.height + 1
|
|
|
|
END
|
|
|
|
FROM categories, ancestors
|
|
|
|
WHERE categories.id = ancestors.parent_category_id
|
|
|
|
AND ancestors.height < :max_height
|
|
|
|
)
|
|
|
|
|
|
|
|
SELECT max(height) FROM ancestors
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
|
|
|
# This is used in a validation so has to produce accurate results before the
|
|
|
|
# record has been saved
|
2019-12-04 07:05:41 -05:00
|
|
|
def depth_of_descendants(max_depth = SiteSetting.max_category_nesting)
|
2019-10-11 08:31:58 -04:00
|
|
|
parent_id = self.parent_category_id
|
|
|
|
|
|
|
|
return max_depth if parent_id == id
|
|
|
|
|
|
|
|
DB.query(<<~SQL, id: id, parent_id: parent_id, max_depth: max_depth)[0].max
|
|
|
|
WITH RECURSIVE descendants(id, depth) AS (
|
|
|
|
SELECT :id :: integer, 0
|
|
|
|
|
|
|
|
UNION ALL
|
|
|
|
|
|
|
|
SELECT
|
|
|
|
categories.id,
|
|
|
|
CASE
|
|
|
|
WHEN categories.id = :parent_id THEN :max_depth
|
|
|
|
ELSE descendants.depth + 1
|
|
|
|
END
|
|
|
|
FROM categories, descendants
|
|
|
|
WHERE categories.parent_category_id = descendants.id
|
|
|
|
AND descendants.depth < :max_depth
|
|
|
|
)
|
|
|
|
|
|
|
|
SELECT max(depth) FROM descendants
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2013-10-23 12:58:11 -04:00
|
|
|
def parent_category_validator
|
|
|
|
if parent_category_id
|
2014-07-15 15:19:17 -04:00
|
|
|
errors.add(:base, I18n.t("category.errors.uncategorized_parent")) if uncategorized?
|
2013-10-23 12:58:11 -04:00
|
|
|
|
2019-10-11 08:31:58 -04:00
|
|
|
errors.add(:base, I18n.t("category.errors.self_parent")) if parent_category_id == id
|
|
|
|
|
|
|
|
total_depth = height_of_ancestors + 1 + depth_of_descendants
|
2019-12-04 07:05:41 -05:00
|
|
|
if total_depth > SiteSetting.max_category_nesting
|
|
|
|
errors.add(:base, I18n.t("category.errors.depth"))
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
2013-10-23 12:58:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-10 02:47:47 -04:00
|
|
|
def group_names=(names)
|
|
|
|
# this line bothers me, destroying in AR can not seem to be queued, thinking of extending it
|
|
|
|
category_groups.destroy_all unless new_record?
|
2013-05-17 15:11:37 -04:00
|
|
|
ids = Group.where(name: names.split(",")).pluck(:id)
|
2013-05-10 02:47:47 -04:00
|
|
|
ids.each { |id| category_groups.build(group_id: id) }
|
|
|
|
end
|
|
|
|
|
2013-07-13 21:24:16 -04:00
|
|
|
# will reset permission on a topic to a particular
|
|
|
|
# set.
|
|
|
|
#
|
|
|
|
# Available permissions are, :full, :create_post, :readonly
|
|
|
|
# hash can be:
|
|
|
|
#
|
|
|
|
# :everyone => :full - everyone has everything
|
|
|
|
# :everyone => :readonly, :staff => :full
|
|
|
|
# 7 => 1 # you can pass a group_id and permission id
|
|
|
|
def set_permissions(permissions)
|
|
|
|
self.read_restricted, @permissions = Category.resolve_permissions(permissions)
|
|
|
|
|
|
|
|
# Ideally we can just call .clear here, but it runs SQL, we only want to run it
|
|
|
|
# on save.
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-07-16 01:44:07 -04:00
|
|
|
def permissions=(permissions)
|
|
|
|
set_permissions(permissions)
|
|
|
|
end
|
|
|
|
|
2015-09-17 03:51:32 -04:00
|
|
|
def permissions_params
|
|
|
|
hash = {}
|
|
|
|
category_groups
|
|
|
|
.includes(:group)
|
|
|
|
.each do |category_group|
|
2018-06-04 15:43:26 -04:00
|
|
|
if category_group.group.present?
|
|
|
|
hash[category_group.group_name] = category_group.permission_type
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
2018-06-04 15:43:26 -04:00
|
|
|
end
|
2015-09-17 03:51:32 -04:00
|
|
|
hash
|
|
|
|
end
|
|
|
|
|
2013-07-13 21:24:16 -04:00
|
|
|
def apply_permissions
|
|
|
|
if @permissions
|
|
|
|
category_groups.destroy_all
|
|
|
|
@permissions.each do |group_id, permission_type|
|
|
|
|
category_groups.build(group_id: group_id, permission_type: permission_type)
|
|
|
|
end
|
|
|
|
@permissions = nil
|
2013-04-29 02:33:24 -04:00
|
|
|
end
|
2013-02-21 18:09:56 -05:00
|
|
|
end
|
2013-04-29 02:33:24 -04:00
|
|
|
|
2018-03-26 17:04:55 -04:00
|
|
|
def self.resolve_permissions(permissions)
|
|
|
|
read_restricted = true
|
|
|
|
|
|
|
|
everyone = Group::AUTO_GROUPS[:everyone]
|
|
|
|
full = CategoryGroup.permission_types[:full]
|
|
|
|
|
|
|
|
mapped =
|
|
|
|
permissions.map do |group, permission|
|
|
|
|
group_id = Group.group_id_from_param(group)
|
|
|
|
permission = CategoryGroup.permission_types[permission] unless permission.is_a?(Integer)
|
|
|
|
|
|
|
|
[group_id, permission]
|
|
|
|
end
|
|
|
|
|
|
|
|
mapped.each do |group, permission|
|
|
|
|
return false, [] if group == everyone && permission == full
|
|
|
|
|
|
|
|
read_restricted = false if group == everyone
|
|
|
|
end
|
|
|
|
|
|
|
|
[read_restricted, mapped]
|
|
|
|
end
|
|
|
|
|
2018-07-16 04:10:22 -04:00
|
|
|
def auto_bump_limiter
|
2018-07-17 20:17:33 -04:00
|
|
|
return nil if num_auto_bump_daily.to_i == 0
|
|
|
|
RateLimiter.new(nil, "auto_bump_limit_#{self.id}", 1, 86_400 / num_auto_bump_daily.to_i)
|
2018-07-16 04:10:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def clear_auto_bump_cache!
|
2018-07-17 20:17:33 -04:00
|
|
|
auto_bump_limiter&.clear!
|
2018-07-16 04:10:22 -04:00
|
|
|
end
|
|
|
|
|
2018-07-16 19:33:33 -04:00
|
|
|
def self.auto_bump_topic!
|
2023-08-03 22:53:22 -04:00
|
|
|
Category
|
|
|
|
.joins(:category_setting)
|
|
|
|
.where("category_settings.num_auto_bump_daily > 0")
|
|
|
|
.shuffle
|
|
|
|
.any?(&:auto_bump_topic!)
|
2018-07-16 19:33:33 -04:00
|
|
|
end
|
|
|
|
|
2018-07-16 04:10:22 -04:00
|
|
|
# will automatically bump a single topic
|
|
|
|
# if number of automatically bumped topics is smaller than threshold
|
|
|
|
def auto_bump_topic!
|
2018-07-18 17:33:30 -04:00
|
|
|
return false if num_auto_bump_daily.to_i == 0
|
2018-07-16 04:10:22 -04:00
|
|
|
|
|
|
|
limiter = auto_bump_limiter
|
|
|
|
return false if !limiter.can_perform?
|
|
|
|
|
2018-07-17 20:56:09 -04:00
|
|
|
filters = []
|
|
|
|
DiscourseEvent.trigger(:filter_auto_bump_topics, self, filters)
|
|
|
|
|
|
|
|
relation = Topic
|
|
|
|
|
|
|
|
filters.each { |filter| relation = filter.call(relation) } if filters.length > 0
|
2023-01-09 07:20:10 -05:00
|
|
|
|
2018-07-17 20:56:09 -04:00
|
|
|
topic =
|
|
|
|
relation
|
2018-07-16 04:10:22 -04:00
|
|
|
.visible
|
|
|
|
.listable_topics
|
2019-11-19 09:24:18 -05:00
|
|
|
.exclude_scheduled_bump_topics
|
2018-07-16 04:10:22 -04:00
|
|
|
.where(category_id: self.id)
|
|
|
|
.where("id <> ?", self.topic_id)
|
2023-03-10 00:45:01 -05:00
|
|
|
.where("bumped_at < ?", (self.auto_bump_cooldown_days || 1).days.ago)
|
2018-07-16 04:10:22 -04:00
|
|
|
.where("pinned_at IS NULL AND NOT closed AND NOT archived")
|
|
|
|
.order("bumped_at ASC")
|
|
|
|
.limit(1)
|
2018-07-17 20:17:33 -04:00
|
|
|
.first
|
2018-07-16 04:10:22 -04:00
|
|
|
|
2018-07-17 20:17:33 -04:00
|
|
|
if topic
|
|
|
|
topic.add_small_action(Discourse.system_user, "autobumped", nil, bump: true)
|
2018-07-16 04:10:22 -04:00
|
|
|
limiter.performed!
|
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-01 17:05:15 -04:00
|
|
|
def allowed_tags=(tag_names_arg)
|
2016-07-19 12:30:02 -04:00
|
|
|
DiscourseTagging.add_or_create_tags_by_name(self, tag_names_arg, unlimited: true)
|
2016-05-30 16:37:06 -04:00
|
|
|
end
|
|
|
|
|
2016-06-07 13:08:59 -04:00
|
|
|
def allowed_tag_groups=(group_names)
|
|
|
|
self.tag_groups = TagGroup.where(name: group_names).all.to_a
|
|
|
|
end
|
|
|
|
|
2022-04-06 09:08:06 -04:00
|
|
|
def required_tag_groups=(required_groups)
|
|
|
|
map =
|
|
|
|
Array(required_groups)
|
|
|
|
.map
|
|
|
|
.with_index { |rg, i| [rg["name"], { min_count: rg["min_count"].to_i, order: i }] }
|
|
|
|
.to_h
|
|
|
|
tag_groups = TagGroup.where(name: map.keys)
|
|
|
|
|
|
|
|
self.category_required_tag_groups =
|
|
|
|
tag_groups
|
|
|
|
.map do |tag_group|
|
|
|
|
attrs = map[tag_group.name]
|
|
|
|
CategoryRequiredTagGroup.new(tag_group: tag_group, **attrs)
|
|
|
|
end
|
|
|
|
.sort_by(&:order)
|
2019-10-30 14:49:00 -04:00
|
|
|
end
|
|
|
|
|
2014-07-14 10:16:24 -04:00
|
|
|
def downcase_email
|
2016-02-24 13:47:58 -05:00
|
|
|
self.email_in = (email_in || "").strip.downcase.presence
|
|
|
|
end
|
|
|
|
|
|
|
|
def email_in_validator
|
|
|
|
return if self.email_in.blank?
|
|
|
|
email_in
|
|
|
|
.split("|")
|
|
|
|
.each do |email|
|
2016-07-28 11:57:30 -04:00
|
|
|
escaped = Rack::Utils.escape_html(email)
|
2016-03-08 14:52:04 -05:00
|
|
|
if !Email.is_valid?(email)
|
2016-07-28 11:57:30 -04:00
|
|
|
self.errors.add(:base, I18n.t("category.errors.invalid_email_in", email: escaped))
|
2016-03-08 14:52:04 -05:00
|
|
|
elsif group = Group.find_by_email(email)
|
2016-07-28 11:57:30 -04:00
|
|
|
self.errors.add(
|
|
|
|
:base,
|
|
|
|
I18n.t(
|
|
|
|
"category.errors.email_already_used_in_group",
|
|
|
|
email: escaped,
|
|
|
|
group_name: Rack::Utils.escape_html(group.name),
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2016-07-28 11:57:30 -04:00
|
|
|
)
|
2016-03-08 14:52:04 -05:00
|
|
|
elsif category = Category.where.not(id: self.id).find_by_email(email)
|
2016-07-28 11:57:30 -04:00
|
|
|
self.errors.add(
|
|
|
|
:base,
|
|
|
|
I18n.t(
|
|
|
|
"category.errors.email_already_used_in_category",
|
|
|
|
email: escaped,
|
|
|
|
category_name: Rack::Utils.escape_html(category.name),
|
2023-01-09 07:20:10 -05:00
|
|
|
),
|
2016-07-28 11:57:30 -04:00
|
|
|
)
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
2016-02-24 13:47:58 -05:00
|
|
|
end
|
2014-07-14 10:16:24 -04:00
|
|
|
end
|
|
|
|
|
2014-08-18 11:07:32 -04:00
|
|
|
def downcase_name
|
|
|
|
self.name_lower = name.downcase if self.name
|
|
|
|
end
|
|
|
|
|
2018-12-05 10:43:07 -05:00
|
|
|
def visible_group_names(user)
|
|
|
|
self.groups.visible_groups(user)
|
|
|
|
end
|
|
|
|
|
2013-05-29 04:11:04 -04:00
|
|
|
def secure_group_ids
|
|
|
|
groups.pluck("groups.id") if self.read_restricted?
|
|
|
|
end
|
|
|
|
|
2013-10-17 02:44:56 -04:00
|
|
|
def update_latest
|
|
|
|
latest_post_id =
|
|
|
|
Post
|
|
|
|
.order("posts.created_at desc")
|
|
|
|
.where("NOT hidden")
|
|
|
|
.joins("join topics on topics.id = topic_id")
|
|
|
|
.where("topics.category_id = :id", id: self.id)
|
|
|
|
.limit(1)
|
|
|
|
.pluck("posts.id")
|
|
|
|
.first
|
2023-01-09 07:20:10 -05:00
|
|
|
|
2013-10-17 02:44:56 -04:00
|
|
|
latest_topic_id =
|
|
|
|
Topic
|
|
|
|
.order("topics.created_at desc")
|
|
|
|
.where("visible")
|
|
|
|
.where("topics.category_id = :id", id: self.id)
|
|
|
|
.limit(1)
|
|
|
|
.pluck("topics.id")
|
|
|
|
.first
|
|
|
|
|
2019-04-29 03:32:25 -04:00
|
|
|
self.update(latest_topic_id: latest_topic_id, latest_post_id: latest_post_id)
|
2013-10-17 02:44:56 -04:00
|
|
|
end
|
2013-07-13 21:24:16 -04:00
|
|
|
|
2014-02-08 17:10:48 -05:00
|
|
|
def self.query_parent_category(parent_slug)
|
2019-10-17 16:02:17 -04:00
|
|
|
encoded_parent_slug = CGI.escape(parent_slug) if SiteSetting.slug_generation_method == "encoded"
|
2023-02-12 23:39:45 -05:00
|
|
|
self.where(slug: (encoded_parent_slug || parent_slug), parent_category_id: nil).pick(:id) ||
|
|
|
|
self.where(id: parent_slug.to_i).pick(:id)
|
2014-02-08 17:10:48 -05:00
|
|
|
end
|
|
|
|
|
2015-02-12 12:21:07 -05:00
|
|
|
def self.query_category(slug_or_id, parent_category_id)
|
2019-10-16 16:08:43 -04:00
|
|
|
encoded_slug_or_id = CGI.escape(slug_or_id) if SiteSetting.slug_generation_method == "encoded"
|
|
|
|
self.where(
|
|
|
|
slug: (encoded_slug_or_id || slug_or_id),
|
|
|
|
parent_category_id: parent_category_id,
|
2018-03-05 17:27:30 -05:00
|
|
|
).first || self.where(id: slug_or_id.to_i, parent_category_id: parent_category_id).first
|
2014-02-08 17:10:48 -05:00
|
|
|
end
|
|
|
|
|
2014-02-27 07:44:21 -05:00
|
|
|
def self.find_by_email(email)
|
2016-03-08 14:52:04 -05:00
|
|
|
self.where("string_to_array(email_in, '|') @> ARRAY[?]", Email.downcase(email)).first
|
2014-02-27 07:44:21 -05:00
|
|
|
end
|
|
|
|
|
2014-02-12 17:24:25 -05:00
|
|
|
def has_children?
|
2023-12-12 18:27:14 -05:00
|
|
|
return @has_children if defined?(@has_children)
|
|
|
|
@has_children = (id && Category.where(parent_category_id: id).exists?)
|
2014-02-12 17:24:25 -05:00
|
|
|
end
|
|
|
|
|
2014-01-15 14:11:19 -05:00
|
|
|
def uncategorized?
|
2013-12-17 15:36:15 -05:00
|
|
|
id == SiteSetting.uncategorized_category_id
|
|
|
|
end
|
2014-02-16 12:45:00 -05:00
|
|
|
|
2019-10-16 16:08:43 -04:00
|
|
|
def seeded?
|
|
|
|
[
|
2023-05-17 04:34:14 -04:00
|
|
|
SiteSetting.general_category_id,
|
2019-10-16 16:08:43 -04:00
|
|
|
SiteSetting.meta_category_id,
|
|
|
|
SiteSetting.staff_category_id,
|
|
|
|
SiteSetting.uncategorized_category_id,
|
|
|
|
].include? id
|
|
|
|
end
|
|
|
|
|
2020-05-22 23:25:56 -04:00
|
|
|
def full_slug(separator = "-")
|
2020-10-09 07:51:24 -04:00
|
|
|
start_idx = "#{Discourse.base_path}/c/".size
|
2020-05-22 23:25:56 -04:00
|
|
|
url[start_idx..-1].gsub("/", separator)
|
|
|
|
end
|
|
|
|
|
2020-05-23 00:56:13 -04:00
|
|
|
@@url_cache = DistributedCache.new("category_url")
|
2020-05-22 23:25:56 -04:00
|
|
|
|
2020-05-23 00:56:13 -04:00
|
|
|
def clear_url_cache
|
|
|
|
@@url_cache.clear
|
|
|
|
end
|
2020-05-22 23:25:56 -04:00
|
|
|
|
2020-05-23 00:56:13 -04:00
|
|
|
def url
|
2024-02-21 11:55:48 -05:00
|
|
|
@@url_cache.defer_get_set(self.id.to_s) do
|
2021-06-03 22:32:13 -04:00
|
|
|
"#{Discourse.base_path}/c/#{slug_path.join("/")}/#{self.id}"
|
|
|
|
end
|
2014-02-16 12:45:00 -05:00
|
|
|
end
|
2014-07-18 13:59:54 -04:00
|
|
|
|
2024-05-27 15:30:19 -04:00
|
|
|
alias_method :relative_url, :url
|
|
|
|
|
2020-05-23 00:56:13 -04:00
|
|
|
# If the name changes, try and update the category definition topic too if it's an exact match
|
2014-07-18 13:59:54 -04:00
|
|
|
def rename_category_definition
|
2024-05-27 06:27:13 -04:00
|
|
|
return if topic.blank?
|
2020-05-23 00:56:13 -04:00
|
|
|
old_name = saved_changes.transform_values(&:first)["name"]
|
2014-07-18 13:59:54 -04:00
|
|
|
if topic.title == I18n.t("category.topic_prefix", category: old_name)
|
2017-03-26 02:59:53 -04:00
|
|
|
topic.update_attribute(:title, I18n.t("category.topic_prefix", category: name))
|
2014-07-18 13:59:54 -04:00
|
|
|
end
|
|
|
|
end
|
2014-11-10 23:32:44 -05:00
|
|
|
|
2016-04-27 07:04:44 -04:00
|
|
|
def create_category_permalink
|
2017-08-31 00:06:56 -04:00
|
|
|
old_slug = saved_changes.transform_values(&:first)["slug"]
|
2020-06-18 04:32:14 -04:00
|
|
|
|
2020-10-09 07:51:24 -04:00
|
|
|
url = +"#{Discourse.base_path}/c"
|
2020-03-19 10:56:28 -04:00
|
|
|
url << "/#{parent_category.slug_path.join("/")}" if parent_category_id
|
2020-06-18 04:32:14 -04:00
|
|
|
url << "/#{old_slug}/#{id}"
|
2019-03-13 15:41:57 -04:00
|
|
|
url = Permalink.normalize_url(url)
|
2016-11-17 11:27:06 -05:00
|
|
|
|
|
|
|
if Permalink.where(url: url).exists?
|
|
|
|
Permalink.where(url: url).update_all(category_id: id)
|
|
|
|
else
|
|
|
|
Permalink.create(url: url, category_id: id)
|
2016-04-27 07:04:44 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_category_permalink
|
2020-03-19 10:56:28 -04:00
|
|
|
permalink = Permalink.find_by_url("c/#{slug_path.join("/")}")
|
2016-04-27 07:04:44 -04:00
|
|
|
permalink.destroy if permalink
|
|
|
|
end
|
|
|
|
|
2014-11-10 23:32:44 -05:00
|
|
|
def publish_discourse_stylesheet
|
2017-04-12 10:52:52 -04:00
|
|
|
Stylesheet::Manager.cache.clear
|
2014-11-10 23:32:44 -05:00
|
|
|
end
|
2016-03-14 12:38:29 -04:00
|
|
|
|
2016-12-21 21:13:14 -05:00
|
|
|
def index_search
|
2020-11-09 00:35:37 -05:00
|
|
|
Jobs.enqueue(
|
|
|
|
:index_category_for_search,
|
|
|
|
category_id: self.id,
|
|
|
|
force: saved_change_to_attribute?(:name),
|
|
|
|
)
|
2016-12-21 21:13:14 -05:00
|
|
|
end
|
|
|
|
|
2024-09-03 21:38:46 -04:00
|
|
|
def moderating_group_ids
|
|
|
|
category_moderation_groups.pluck(:group_id)
|
2019-04-17 17:12:32 -04:00
|
|
|
end
|
|
|
|
|
2020-04-22 07:12:33 -04:00
|
|
|
def self.find_by_slug_path(slug_path)
|
|
|
|
return nil if slug_path.empty?
|
|
|
|
return nil if slug_path.size > SiteSetting.max_category_nesting
|
2019-10-16 16:08:43 -04:00
|
|
|
|
2022-02-16 21:46:06 -05:00
|
|
|
slug_path.map! { |slug| CGI.escape(slug.downcase) }
|
2019-10-16 16:08:43 -04:00
|
|
|
|
2020-04-22 07:12:33 -04:00
|
|
|
query =
|
|
|
|
slug_path.inject(nil) do |parent_id, slug|
|
2020-12-18 09:05:01 -05:00
|
|
|
category = Category.where(slug: slug, parent_category_id: parent_id)
|
|
|
|
|
2023-01-20 13:52:49 -05:00
|
|
|
if match_id = /\A(\d+)-category/.match(slug).presence
|
2020-12-18 09:05:01 -05:00
|
|
|
category = category.or(Category.where(id: match_id[1], parent_category_id: parent_id))
|
|
|
|
end
|
|
|
|
|
|
|
|
category.select(:id)
|
2020-04-22 07:12:33 -04:00
|
|
|
end
|
2019-10-15 11:39:09 -04:00
|
|
|
|
2020-04-22 07:12:33 -04:00
|
|
|
Category.find_by_id(query)
|
|
|
|
end
|
|
|
|
|
2020-11-03 09:57:58 -05:00
|
|
|
def self.find_by_slug_path_with_id(slug_path_with_id)
|
|
|
|
slug_path = slug_path_with_id.split("/")
|
|
|
|
|
|
|
|
if slug_path.last =~ /\A\d+\Z/
|
|
|
|
id = slug_path.pop.to_i
|
|
|
|
Category.find_by_id(id)
|
|
|
|
else
|
|
|
|
Category.find_by_slug_path(slug_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-22 07:12:33 -04:00
|
|
|
def self.find_by_slug(category_slug, parent_category_slug = nil)
|
|
|
|
return nil if category_slug.nil?
|
|
|
|
|
|
|
|
find_by_slug_path([parent_category_slug, category_slug].compact)
|
2016-03-14 12:38:29 -04:00
|
|
|
end
|
2017-03-08 11:31:30 -05:00
|
|
|
|
|
|
|
def subcategory_list_includes_topics?
|
2017-03-15 17:34:38 -04:00
|
|
|
subcategory_list_style.end_with?("with_featured_topics")
|
2017-03-08 11:31:30 -05:00
|
|
|
end
|
2018-03-27 02:23:35 -04:00
|
|
|
|
2018-05-21 05:29:19 -04:00
|
|
|
%i[category_created category_updated category_destroyed].each do |event|
|
|
|
|
define_method("trigger_#{event}_event") do
|
|
|
|
DiscourseEvent.trigger(event, self)
|
|
|
|
true
|
|
|
|
end
|
2018-03-27 02:23:35 -04:00
|
|
|
end
|
2019-02-14 00:38:52 -05:00
|
|
|
|
|
|
|
def permissions_compatibility_validator
|
|
|
|
# when saving subcategories
|
|
|
|
if @permissions && parent_category_id.present?
|
|
|
|
return if parent_category.category_groups.empty?
|
|
|
|
|
|
|
|
parent_permissions = parent_category.category_groups.pluck(:group_id, :permission_type)
|
|
|
|
child_permissions =
|
2023-01-09 07:20:10 -05:00
|
|
|
(
|
2019-02-14 00:38:52 -05:00
|
|
|
if @permissions.empty?
|
|
|
|
[[Group[:everyone].id, CategoryGroup.permission_types[:full]]]
|
2023-01-09 07:20:10 -05:00
|
|
|
else
|
2019-02-14 00:38:52 -05:00
|
|
|
@permissions
|
2023-01-09 07:20:10 -05:00
|
|
|
end
|
|
|
|
)
|
2019-02-14 00:38:52 -05:00
|
|
|
check_permissions_compatibility(parent_permissions, child_permissions)
|
|
|
|
|
|
|
|
# when saving parent category
|
|
|
|
elsif @permissions && subcategories.present?
|
|
|
|
return if @permissions.empty?
|
|
|
|
|
|
|
|
parent_permissions = @permissions
|
|
|
|
child_permissions = subcategories_permissions.uniq
|
|
|
|
|
|
|
|
check_permissions_compatibility(parent_permissions, child_permissions)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-06 05:30:52 -04:00
|
|
|
def self.ensure_consistency!
|
2019-06-06 21:19:57 -04:00
|
|
|
sql = <<~SQL
|
|
|
|
SELECT t.id FROM topics t
|
|
|
|
JOIN categories c ON c.topic_id = t.id
|
|
|
|
LEFT JOIN posts p ON p.topic_id = t.id AND p.post_number = 1
|
|
|
|
WHERE p.id IS NULL
|
|
|
|
SQL
|
|
|
|
|
2019-06-07 00:56:31 -04:00
|
|
|
DB.query_single(sql).each { |id| Topic.with_deleted.find_by(id: id).destroy! }
|
2019-06-06 21:19:57 -04:00
|
|
|
|
|
|
|
sql = <<~SQL
|
|
|
|
UPDATE categories c
|
|
|
|
SET topic_id = NULL
|
|
|
|
WHERE c.id IN (
|
|
|
|
SELECT c2.id FROM categories c2
|
|
|
|
LEFT JOIN topics t ON t.id = c2.topic_id AND t.deleted_at IS NULL
|
|
|
|
WHERE t.id IS NULL AND c2.topic_id IS NOT NULL
|
|
|
|
)
|
|
|
|
SQL
|
|
|
|
|
|
|
|
DB.exec(sql)
|
|
|
|
|
2019-06-06 05:30:52 -04:00
|
|
|
Category
|
|
|
|
.joins("LEFT JOIN topics ON categories.topic_id = topics.id AND topics.deleted_at IS NULL")
|
2019-06-06 21:19:57 -04:00
|
|
|
.where("categories.id <> ?", SiteSetting.uncategorized_category_id)
|
2019-06-06 06:20:16 -04:00
|
|
|
.where(topics: { id: nil })
|
2019-06-06 05:30:52 -04:00
|
|
|
.find_each { |category| category.create_category_definition }
|
|
|
|
end
|
|
|
|
|
2024-05-06 11:01:05 -04:00
|
|
|
def slug_path(parent_ids = Set.new)
|
2019-10-30 13:22:32 -04:00
|
|
|
if self.parent_category_id.present?
|
2024-05-06 11:01:05 -04:00
|
|
|
if parent_ids.add?(self.parent_category_id)
|
|
|
|
self.parent_category.slug_path(parent_ids) << self.slug_for_url
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
2019-10-30 13:22:32 -04:00
|
|
|
else
|
|
|
|
[self.slug_for_url]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-08-07 21:18:55 -04:00
|
|
|
def slug_ref(depth: 1)
|
|
|
|
if self.parent_category_id.present?
|
|
|
|
built_ref = [self.slug]
|
|
|
|
parent = self.parent_category
|
|
|
|
while parent.present? && (built_ref.length < depth + 1)
|
|
|
|
built_ref << parent.slug
|
|
|
|
parent = parent.parent_category
|
|
|
|
end
|
|
|
|
built_ref.reverse.join(Category::SLUG_REF_SEPARATOR)
|
|
|
|
else
|
|
|
|
self.slug
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-13 05:53:23 -04:00
|
|
|
def cannot_delete_reason
|
|
|
|
return I18n.t("category.cannot_delete.uncategorized") if self.uncategorized?
|
|
|
|
return I18n.t("category.cannot_delete.has_subcategories") if self.has_children?
|
|
|
|
|
|
|
|
if self.topic_count != 0
|
|
|
|
oldest_topic = self.topics.where.not(id: self.topic_id).order("created_at ASC").limit(1).first
|
|
|
|
if oldest_topic
|
|
|
|
I18n.t(
|
|
|
|
"category.cannot_delete.topic_exists",
|
|
|
|
count: self.topic_count,
|
|
|
|
topic_link: "<a href=\"#{oldest_topic.url}\">#{CGI.escapeHTML(oldest_topic.title)}</a>",
|
|
|
|
)
|
|
|
|
else
|
|
|
|
# This is a weird case, probably indicating a bug.
|
|
|
|
I18n.t("category.cannot_delete.topic_exists_no_oldest", count: self.topic_count)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-28 14:25:26 -04:00
|
|
|
def has_restricted_tags?
|
|
|
|
tags.count > 0 || tag_groups.count > 0
|
|
|
|
end
|
|
|
|
|
2019-02-14 00:38:52 -05:00
|
|
|
private
|
|
|
|
|
2023-03-10 00:45:01 -05:00
|
|
|
def ensure_category_setting
|
|
|
|
self.build_category_setting if self.category_setting.blank?
|
|
|
|
end
|
|
|
|
|
2019-02-14 00:38:52 -05:00
|
|
|
def check_permissions_compatibility(parent_permissions, child_permissions)
|
|
|
|
parent_groups = parent_permissions.map(&:first)
|
2019-03-03 22:49:26 -05:00
|
|
|
|
|
|
|
return if parent_groups.include?(Group[:everyone].id)
|
|
|
|
|
2019-02-14 00:38:52 -05:00
|
|
|
child_groups = child_permissions.map(&:first)
|
2019-04-01 00:34:52 -04:00
|
|
|
only_subcategory_groups = child_groups - parent_groups
|
2019-02-14 00:38:52 -05:00
|
|
|
|
2019-04-01 00:34:52 -04:00
|
|
|
if only_subcategory_groups.present?
|
|
|
|
group_names = Group.where(id: only_subcategory_groups).pluck(:name).join(", ")
|
|
|
|
errors.add(:base, I18n.t("category.errors.permission_conflict", group_names: group_names))
|
|
|
|
end
|
2019-02-14 00:38:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def subcategories_permissions
|
2019-10-14 13:47:15 -04:00
|
|
|
everyone = Group[:everyone].id
|
|
|
|
full = CategoryGroup.permission_types[:full]
|
|
|
|
|
|
|
|
result = DB.query(<<-SQL, id: id, everyone: everyone, full: full)
|
|
|
|
SELECT category_groups.group_id, category_groups.permission_type
|
|
|
|
FROM categories, category_groups
|
|
|
|
WHERE categories.parent_category_id = :id
|
|
|
|
AND categories.id = category_groups.category_id
|
|
|
|
UNION
|
|
|
|
SELECT :everyone, :full
|
|
|
|
FROM categories
|
|
|
|
WHERE categories.parent_category_id = :id
|
|
|
|
AND (
|
|
|
|
SELECT DISTINCT 1
|
|
|
|
FROM category_groups
|
|
|
|
WHERE category_groups.category_id = categories.id
|
|
|
|
) IS NULL
|
|
|
|
SQL
|
|
|
|
|
|
|
|
result.map { |row| [row.group_id, row.permission_type] }
|
2019-02-14 00:38:52 -05:00
|
|
|
end
|
2021-06-23 03:21:11 -04:00
|
|
|
|
|
|
|
def clear_site_cache
|
|
|
|
Site.clear_cache
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_custom_fields_change
|
|
|
|
clear_site_cache
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-05-23 22:48:32 -04:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: categories
|
|
|
|
#
|
2021-04-14 01:54:09 -04:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# name :string(50) not null
|
|
|
|
# color :string(6) default("0088CC"), not null
|
|
|
|
# topic_id :integer
|
|
|
|
# topic_count :integer default(0), not null
|
|
|
|
# created_at :datetime not null
|
|
|
|
# updated_at :datetime not null
|
|
|
|
# user_id :integer not null
|
|
|
|
# topics_year :integer default(0)
|
|
|
|
# topics_month :integer default(0)
|
|
|
|
# topics_week :integer default(0)
|
|
|
|
# slug :string not null
|
|
|
|
# description :text
|
|
|
|
# text_color :string(6) default("FFFFFF"), not null
|
|
|
|
# read_restricted :boolean default(FALSE), not null
|
|
|
|
# auto_close_hours :float
|
|
|
|
# post_count :integer default(0), not null
|
|
|
|
# latest_post_id :integer
|
|
|
|
# latest_topic_id :integer
|
|
|
|
# position :integer
|
|
|
|
# parent_category_id :integer
|
|
|
|
# posts_year :integer default(0)
|
|
|
|
# posts_month :integer default(0)
|
|
|
|
# posts_week :integer default(0)
|
|
|
|
# email_in :string
|
|
|
|
# email_in_allow_strangers :boolean default(FALSE)
|
|
|
|
# topics_day :integer default(0)
|
|
|
|
# posts_day :integer default(0)
|
|
|
|
# allow_badges :boolean default(TRUE), not null
|
|
|
|
# name_lower :string(50) not null
|
|
|
|
# auto_close_based_on_last_post :boolean default(FALSE)
|
|
|
|
# topic_template :text
|
|
|
|
# contains_messages :boolean
|
|
|
|
# sort_order :string
|
|
|
|
# sort_ascending :boolean
|
|
|
|
# uploaded_logo_id :integer
|
|
|
|
# uploaded_background_id :integer
|
|
|
|
# topic_featured_link_allowed :boolean default(TRUE)
|
|
|
|
# all_topics_wiki :boolean default(FALSE), not null
|
|
|
|
# show_subcategory_list :boolean default(FALSE)
|
|
|
|
# num_featured_topics :integer default(3)
|
|
|
|
# default_view :string(50)
|
|
|
|
# subcategory_list_style :string(50) default("rows_with_featured_topics")
|
|
|
|
# default_top_period :string(20) default("all")
|
|
|
|
# mailinglist_mirror :boolean default(FALSE), not null
|
|
|
|
# minimum_required_tags :integer default(0), not null
|
|
|
|
# navigate_to_first_post_after_read :boolean default(FALSE), not null
|
|
|
|
# search_priority :integer default(0)
|
|
|
|
# allow_global_tags :boolean default(FALSE), not null
|
|
|
|
# read_only_banner :string
|
|
|
|
# default_list_filter :string(20) default("all")
|
2021-07-05 18:14:15 -04:00
|
|
|
# allow_unlimited_owner_edits_on_first_post :boolean default(FALSE), not null
|
2021-06-27 15:46:11 -04:00
|
|
|
# default_slow_mode_seconds :integer
|
2022-10-07 11:00:44 -04:00
|
|
|
# uploaded_logo_dark_id :integer
|
2023-10-20 08:48:06 -04:00
|
|
|
# uploaded_background_dark_id :integer
|
2013-05-23 22:48:32 -04:00
|
|
|
#
|
|
|
|
# Indexes
|
|
|
|
#
|
2019-05-02 18:34:12 -04:00
|
|
|
# index_categories_on_email_in (email_in) UNIQUE
|
|
|
|
# index_categories_on_reviewable_by_group_id (reviewable_by_group_id)
|
|
|
|
# index_categories_on_search_priority (search_priority)
|
2021-04-14 01:54:09 -04:00
|
|
|
# index_categories_on_topic_count (topic_count)
|
2019-12-30 01:13:27 -05:00
|
|
|
# unique_index_categories_on_name (COALESCE(parent_category_id, '-1'::integer), name) UNIQUE
|
2021-04-14 01:54:09 -04:00
|
|
|
# unique_index_categories_on_slug (COALESCE(parent_category_id, '-1'::integer), lower((slug)::text)) UNIQUE WHERE ((slug)::text <> ''::text)
|
2013-05-23 22:48:32 -04:00
|
|
|
#
|