diff options
author | ThibG <thib@sitedethib.com> | 2018-10-05 19:01:44 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-05 19:01:44 +0200 |
commit | 515ce8f4697fe7edfdf14c45575a71164b920db9 (patch) | |
tree | 1211e136ba3e886ee8daaa5200babffc42375dd1 /app/validators | |
parent | a9e8f98a9d32c57956d41009bd09a1f33310676e (diff) | |
parent | 00fcdebed758f031b486ec239fd425fc54a180b3 (diff) |
Merge pull request #762 from ThibG/glitch-soc/merge-upstream
Merge upstream changes
Diffstat (limited to 'app/validators')
-rw-r--r-- | app/validators/follow_limit_validator.rb | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/app/validators/follow_limit_validator.rb b/app/validators/follow_limit_validator.rb new file mode 100644 index 000000000..eb083ed85 --- /dev/null +++ b/app/validators/follow_limit_validator.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +class FollowLimitValidator < ActiveModel::Validator + LIMIT = ENV.fetch('MAX_FOLLOWS_THRESHOLD', 7_500).to_i + RATIO = ENV.fetch('MAX_FOLLOWS_RATIO', 1.1).to_f + + def validate(follow) + return if follow.account.nil? || !follow.account.local? + follow.errors.add(:base, I18n.t('users.follow_limit_reached', limit: self.class.limit_for_account(follow.account))) if limit_reached?(follow.account) + end + + class << self + def limit_for_account(account) + if account.following_count < LIMIT + LIMIT + else + account.followers_count * RATIO + end + end + end + + private + + def limit_reached?(account) + account.following_count >= self.class.limit_for_account(account) + end +end |