diff --git a/app/models/flag_condition.rb b/app/models/flag_condition.rb index 4746d4b7e..be3349e56 100644 --- a/app/models/flag_condition.rb +++ b/app/models/flag_condition.rb @@ -14,9 +14,6 @@ class FlagCondition < ApplicationRecord def accuracy_and_post_count return unless flags_enabled post_feedback_results = posts.pluck(:is_tp) - true_positive_count = post_feedback_results.count(true) - - accuracy = true_positive_count.to_f * 100 / post_feedback_results.count.to_f if accuracy < FlagSetting['min_accuracy'].to_f errors.add(:accuracy, "must be over #{number_to_percentage(FlagSetting['min_accuracy'].to_f, precision: 2)}") @@ -26,6 +23,13 @@ def accuracy_and_post_count errors.add(:post_count, "must be over #{FlagSetting['min_post_count']}") end + def accuracy + post_feedback_results = posts.pluck(:is_tp) + true_positive_count = post_feedback_results.count(true) + + true_positive_count.to_f * 100 / post_feedback_results.count.to_f + end + def self.revalidate_all FlagCondition.where(flags_enabled: true).find_each do |fc| unless fc.validate diff --git a/app/models/post.rb b/app/models/post.rb index e05d89de1..24117105b 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -106,7 +106,31 @@ def autoflag end Rails.logger.warn "[autoflagging] #{id}: lottery begin" - max_flags = [post.site.max_flags_per_post, (FlagSetting['max_flags'] || '3').to_i].min + + # Defined by the scaled_max_flags FlagSetting + # scaled_max_flags = 0,0,0,99.9,99.99,101 would always allow 3 flags, + # 4 flags on 99.9% accuracy, and 5 flags on 99.99% accuracy. + # 101% means six flags is never allowed + scaled_maxes = FlagSetting['scaled_max_flags']&.split(',') + Rails.logger.warn "[autoflagging] #{id}: scaled maxes: #{scaled_maxes}" + if !scaled_maxes.nil? && scaled_maxes.count == 6 + # Check historical accuracy + fake_flag_condition = FlagCondition.new( + sites: Site.all, + max_poster_rep: post.user_reputation, + min_reason_count: 1, + min_weight: post.reasons.sum(&:weight) + ) + + accuracy = fake_flag_condition.accuracy # Decimal number, like 0.998 + + # If the accuracy is higher than all 6 thresholds (indicating 6 flags), index will be null + scaled_max = scaled_maxes.index { |n| n.to_f > accuracy * 100 } || FlagSetting['max_flags'].to_i + else + scaled_max = FlagSetting['max_flags'].to_i + end + + max_flags = [post.site.max_flags_per_post, (FlagSetting['max_flags'] || '3').to_i, scaled_max].min # Send the first flag with Smokey's account; shows up nicely in the flag queue / timeline # At this stage, we know that at least one user has a matching flag_condition (thus 99.x% accuracy)