lib/decidim/cdtb/users/remover.rb in decidim-cdtb-0.1.6 vs lib/decidim/cdtb/users/remover.rb in decidim-cdtb-0.1.7
- old
+ new
@@ -4,12 +4,13 @@
module Cdtb
module Users
# Remove Decidim::User's
#
class Remover < ::Decidim::Cdtb::Task
- def initialize(csv_path)
+ def initialize(csv_path, reporter_user_email)
@csv_path = csv_path
+ @reporter_user_email = reporter_user_email
progress_bar = { title: "Decidim::User" }
super("USER REMOVER", progress_bar: progress_bar)
end
def prepare_execution(_ctx); end
@@ -23,14 +24,14 @@
CSV.foreach(@csv_path, headers: true, col_sep: ",") do |row|
user = Decidim::User.find_by(id: row[0])
next unless user.present?
- reporter_user = Decidim::User.find_by(email: "support@coditramuntana.com",
+ reporter_user = Decidim::User.find_by(email: @reporter_user_email,
organization: user.organization)
comments = Decidim::Comments::Comment.where(decidim_author_id: user.id)
- manage_comments(comments, reporter_user) unless comments.empty?
+ manage_comments(comments, user, reporter_user) unless comments.empty?
destroy_user(user) if block_user(user, reporter_user)
progress_bar.increment
end
end
@@ -38,14 +39,13 @@
log_task_step("#{@num_applied} users removed")
end
private
- def manage_comments(comments, reporter_user)
+ def manage_comments(comments, user, reporter_user)
comments.find_each do |comment|
- report_comment(comment, reporter_user)
- hide_comment(comment, reporter_user)
+ report_comment(comment, user, reporter_user)
end
end
def block_user(user, reporter_user)
params = {
@@ -89,38 +89,36 @@
puts "ERROR: User #{user.id} not removed"
end
end
end
- def report_comment(comment, reporter_user)
+ def report_comment(comment, user, reporter_user)
params = {
reason: "spam",
details: "Spam message"
}
- form = Decidim::ReportForm.from_params(params)
+ form = Decidim::ReportForm.from_params(params).with_context(context_for_report(user, comment, reporter_user))
+ reportable = GlobalID::Locator.locate_signed(comment.to_sgid.to_s)
- CreateReport.call(form, comment, reporter_user) do
+ Decidim::CreateReport.call(form, reportable, reporter_user) do
on(:ok) do
puts "OK: Comment #{comment.id} of User #{user.id} reported"
end
on(:invalid) do
puts "ERROR: Comment #{comment.id} of User #{user.id} not reported"
end
end
end
- def hide_comment(comment, reporter_user)
- Admin::HideResource.call(comment, reporter_user) do
- on(:ok) do
- puts "OK: Comment #{comment.id} of User #{user.id} hided"
- end
-
- on(:invalid) do
- puts "ERROR: Comment #{comment.id} of User #{user.id} not hided"
- end
- end
+ def context_for_report(user, comment, reporter_user)
+ {
+ current_organization: user.organization,
+ current_component: comment.component,
+ current_user: reporter_user,
+ current_participatory_space: comment.participatory_space
+ }
end
end
end
end
end