From 0e3acca6d7fa0aa048bd22e19a944e50e30d7e0f Mon Sep 17 00:00:00 2001 From: Pablo Brasero Date: Thu, 20 Nov 2025 12:15:17 +0000 Subject: [PATCH] Move spam scoring in User to SpamScorer --- app/models/user.rb | 16 +--------------- lib/spam_scorer.rb | 4 ++++ lib/spam_scorer/user.rb | 31 +++++++++++++++++++++++++++++++ test/lib/spam_scorer_test.rb | 6 ++++++ test/models/user_test.rb | 5 +++++ 5 files changed, 47 insertions(+), 15 deletions(-) create mode 100644 lib/spam_scorer/user.rb diff --git a/app/models/user.rb b/app/models/user.rb index a4208dc2d..8c641b9a0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -396,21 +396,7 @@ class User < ApplicationRecord ## # return a spam score for a user def spam_score - changeset_score = changesets.size * 50 - trace_score = traces.size * 50 - diary_entry_score = diary_entries.visible.inject(0) { |acc, elem| acc + SpamScorer.new(elem.body).score } - diary_comment_score = diary_comments.visible.inject(0) { |acc, elem| acc + SpamScorer.new(elem.body).score } - report_score = Report.where(:category => "spam", :issue => issues.with_status("open")).distinct.count(:user_id) * 20 - - score = SpamScorer.new(description).score / 4.0 - score += diary_entries.visible.where("created_at > ?", 1.day.ago).count * 10 - score += diary_entry_score / diary_entries.visible.length unless diary_entries.visible.empty? - score += diary_comment_score / diary_comments.visible.length unless diary_comments.visible.empty? - score += report_score - score -= changeset_score - score -= trace_score - - score.to_i + SpamScorer.new_from_user(self).score end ## diff --git a/lib/spam_scorer.rb b/lib/spam_scorer.rb index 546a7d824..6d62c6951 100644 --- a/lib/spam_scorer.rb +++ b/lib/spam_scorer.rb @@ -8,4 +8,8 @@ module SpamScorer def self.new_from_rich_text(text) self::RichText.new(text) end + + def self.new_from_user(user) + self::User.new(user) + end end diff --git a/lib/spam_scorer/user.rb b/lib/spam_scorer/user.rb new file mode 100644 index 000000000..f04668c73 --- /dev/null +++ b/lib/spam_scorer/user.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +module SpamScorer + class User + def initialize(user) + @user = user + end + + def score + changeset_score = user.changesets.size * 50 + trace_score = user.traces.size * 50 + diary_entry_score = user.diary_entries.visible.inject(0) { |acc, elem| acc + SpamScorer.new_from_rich_text(elem.body).score } + diary_comment_score = user.diary_comments.visible.inject(0) { |acc, elem| acc + SpamScorer.new_from_rich_text(elem.body).score } + report_score = Report.where(:category => "spam", :issue => user.issues.with_status("open")).distinct.count(:user_id) * 20 + + score = SpamScorer.new_from_rich_text(user.description).score / 4.0 + score += user.diary_entries.visible.where("created_at > ?", 1.day.ago).count * 10 + score += diary_entry_score / user.diary_entries.visible.length unless user.diary_entries.visible.empty? + score += diary_comment_score / user.diary_comments.visible.length unless user.diary_comments.visible.empty? + score += report_score + score -= changeset_score + score -= trace_score + + score.to_i + end + + private + + attr_reader :user + end +end diff --git a/test/lib/spam_scorer_test.rb b/test/lib/spam_scorer_test.rb index a8a17a7f6..f45c6de90 100644 --- a/test/lib/spam_scorer_test.rb +++ b/test/lib/spam_scorer_test.rb @@ -20,4 +20,10 @@ class SpamScorerTest < ActiveSupport::TestCase scorer = SpamScorer.new_from_rich_text(r) assert_equal 141, scorer.score.round end + + def test_user_spam_score + user = build(:user, :description => "foo [bar](http://example.com/) baz") + scorer = SpamScorer.new_from_user(user) + assert_equal 12, scorer.score + end end diff --git a/test/models/user_test.rb b/test/models/user_test.rb index 1b491236c..d37cc2f4b 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -150,6 +150,11 @@ class UserTest < ActiveSupport::TestCase assert_predicate user, :valid?, "should allow nil value" end + def test_spam_score + user = build(:user, :description => "foo [bar](http://example.com/) baz") + assert_equal 12, user.spam_score + end + def test_follows alice = create(:user, :active) bob = create(:user, :active) -- 2.39.5