gab-social/app/validators/follow_limit_validator.rb
Developer 2a8ca2cd56 Added new spam_flag to Accounts, replacing is_flagged_as_spam
• Added:
- new spam_flag to Accounts, replacing is_flagged_as_spam
- null/0: no spam, 1: spam, 2: safe from spam

• Updated:
- Comment, Status to reflect changes

• Todo:
- Fully remove is_flagged_as_spam
- Update SortingQueryBuilder
2021-02-18 23:46:53 -05:00

32 lines
954 B
Ruby

# 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)
adjusted_limit = account.is_pro ? 50000 : LIMIT
adjusted_limit = account.is_spam? ? 0 : LIMIT
adjusted_limit = !account.user.confirmed? ? 10 : LIMIT
if account.following_count < adjusted_limit
adjusted_limit
else
[(account.followers_count * RATIO).round, adjusted_limit].max
end
end
end
private
def limit_reached?(account)
account.following_count >= self.class.limit_for_account(account)
end
end