Welcome to mirror list, hosted at ThFree Co, Russian Federation.

trust_score.rb « abuse « models « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b7ed504a0ba00d68cb23d949978f54433efa41b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# frozen_string_literal: true

module Abuse
  class TrustScore < ApplicationRecord
    MAX_EVENTS = 100
    SPAMCHECK_HAM_THRESHOLD = 0.5

    self.table_name = 'abuse_trust_scores'

    enum source: Enums::Abuse::Source.sources

    belongs_to :user

    validates :user, presence: true
    validates :score, presence: true
    validates :source, presence: true

    before_create :assign_correlation_id
    after_commit :remove_old_scores

    private

    def assign_correlation_id
      self.correlation_id_value ||= (Labkit::Correlation::CorrelationId.current_id || '')
    end

    def remove_old_scores
      count = user.trust_scores_for_source(source).count
      return unless count > MAX_EVENTS

      TrustScore.delete(
        user.trust_scores_for_source(source)
        .order(created_at: :asc)
        .limit(count - MAX_EVENTS)
      )
    end
  end
end