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

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'app/models/abuse/user_trust_score.rb')
-rw-r--r--app/models/abuse/user_trust_score.rb53
1 files changed, 53 insertions, 0 deletions
diff --git a/app/models/abuse/user_trust_score.rb b/app/models/abuse/user_trust_score.rb
new file mode 100644
index 00000000000..3a935e230ae
--- /dev/null
+++ b/app/models/abuse/user_trust_score.rb
@@ -0,0 +1,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