2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-31 08:32:21 +08:00
|
|
|
class DestroyTask
|
2019-07-18 02:41:13 +08:00
|
|
|
|
|
|
|
def initialize(io = $stdout)
|
|
|
|
@io = io
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy_topics(category, parent_category = nil, delete_system_topics = false)
|
2018-09-10 19:52:14 +08:00
|
|
|
c = Category.find_by_slug(category, parent_category)
|
|
|
|
descriptive_slug = parent_category ? "#{parent_category}/#{category}" : category
|
2019-07-18 02:41:13 +08:00
|
|
|
return @io.puts "A category with the slug: #{descriptive_slug} could not be found" if c.nil?
|
|
|
|
if delete_system_topics
|
|
|
|
topics = Topic.where(category_id: c.id, pinned_at: nil)
|
|
|
|
else
|
|
|
|
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"
|
2019-07-19 09:15:01 +08:00
|
|
|
topics.find_each do |topic|
|
2019-07-18 02:41:13 +08:00
|
|
|
@io.puts "Deleting #{topic.slug}..."
|
2018-03-31 08:32:21 +08:00
|
|
|
first_post = topic.ordered_posts.first
|
|
|
|
if first_post.nil?
|
2019-07-18 02:41:13 +08:00
|
|
|
return @io.puts "Topic.ordered_posts.first was nil"
|
2018-03-31 08:32:21 +08:00
|
|
|
end
|
2019-07-19 09:15:01 +08:00
|
|
|
@io.puts PostDestroyer.new(Discourse.system_user, first_post).destroy
|
2019-07-18 02:41:13 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy_topics_in_category(category_id, delete_system_topics = false)
|
|
|
|
c = Category.find(category_id)
|
|
|
|
return @io.puts "A category with the id: #{category_id} could not be found" if c.nil?
|
|
|
|
if delete_system_topics
|
|
|
|
topics = Topic.where(category_id: c.id, pinned_at: nil)
|
|
|
|
else
|
|
|
|
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"
|
2019-07-19 09:15:01 +08:00
|
|
|
topics.find_each do |topic|
|
2019-07-18 02:41:13 +08:00
|
|
|
first_post = topic.ordered_posts.first
|
|
|
|
return @io.puts "Topic.ordered_posts.first was nil for topic: #{topic.id}" if first_post.nil?
|
2019-07-19 09:15:01 +08:00
|
|
|
PostDestroyer.new(Discourse.system_user, first_post).destroy
|
2018-03-31 08:32:21 +08:00
|
|
|
end
|
2019-07-18 02:41:13 +08:00
|
|
|
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"
|
2018-03-31 08:32:21 +08:00
|
|
|
end
|
|
|
|
|
2019-07-18 02:41:13 +08:00
|
|
|
def destroy_topics_all_categories
|
2018-03-31 08:32:21 +08:00
|
|
|
categories = Category.all
|
|
|
|
categories.each do |c|
|
2019-07-18 02:41:13 +08:00
|
|
|
@io.puts destroy_topics(c.slug, c.parent_category&.slug)
|
2018-03-31 08:32:21 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-18 02:41:13 +08:00
|
|
|
def destroy_private_messages
|
2019-07-19 09:15:01 +08:00
|
|
|
Topic.where(archetype: "private_message").find_each do |pm|
|
2019-07-18 02:41:13 +08:00
|
|
|
@io.puts "Destroying #{pm.slug} pm"
|
2018-03-31 08:32:21 +08:00
|
|
|
first_post = pm.ordered_posts.first
|
2019-07-19 09:15:01 +08:00
|
|
|
@io.puts PostDestroyer.new(Discourse.system_user, first_post).destroy
|
2019-07-18 02:41:13 +08:00
|
|
|
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?
|
2019-07-19 09:15:01 +08:00
|
|
|
subcategories = Category.where(parent_category_id: c.id)
|
2019-07-18 02:41:13 +08:00
|
|
|
@io.puts "There are #{subcategories.count} subcategories to delete" if subcategories
|
2019-07-19 09:15:01 +08:00
|
|
|
subcategories.each do |s|
|
2019-07-18 02:41:13 +08:00
|
|
|
category_topic_destroyer(s, destroy_system_topics)
|
2018-03-31 08:32:21 +08:00
|
|
|
end
|
2019-07-18 02:41:13 +08:00
|
|
|
category_topic_destroyer(c, destroy_system_topics)
|
2018-03-31 08:32:21 +08:00
|
|
|
end
|
|
|
|
|
2019-07-18 02:41:13 +08:00
|
|
|
def destroy_groups
|
2018-03-31 08:32:21 +08:00
|
|
|
groups = Group.where(automatic: false)
|
|
|
|
groups.each do |group|
|
2019-07-18 02:41:13 +08:00
|
|
|
@io.puts "destroying group: #{group.id}"
|
|
|
|
@io.puts group.destroy
|
2018-03-31 08:32:21 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-18 02:41:13 +08:00
|
|
|
def destroy_users
|
2019-07-19 09:15:01 +08:00
|
|
|
User.human_users.where(admin: false).find_each do |user|
|
2018-03-31 08:32:21 +08:00
|
|
|
begin
|
2019-07-19 09:15:01 +08:00
|
|
|
if UserDestroyer.new(Discourse.system_user).destroy(user, delete_posts: true)
|
2019-07-18 02:41:13 +08:00
|
|
|
@io.puts "#{user.username} deleted"
|
2018-03-31 08:32:21 +08:00
|
|
|
else
|
2019-07-18 02:41:13 +08:00
|
|
|
@io.puts "#{user.username} not deleted"
|
2018-03-31 08:32:21 +08:00
|
|
|
end
|
|
|
|
rescue UserDestroyer::PostsExistError
|
|
|
|
raise Discourse::InvalidAccess.new("User #{user.username} has #{user.post_count} posts, so can't be deleted.")
|
|
|
|
rescue NoMethodError
|
2019-07-18 02:41:13 +08:00
|
|
|
@io.puts "#{user.username} could not be deleted"
|
2020-06-01 23:55:08 +08:00
|
|
|
rescue Discourse::InvalidAccess => e
|
|
|
|
@io.puts "#{user.username} #{e.message}"
|
2018-03-31 08:32:21 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-18 02:41:13 +08:00
|
|
|
def destroy_stats
|
2020-11-26 13:29:39 +08:00
|
|
|
ApplicationRequest.delete_all
|
|
|
|
IncomingLink.delete_all
|
|
|
|
UserVisit.delete_all
|
|
|
|
UserProfileView.delete_all
|
|
|
|
UserProfile.update_all(views: 0)
|
|
|
|
PostAction.unscoped.delete_all
|
|
|
|
EmailLog.delete_all
|
2018-03-31 08:32:21 +08:00
|
|
|
end
|
2019-07-18 02:41:13 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def category_topic_destroyer(category, destroy_system_topics = false)
|
2019-07-19 09:15:01 +08:00
|
|
|
destroy_topics_in_category(category.id, destroy_system_topics)
|
2019-07-18 02:41:13 +08:00
|
|
|
@io.puts "Destroying #{category.slug} category"
|
|
|
|
category.destroy
|
|
|
|
end
|
|
|
|
|
2018-03-31 08:32:21 +08:00
|
|
|
end
|