2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-02-17 00:10:30 -05:00
|
|
|
class MaxEmojisValidator < ActiveModel::EachValidator
|
|
|
|
|
|
|
|
def validate_each(record, attribute, value)
|
2018-10-19 10:58:35 -04:00
|
|
|
unescaped_title = PrettyText.unescape_emoji(Emoji.unicode_unescape(CGI::escapeHTML(value)))
|
2018-10-22 05:38:26 -04:00
|
|
|
if unescaped_title.present? && unescaped_title.scan(/<img.+?class\s*=\s*'(emoji|emoji emoji-custom)'/).size > SiteSetting.max_emojis_in_title
|
2018-02-17 00:10:30 -05:00
|
|
|
record.errors.add(
|
2018-10-19 10:58:35 -04:00
|
|
|
attribute, SiteSetting.max_emojis_in_title > 0 ? :max_emojis : :emojis_disabled,
|
2018-02-17 00:10:30 -05:00
|
|
|
max_emojis_count: SiteSetting.max_emojis_in_title
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|