From ce0a51665ecde210689eb5823e185519d89c4155 Mon Sep 17 00:00:00 2001 From: Arpit Jalan Date: Fri, 19 Oct 2018 20:28:35 +0530 Subject: [PATCH] FIX: count emoji shortcuts in topic title https://meta.discourse.org/t/max-emojis-in-title-set-to-0-conflicting-with-emoji-shortcuts/98368/3?u=techapj --- config/locales/server.en.yml | 3 ++- lib/validators/max_emojis_validator.rb | 5 +++-- spec/components/validators/max_emojis_validator_spec.rb | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index c1ba5847b76..651b276d7f2 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -139,6 +139,7 @@ en: odd: must be odd record_invalid: ! 'Validation failed: %{errors}' max_emojis: "can't have more than %{max_emojis_count} emoji" + emojis_disabled: "can't have emoji" ip_address_already_screened: "is already included in an existing rule" restrict_dependent_destroy: one: "Cannot delete record because a dependent %{record} exists" @@ -1868,7 +1869,7 @@ en: sso_provider_secrets: key: "www.example.com" value: "SSO secret" - + search: within_post: "#%{post_number} by %{username}" types: diff --git a/lib/validators/max_emojis_validator.rb b/lib/validators/max_emojis_validator.rb index 3476cd9c011..5f8e6e6c7a7 100644 --- a/lib/validators/max_emojis_validator.rb +++ b/lib/validators/max_emojis_validator.rb @@ -1,9 +1,10 @@ class MaxEmojisValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) - if Emoji.unicode_unescape(value).scan(/:([\w\-+]+(?::t\d)?):/).size > SiteSetting.max_emojis_in_title + unescaped_title = PrettyText.unescape_emoji(Emoji.unicode_unescape(CGI::escapeHTML(value))) + if unescaped_title.scan(/ SiteSetting.max_emojis_in_title record.errors.add( - attribute, :max_emojis, + attribute, SiteSetting.max_emojis_in_title > 0 ? :max_emojis : :emojis_disabled, max_emojis_count: SiteSetting.max_emojis_in_title ) end diff --git a/spec/components/validators/max_emojis_validator_spec.rb b/spec/components/validators/max_emojis_validator_spec.rb index 351746e44b0..671caaea999 100644 --- a/spec/components/validators/max_emojis_validator_spec.rb +++ b/spec/components/validators/max_emojis_validator_spec.rb @@ -14,7 +14,7 @@ describe MaxEmojisValidator do shared_examples "validating any topic title" do it 'adds an error when emoji count is greater than SiteSetting.max_emojis_in_title' do SiteSetting.max_emojis_in_title = 3 - record.title = '🧐 Lots of emojis here 🎃 :joy: :sunglasses:' + record.title = '🧐 Lots of emojis here 🎃 :joy: :)' validate expect(record.errors[:title][0]).to eq(I18n.t("errors.messages.max_emojis", max_emojis_count: 3))