Class: Decidim::Cdtb::Users::Remover

Inherits:
Task
  • Object
show all
Defined in:
lib/decidim/cdtb/users/remover.rb

Overview

Remove Decidim::User’s

rubocop:disable Metrics/ClassLength

Instance Attribute Summary

Attributes inherited from Task

#num_applied, #title

Instance Method Summary collapse

Methods inherited from Task

#execute!, #finish, #init

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_id, csv_path, reporter_user_email) ⇒ Remover



10
11
12
13
14
15
16
# File 'lib/decidim/cdtb/users/remover.rb', line 10

def initialize(organization_id, csv_path, reporter_user_email)
  @organization= Decidim::Organization.find_by(id: organization_id)
  @csv_path = csv_path
  @reporter_user_email = reporter_user_email
  progress_bar = { title: "Decidim::User" }
  super("USER REMOVER", progress_bar:)
end

Instance Method Details

#do_execution(context) ⇒ Object

rubocop:disable Metrics/AbcSize



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/decidim/cdtb/users/remover.rb', line 25

def do_execution(context)
  progress_bar = context[:progress_bar]

  reporter_user = Decidim::User.find_by(email: @reporter_user_email, organization: @organization)
  emails_on_moderations = @organization.users.where(email_on_moderations: true).pluck(:email)

  disable_email_moderations(emails_on_moderations)

  CSV.foreach(@csv_path, headers: true, col_sep: ",") do |row|
    user = Decidim::User.find_by(id: row[0])
    next unless user.present?

    comments = Decidim::Comments::Comment.where(decidim_author_id: user.id)
    manage_comments(comments, user, reporter_user) unless comments.empty?
    if block_user(user, reporter_user)
      remove_action_logs_by(user)
      destroy_user(user)
    end
    progress_bar.increment
  end
ensure
  enable_email_moderations(emails_on_moderations)
end

#end_execution(_ctx) ⇒ Object

rubocop:enable Metrics/AbcSize



50
51
52
# File 'lib/decidim/cdtb/users/remover.rb', line 50

def end_execution(_ctx)
  log_task_step("#{@num_applied} users removed")
end

#prepare_execution(_ctx) ⇒ Object



18
# File 'lib/decidim/cdtb/users/remover.rb', line 18

def prepare_execution(_ctx); end

#total_itemsObject



20
21
22
# File 'lib/decidim/cdtb/users/remover.rb', line 20

def total_items
  File.open(@csv_path).readlines.size - 1
end