Skip to content

Commit

Permalink
fix: Add 'SPAM_DETECTION_BLOCKING_LEVEL' env var in BlockUsersJob
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentinchampenois committed Nov 14, 2024
1 parent fb2ee46 commit 513e024
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ SPAM_DETECTION_EMAIL
PERFORM_BLOCK_USER
default_value: false
usage: Determine if the bot can perform blocking, default mode is just report
SPAM_DETECTION_BLOCKING_LEVEL
default_value: 0.99
usage: Define which level of spam probability is considered as blocking in job Decidim::SpamDetection::BlockUsersJob (and task rake decidim:spam_detection:block_users)
```

## API usage
Expand Down
18 changes: 10 additions & 8 deletions app/jobs/decidim/spam_detection/block_users_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,29 @@ module SpamDetection
class BlockUsersJob < ApplicationJob
queue_as :default

def perform
Rails.logger.info "Blocking users marked as spam"
users = reported_spams_users
def perform(**args)
level = spam_level(args[:spam_level]&.to_f)
Rails.logger.info "Blocking users marked as spam with spam level: #{level}..."
users = reported_spams_users(level)
Rails.logger.info "Blocking users marked as spam: #{users.count} users found"
users.find_each do |user|
Decidim::SpamDetection::BlockSpamUserCommand.call(user, spam_level).call
Decidim::SpamDetection::BlockSpamUserCommand.call(user, level).call
end
Rails.logger.info "Terminated..."
end

private

def reported_spams_users
def reported_spams_users(level)
@reported_spams_users ||= Decidim::User.where(admin: false, blocked: false, deleted_at: nil)
.where("(extended_data #> '{spam_detection, unreported_at}') is null")
.where("(extended_data #> '{spam_detection, unblocked_at}') is null")
.where("(extended_data -> 'spam_detection' ->> 'spam_probability')::float >= ?", spam_level)
.where("(extended_data -> 'spam_detection' ->> 'spam_probability')::float >= ?", level)
end

def spam_level
Decidim::SpamDetection::SpamUserCommandAdapter::SPAM_LEVEL[:very_sure]
def spam_level(spam_level = nil)
spam_level ||= ENV.fetch("SPAM_DETECTION_BLOCKING_LEVEL", nil)&.to_f
spam_level || Decidim::SpamDetection::SpamUserCommandAdapter::SPAM_LEVEL[:very_sure]
end
end
end
Expand Down

0 comments on commit 513e024

Please sign in to comment.