Class: Decidim::SpamDetection::MarkUsersService

Inherits:
Object
  • Object
show all
Defined in:
app/services/decidim/spam_detection/mark_users_service.rb

Constant Summary collapse

PUBLICY_SEARCHABLE_COLUMNS =
[
  :id,
  :decidim_organization_id,
  :sign_in_count,
  :personal_url,
  :about,
  :avatar,
  :extended_data,
  :followers_count,
  :following_count,
  :invitations_count,
  :failed_attempts,
  :admin
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMarkUsersService

Returns a new instance of MarkUsersService.



24
25
26
27
28
29
30
31
# File 'app/services/decidim/spam_detection/mark_users_service.rb', line 24

def initialize
  @users = Decidim::User.left_outer_joins(:user_moderation)
                        .where(decidim_user_moderations: { decidim_user_id: nil })
                        .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")
  @results = {}
end

Class Method Details

.callObject



33
34
35
36
37
# File 'app/services/decidim/spam_detection/mark_users_service.rb', line 33

def self.call
  return unless Decidim::SpamDetection.service_activated?

  new.ask_and_mark
end

Instance Method Details

#ask_and_markObject



39
40
41
42
43
44
# File 'app/services/decidim/spam_detection/mark_users_service.rb', line 39

def ask_and_mark
  spam_probability_array = Decidim::SpamDetection::ApiProxy.request(cleaned_users)

  mark_spam_users(merge_response_with_users(spam_probability_array))
  notify_admins!
end

#cleaned_usersObject



55
56
57
58
# File 'app/services/decidim/spam_detection/mark_users_service.rb', line 55

def cleaned_users
  @cleaned_users ||= @users.select(PUBLICY_SEARCHABLE_COLUMNS)
                           .map { |u| u.serializable_hash(force_except: true) }
end

#mark_spam_users(probability_array) ⇒ Object



46
47
48
49
50
51
52
53
# File 'app/services/decidim/spam_detection/mark_users_service.rb', line 46

def mark_spam_users(probability_array)
  probability_array.each do |probability_hash|
    result = Decidim::SpamDetection::SpamUserCommandAdapter.call(probability_hash).result
    organization_id = probability_hash["decidim_organization_id"]

    add_to_results(organization_id.to_s, result)
  end
end

#merge_response_with_users(response) ⇒ Object



60
61
62
# File 'app/services/decidim/spam_detection/mark_users_service.rb', line 60

def merge_response_with_users(response)
  response.map { |resp| resp.merge("original_user" => @users.find(resp["id"])) }
end

#notify_admins!Object



70
71
72
# File 'app/services/decidim/spam_detection/mark_users_service.rb', line 70

def notify_admins!
  Decidim::SpamDetection::NotifyAdmins.perform_later(status)
end

#statusObject



64
65
66
67
68
# File 'app/services/decidim/spam_detection/mark_users_service.rb', line 64

def status
  @results.each_with_object({}) do |result, hash|
    hash[result[0]] = result[1].tally
  end
end