The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

28 lines
759 B

  1. # frozen_string_literal: true
  2. class FollowLimitValidator < ActiveModel::Validator
  3. LIMIT = ENV.fetch('MAX_FOLLOWS_THRESHOLD', 7_500).to_i
  4. RATIO = ENV.fetch('MAX_FOLLOWS_RATIO', 1.1).to_f
  5. def validate(follow)
  6. return if follow.account.nil? || !follow.account.local?
  7. follow.errors.add(:base, I18n.t('users.follow_limit_reached', limit: self.class.limit_for_account(follow.account))) if limit_reached?(follow.account)
  8. end
  9. class << self
  10. def limit_for_account(account)
  11. if account.following_count < LIMIT
  12. LIMIT
  13. else
  14. [(account.followers_count * RATIO).round, LIMIT].max
  15. end
  16. end
  17. end
  18. private
  19. def limit_reached?(account)
  20. account.following_count >= self.class.limit_for_account(account)
  21. end
  22. end