1
0

reaction_validator.rb 927 B

12345678910111213141516171819202122232425262728
  1. # frozen_string_literal: true
  2. class ReactionValidator < ActiveModel::Validator
  3. SUPPORTED_EMOJIS = Oj.load_file(Rails.root.join('app', 'javascript', 'mastodon', 'features', 'emoji', 'emoji_map.json').to_s).keys.freeze
  4. LIMIT = 8
  5. def validate(reaction)
  6. return if reaction.name.blank?
  7. reaction.errors.add(:name, I18n.t('reactions.errors.unrecognized_emoji')) if reaction.custom_emoji_id.blank? && !unicode_emoji?(reaction.name)
  8. reaction.errors.add(:base, I18n.t('reactions.errors.limit_reached')) if new_reaction?(reaction) && limit_reached?(reaction)
  9. end
  10. private
  11. def unicode_emoji?(name)
  12. SUPPORTED_EMOJIS.include?(name)
  13. end
  14. def new_reaction?(reaction)
  15. !reaction.announcement.announcement_reactions.exists?(name: reaction.name)
  16. end
  17. def limit_reached?(reaction)
  18. reaction.announcement.announcement_reactions.where.not(name: reaction.name).count('distinct name') >= LIMIT
  19. end
  20. end