docbrown/app/services/moderator/delete_user.rb
rhymes cc8bfcb5c0
Enable Rubocop 0.89 cops and fix Lint/ violations (#9709)
* Enable new Lint/ cops and run rubocop -a

* Fixing last remaining things with rubocop -a

* Enable and fix Style/ExplicitBlockArgument and Style/GlobalStdStream

* Forgot parenthesis
2020-08-10 16:57:12 +02:00

47 lines
1.2 KiB
Ruby

module Moderator
class DeleteUser < ManageActivityAndRoles
attr_reader :user, :admin, :user_params
def self.call(admin:, user:, user_params:)
if user_params[:ghostify] == "true"
new(user: user, admin: admin, user_params: user_params).ghostify
else
Users::DeleteWorker.perform_async(user.id, true)
end
end
def ghostify
@ghost = User.find_by(username: "ghost")
reassign_articles
reassign_comments
delete_user
CacheBuster.bust("/ghost")
end
private
def delete_user
Users::DeleteWorker.new.perform(user.id, true)
end
def reassign_comments
return unless user.comments.any?
user.comments.find_each do |comment|
comment.update(user_id: @ghost.id)
end
@ghost.touch(:last_comment_at)
end
def reassign_articles
return unless user.articles.any?
# preload associations that are going to be used during indexing
user.articles.preload(:organization, :tag_taggings, :tags).find_each do |article|
path = "/#{@ghost.username}/#{article.slug}"
article.update_columns(user_id: @ghost.id, path: path)
article.index_to_elasticsearch_inline
end
end
end
end