REFACTOR: Cleanup rake tasks based on feedback
Follow up to: [FEATURE: Create a rake task for destroying categories][1]
- `Discourse.system_user` is my friend
- Remove puts statements from rake tasks that don't return anything
- `for_each` is also my friend
- Use `human_users` to also exclude discobot
- Sort/format categories:list
[1]: 092eeb5ca3
This commit is contained in:
parent
a6be0ca5c7
commit
d26aa6e71e
|
@ -16,14 +16,13 @@ class DestroyTask
|
|||
topics = Topic.where(category_id: c.id, pinned_at: nil).where.not(user_id: -1)
|
||||
end
|
||||
@io.puts "There are #{topics.count} topics to delete in #{descriptive_slug} category"
|
||||
topics.each do |topic|
|
||||
topics.find_each do |topic|
|
||||
@io.puts "Deleting #{topic.slug}..."
|
||||
first_post = topic.ordered_posts.first
|
||||
if first_post.nil?
|
||||
return @io.puts "Topic.ordered_posts.first was nil"
|
||||
end
|
||||
system_user = User.find(-1)
|
||||
@io.puts PostDestroyer.new(system_user, first_post).destroy
|
||||
@io.puts PostDestroyer.new(Discourse.system_user, first_post).destroy
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -36,11 +35,10 @@ class DestroyTask
|
|||
topics = Topic.where(category_id: c.id, pinned_at: nil).where.not(user_id: -1)
|
||||
end
|
||||
@io.puts "There are #{topics.count} topics to delete in #{c.slug} category"
|
||||
topics.each do |topic|
|
||||
topics.find_each do |topic|
|
||||
first_post = topic.ordered_posts.first
|
||||
return @io.puts "Topic.ordered_posts.first was nil for topic: #{topic.id}" if first_post.nil?
|
||||
system_user = User.find(-1)
|
||||
PostDestroyer.new(system_user, first_post).destroy
|
||||
PostDestroyer.new(Discourse.system_user, first_post).destroy
|
||||
end
|
||||
topics = Topic.where(category_id: c.id, pinned_at: nil)
|
||||
@io.puts "There are #{topics.count} topics that could not be deleted in #{c.slug} category"
|
||||
|
@ -54,22 +52,19 @@ class DestroyTask
|
|||
end
|
||||
|
||||
def destroy_private_messages
|
||||
pms = Topic.where(archetype: "private_message")
|
||||
current_user = User.find(-1) #system
|
||||
pms.each do |pm|
|
||||
Topic.where(archetype: "private_message").find_each do |pm|
|
||||
@io.puts "Destroying #{pm.slug} pm"
|
||||
first_post = pm.ordered_posts.first
|
||||
@io.puts PostDestroyer.new(current_user, first_post).destroy
|
||||
@io.puts PostDestroyer.new(Discourse.system_user, first_post).destroy
|
||||
end
|
||||
end
|
||||
|
||||
def destroy_category(category_id, destroy_system_topics = false)
|
||||
c = Category.find_by_id(category_id)
|
||||
return @io.puts "A category with the id: #{category_id} could not be found" if c.nil?
|
||||
subcategories = Category.where(parent_category_id: c.id).pluck(:id)
|
||||
subcategories = Category.where(parent_category_id: c.id)
|
||||
@io.puts "There are #{subcategories.count} subcategories to delete" if subcategories
|
||||
subcategories.each do |subcategory_id|
|
||||
s = Category.find_by_id(subcategory_id)
|
||||
subcategories.each do |s|
|
||||
category_topic_destroyer(s, destroy_system_topics)
|
||||
end
|
||||
category_topic_destroyer(c, destroy_system_topics)
|
||||
|
@ -84,14 +79,9 @@ class DestroyTask
|
|||
end
|
||||
|
||||
def destroy_users
|
||||
users = User.where(admin: false, id: 1..Float::INFINITY)
|
||||
@io.puts "There are #{users.count} users to delete"
|
||||
options = {}
|
||||
options[:delete_posts] = true
|
||||
current_user = User.find(-1) #system
|
||||
users.each do |user|
|
||||
User.human_users.where(admin: false).find_each do |user|
|
||||
begin
|
||||
if UserDestroyer.new(current_user).destroy(user, options)
|
||||
if UserDestroyer.new(Discourse.system_user).destroy(user, delete_posts: true)
|
||||
@io.puts "#{user.username} deleted"
|
||||
else
|
||||
@io.puts "#{user.username} not deleted"
|
||||
|
@ -121,7 +111,7 @@ class DestroyTask
|
|||
private
|
||||
|
||||
def category_topic_destroyer(category, destroy_system_topics = false)
|
||||
destroy_topics_log = destroy_topics_in_category(category.id, destroy_system_topics)
|
||||
destroy_topics_in_category(category.id, destroy_system_topics)
|
||||
@io.puts "Destroying #{category.slug} category"
|
||||
category.destroy
|
||||
end
|
||||
|
|
|
@ -39,8 +39,13 @@ end
|
|||
|
||||
desc "Output a list of categories"
|
||||
task "categories:list" => :environment do
|
||||
categories = Category.pluck(:id, :slug, :parent_category_id)
|
||||
categories = Category.where(parent_category_id: nil).order(:slug).pluck(:id, :slug)
|
||||
puts "id category-slug"
|
||||
puts "-- -----------------"
|
||||
categories.each do |c|
|
||||
puts "id: #{c[0]}, slug: #{c[1]}, parent: #{c[2]}"
|
||||
puts "#{c[0]} #{c[1]}"
|
||||
Category.where(parent_category_id: c[0]).order(:slug).pluck(:id, :slug).each do |s|
|
||||
puts " #{s[0]} #{s[1]}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,47 +4,41 @@
|
|||
# content and users from your site.
|
||||
desc "Remove all topics in a category"
|
||||
task "destroy:topics", [:category, :parent_category] => :environment do |t, args|
|
||||
destroy_task = DestroyTask.new
|
||||
category = args[:category]
|
||||
parent_category = args[:parent_category]
|
||||
descriptive_slug = parent_category ? "#{parent_category}/#{category}" : category
|
||||
puts "Going to delete all topics in the #{descriptive_slug} category"
|
||||
destroy_task.destroy_topics(category, parent_category)
|
||||
DestroyTask.new.destroy_topics(category, parent_category)
|
||||
end
|
||||
|
||||
desc "Remove all topics in all categories"
|
||||
task "destroy:topics_all_categories" => :environment do
|
||||
destroy_task = DestroyTask.new
|
||||
puts "Going to delete all topics in all categories..."
|
||||
puts log = destroy_task.destroy_topics_all_categories
|
||||
DestroyTask.new.destroy_topics_all_categories
|
||||
end
|
||||
|
||||
desc "Remove all private messages"
|
||||
task "destroy:private_messages" => :environment do
|
||||
destroy_task = DestroyTask.new
|
||||
puts "Going to delete all private messages..."
|
||||
puts log = destroy_task.destroy_private_messages
|
||||
DestroyTask.new.destroy_private_messages
|
||||
end
|
||||
|
||||
desc "Destroy all groups"
|
||||
task "destroy:groups" => :environment do
|
||||
destroy_task = DestroyTask.new
|
||||
puts "Going to delete all non-default groups..."
|
||||
puts log = destroy_task.destroy_groups
|
||||
DestroyTask.new.destroy_groups
|
||||
end
|
||||
|
||||
desc "Destroy all non-admin users"
|
||||
task "destroy:users" => :environment do
|
||||
destroy_task = DestroyTask.new
|
||||
puts "Going to delete all non-admin users..."
|
||||
puts log = destroy_task.destroy_users
|
||||
DestroyTask.new.destroy_users
|
||||
end
|
||||
|
||||
desc "Destroy site stats"
|
||||
task "destroy:stats" => :environment do
|
||||
destroy_task = DestroyTask.new
|
||||
puts "Going to delete all site stats..."
|
||||
destroy_task.destroy_stats
|
||||
DestroyTask.new.destroy_stats
|
||||
end
|
||||
|
||||
# Example: rake destroy:categories[28,29,44,85]
|
||||
|
|
Loading…
Reference in New Issue