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

user_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: 3a935e230ae0cdec8aca3290907a9a956aa0b592 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# frozen_string_literal: true

module Abuse
  class UserTrustScore
    MAX_EVENTS = 100
    SPAMCHECK_HAM_THRESHOLD = 0.5

    def initialize(user)
      @user = user
    end

    def spammer?
      spam_score > SPAMCHECK_HAM_THRESHOLD
    end

    def spam_score
      user_scores.spamcheck.average(:score) || 0.0
    end

    def telesign_score
      user_scores.telesign.order_created_at_desc.first&.score || 0.0
    end

    def arkose_global_score
      user_scores.arkose_global_score.order_created_at_desc.first&.score || 0.0
    end

    def arkose_custom_score
      user_scores.arkose_custom_score.order_created_at_desc.first&.score || 0.0
    end

    def trust_scores_for_source(source)
      user_scores.where(source: source)
    end

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

      Abuse::TrustScore.delete(
        trust_scores_for_source(source)
        .order_created_at_asc
        .limit(count - MAX_EVENTS)
      )
    end

    private

    def user_scores
      Abuse::TrustScore.where(user_id: @user.id)
    end
  end
end