Arpit Jalan 2018-10-19 20:28:35 +05:30
parent dca830cb73
commit ce0a51665e
3 changed files with 6 additions and 4 deletions

View File

@ -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:

View File

@ -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

View File

@ -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))