Class: Decidim::Cdtb::Spam::SpamUsersDetector
- Defined in:
- lib/decidim/cdtb/spam/spam_users_detector.rb
Overview
Detect spam behavior in users
Constant Summary collapse
- URL_REGEX =
rubocop:disable Style/RedundantRegexpEscape
%r{(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}| www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.| (?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})}
Instance Attribute Summary
Attributes inherited from Task
Instance Method Summary collapse
-
#do_execution(context) ⇒ Object
rubocop:disable Metrics/AbcSize.
-
#end_execution(_ctx) ⇒ Object
rubocop:enable Metrics/AbcSize.
-
#initialize(organization = nil) ⇒ SpamUsersDetector
constructor
rubocop:enable Style/RedundantRegexpEscape.
- #prepare_execution(_ctx) ⇒ Object
- #spam_user?(user) ⇒ Boolean
- #total_items ⇒ Object
Methods inherited from Task
Methods included from TasksUtils
#do_log_error, #do_log_info, #log_task_end, #log_task_failure, #log_task_info, #log_task_step, #log_task_title, #logger
Constructor Details
#initialize(organization = nil) ⇒ SpamUsersDetector
rubocop:enable Style/RedundantRegexpEscape
17 18 19 20 21 |
# File 'lib/decidim/cdtb/spam/spam_users_detector.rb', line 17 def initialize(organization = nil) @organization = organization = { title: "Decidim::User" } super("SPAM DETECTOR", progress_bar:) end |
Instance Method Details
#do_execution(context) ⇒ Object
rubocop:disable Metrics/AbcSize
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/decidim/cdtb/spam/spam_users_detector.rb', line 41 def do_execution(context) = context[:progress_bar] filename= "spam_users.csv" filepath= Rails.env.test? ? "tmp/#{filename}" : filename CSV.open(filepath, "w") do |csv| csv_headers = ["ID", "Is suspicious?", "Name", "Email", "Nickname", "Personal URL", "About", "Organization ID", "Organization Name", "Last Sign In At"] csv << csv_headers @users.find_each do |user| suspicious = "NO" if spam_user?(user) suspicious = "YES" @num_applied+= 1 end csv << [user.id, suspicious, user.name, user.email, user.nickname, user.personal_url, user.about, user.organization.id, user.organization.name, user.last_sign_in_at&.strftime(Decidim::Cdtb::STRFTIME_FORMAT)] .increment end end end |
#end_execution(_ctx) ⇒ Object
rubocop:enable Metrics/AbcSize
67 68 69 70 71 72 73 74 |
# File 'lib/decidim/cdtb/spam/spam_users_detector.rb', line 67 def end_execution(_ctx) if @num_applied.positive? log_task_step("#{@num_applied} suspicious users") log_task_step("Suspicious users list exported to spam_users.csv") else log_task_step("There are not suspicious users!!") end end |
#prepare_execution(_ctx) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/decidim/cdtb/spam/spam_users_detector.rb', line 23 def prepare_execution(_ctx) base_query = Decidim::User.where(deleted_at: nil) @users = if @organization.present? base_query.where(organization: @organization) else base_query end @num_users = @users.count log_task_info("Checking #{@num_users} users...") end |
#spam_user?(user) ⇒ Boolean
76 77 78 |
# File 'lib/decidim/cdtb/spam/spam_users_detector.rb', line 76 def spam_user?(user) has_spam_word?(user) || has_spam_url?(user) end |
#total_items ⇒ Object
36 37 38 |
# File 'lib/decidim/cdtb/spam/spam_users_detector.rb', line 36 def total_items @num_users end |