FIX: Do not error if autogenerated channel slug is blank (#19759)

In certain cases, like when `SiteSetting.slug_generation_method`
is set to `none` with certain locales, the autogenerated chat
channel slugs will end up blank. This was causing errors in
unrelated jobs calling `update!` on the channel. Instead, we
should just copy Category behaviour, which does not error
if the autogenerated slug ends up blank. We already allow
for this with chat channel URLs, using `-` in place of the
missing slug.
This commit is contained in:
Martin Brennan 2023-01-06 09:04:52 +10:00 committed by GitHub
parent 6977761593
commit 07cc8c8634
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 9 deletions

View File

@ -28,16 +28,18 @@ class CategoryChannel < ChatChannel
end end
def ensure_slug_ok def ensure_slug_ok
# if we don't unescape it first we strip the % from the encoded version if self.slug.present?
slug = SiteSetting.slug_generation_method == "encoded" ? CGI.unescape(self.slug) : self.slug # if we don't unescape it first we strip the % from the encoded version
self.slug = Slug.for(slug, "", method: :encoded) slug = SiteSetting.slug_generation_method == "encoded" ? CGI.unescape(self.slug) : self.slug
self.slug = Slug.for(slug, "", method: :encoded)
if self.slug.blank? if self.slug.blank?
errors.add(:slug, :invalid) errors.add(:slug, :invalid)
elsif SiteSetting.slug_generation_method == "ascii" && !CGI.unescape(self.slug).ascii_only? 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")) errors.add(:slug, I18n.t("chat.category_channel.errors.slug_contains_non_ascii_chars"))
elsif duplicate_slug? elsif duplicate_slug?
errors.add(:slug, I18n.t("chat.category_channel.errors.is_already_in_use")) errors.add(:slug, I18n.t("chat.category_channel.errors.is_already_in_use"))
end
end end
end end
end end

View File

@ -124,6 +124,13 @@ RSpec.describe CategoryChannel do
channel.validate channel.validate
expect(channel.errors.full_messages).to include("Slug is invalid") expect(channel.errors.full_messages).to include("Slug is invalid")
end end
it "does not add a validation error when autogenerated slug ends up empty" do
SiteSetting.slug_generation_method = "none"
channel.update(name: "は無効です", slug: nil)
expect(channel).to be_valid
expect(channel.slug).to eq("")
end
end end
context "when there is a duplicate slug" do context "when there is a duplicate slug" do