Skip to content

A platform for community discussion. Free, open, simple.

FIX: count emoji shortcuts in topic title

Arpit Jalance0a51665ecd
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(/<img.+?class\s*=\s*'(emoji|emoji emoji-custom)'/).size > 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))