FIX: Count current penalty if it started more than 6 months ago (#8313)

FIX: Count current penalty if it started more than 6 months ago (#8313)

diff --git a/app/models/trust_level3_requirements.rb b/app/models/trust_level3_requirements.rb
index bfd70fea0f..26224e018e 100644
--- a/app/models/trust_level3_requirements.rb
+++ b/app/models/trust_level3_requirements.rb
@@ -7,9 +7,14 @@ class TrustLevel3Requirements
   class PenaltyCounts
     attr_reader :silenced, :suspended
 
-    def initialize(row)
+    def initialize(user, row)
       @silenced = row['silence_count'] || 0
       @suspended = row['suspend_count'] || 0
+
+      # If penalty started more than 6 months ago and still continues, it will
+      # not be selected by the query from 'penalty_counts'.
+      @silenced += 1 if @silenced == 0 && user.silenced?
+      @suspended += 1 if @suspended == 0 && user.suspended?
     end
 
     def total
@@ -114,7 +119,7 @@ class TrustLevel3Requirements
         AND uh.created_at > :since
     SQL
 
-    PenaltyCounts.new(DB.query_hash(sql, args).first)
+    PenaltyCounts.new(@user, DB.query_hash(sql, args).first)
   end
 
   def min_days_visited
diff --git a/spec/models/trust_level3_requirements_spec.rb b/spec/models/trust_level3_requirements_spec.rb
index 3f124019a3..b027d1e541 100644
--- a/spec/models/trust_level3_requirements_spec.rb
+++ b/spec/models/trust_level3_requirements_spec.rb
@@ -78,6 +78,18 @@ describe TrustLevel3Requirements do
         expect(tl3_requirements.penalty_counts.suspended).to eq(1)
         expect(tl3_requirements.penalty_counts.total).to eq(2)
       end
+
+      it "does return if the user has been silenced or suspended over 6 months ago and continues" do
+        freeze_time 1.year.ago do
+          UserSilencer.new(user, moderator, silenced_till: 10.years.from_now).silence
+          UserHistory.create!(target_user_id: user.id, action: UserHistory.actions[:suspend_user])
+          user.update(suspended_till: 10.years.from_now)
+        end
+
+        expect(tl3_requirements.penalty_counts.silenced).to eq(1)
+        expect(tl3_requirements.penalty_counts.suspended).to eq(1)
+        expect(tl3_requirements.penalty_counts.total).to eq(2)
+      end
     end
 
     it "time_period uses site setting" do

GitHub sha: dfc002d3

Where is this 6 months delay coming from?

1 Like

It is coming from here:

1 Like