From a46ab86adfc9e4ea182af9a555237f17071e194c Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Thu, 4 Oct 2018 17:36:11 +0200 Subject: Limit the number of people that can be followed from one account (#8807) Configurable soft limit of 7,500, and above that, configurable ratio of 1.1 * followers, controlled by: - MAX_FOLLOWS_THRESHOLD - MAX_FOLLOWS_RATIO Fix #2311 --- app/validators/follow_limit_validator.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 app/validators/follow_limit_validator.rb (limited to 'app/validators/follow_limit_validator.rb') 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 -- cgit