2022-11-02 09:41:30 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class CategoryChannel < ChatChannel
|
|
|
|
alias_attribute :category, :chatable
|
|
|
|
|
|
|
|
delegate :read_restricted?, to: :category
|
|
|
|
delegate :url, to: :chatable, prefix: true
|
|
|
|
|
|
|
|
%i[category_channel? public_channel? chatable_has_custom_fields?].each do |name|
|
|
|
|
define_method(name) { true }
|
|
|
|
end
|
|
|
|
|
|
|
|
def allowed_group_ids
|
|
|
|
return if !read_restricted?
|
|
|
|
|
|
|
|
staff_groups = Group::AUTO_GROUPS.slice(:staff, :moderators, :admins).values
|
|
|
|
category.secure_group_ids.to_a.concat(staff_groups)
|
|
|
|
end
|
|
|
|
|
2022-11-04 10:06:24 -04:00
|
|
|
def title(_ = nil)
|
2022-11-02 09:41:30 -04:00
|
|
|
name.presence || category.name
|
|
|
|
end
|
2022-11-04 10:06:24 -04:00
|
|
|
|
2022-11-08 19:28:31 -05:00
|
|
|
def generate_auto_slug
|
|
|
|
return if self.slug.present?
|
|
|
|
self.slug = Slug.for(self.title.strip, "")
|
|
|
|
self.slug = "" if duplicate_slug?
|
|
|
|
end
|
|
|
|
|
|
|
|
def ensure_slug_ok
|
|
|
|
# if we don't unescape it first we strip the % from the encoded version
|
|
|
|
slug = SiteSetting.slug_generation_method == "encoded" ? CGI.unescape(self.slug) : self.slug
|
|
|
|
self.slug = Slug.for(slug, "", method: :encoded)
|
|
|
|
|
|
|
|
if self.slug.blank?
|
|
|
|
errors.add(:slug, :invalid)
|
|
|
|
elsif SiteSetting.slug_generation_method == "ascii" && !CGI.unescape(self.slug).ascii_only?
|
|
|
|
errors.add(:slug, I18n.t("chat.category_channel.errors.slug_contains_non_ascii_chars"))
|
|
|
|
elsif duplicate_slug?
|
|
|
|
errors.add(:slug, I18n.t("chat.category_channel.errors.is_already_in_use"))
|
|
|
|
end
|
2022-11-04 10:06:24 -04:00
|
|
|
end
|
2022-11-02 09:41:30 -04:00
|
|
|
end
|