discourse/lib/validators/max_emojis_validator.rb
Sam Saffron 30990006a9 DEV: enable frozen string literal on all files
This reduces chances of errors where consumers of strings mutate inputs
and reduces memory usage of the app.

Test suite passes now, but there may be some stuff left, so we will run
a few sites on a branch prior to merging
2019-05-13 09:31:32 +08:00

15 lines
572 B
Ruby

# frozen_string_literal: true
class MaxEmojisValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
unescaped_title = PrettyText.unescape_emoji(Emoji.unicode_unescape(CGI::escapeHTML(value)))
if unescaped_title.present? && unescaped_title.scan(/<img.+?class\s*=\s*'(emoji|emoji emoji-custom)'/).size > SiteSetting.max_emojis_in_title
record.errors.add(
attribute, SiteSetting.max_emojis_in_title > 0 ? :max_emojis : :emojis_disabled,
max_emojis_count: SiteSetting.max_emojis_in_title
)
end
end
end