闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

22 lines
591 B

  1. # frozen_string_literal: true
  2. class NoteLengthValidator < ActiveModel::EachValidator
  3. def validate_each(record, attribute, value)
  4. record.errors.add(attribute, I18n.t('statuses.over_character_limit', max: options[:maximum])) if too_long?(value)
  5. end
  6. private
  7. def too_long?(value)
  8. countable_text(value).mb_chars.grapheme_length > options[:maximum]
  9. end
  10. def countable_text(value)
  11. return '' if value.nil?
  12. value.dup.tap do |new_text|
  13. new_text.gsub!(FetchLinkCardService::URL_PATTERN, 'x' * 23)
  14. new_text.gsub!(Account::MENTION_RE, '@\2')
  15. end
  16. end
  17. end