Class: DestroyTask

Inherits:
Object
  • Object
show all
Defined in:
app/services/destroy_task.rb

Instance Method Summary collapse

Constructor Details

#initialize(io = STDOUT) ⇒ DestroyTask

Returns a new instance of DestroyTask.



4
5
6
# File 'app/services/destroy_task.rb', line 4

def initialize(io = STDOUT)
  @io = io
end

Instance Method Details

#destroy_category(category_id, destroy_system_topics = false) ⇒ Object



59
60
61
62
63
64
65
66
# File 'app/services/destroy_task.rb', line 59

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)
  @io.puts "There are #{subcategories.count} subcategories to delete" if subcategories
  subcategories.each { |s| category_topic_destroyer(s, destroy_system_topics) }
  category_topic_destroyer(c, destroy_system_topics)
end

#destroy_groupsObject



68
69
70
71
72
73
74
# File 'app/services/destroy_task.rb', line 68

def destroy_groups
  groups = Group.where(automatic: false)
  groups.each do |group|
    @io.puts "destroying group: #{group.id}"
    @io.puts group.destroy
  end
end

#destroy_private_messagesObject



49
50
51
52
53
54
55
56
57
# File 'app/services/destroy_task.rb', line 49

def destroy_private_messages
  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(Discourse.system_user, first_post).destroy
    end
end

#destroy_statsObject



103
104
105
106
107
108
109
110
111
# File 'app/services/destroy_task.rb', line 103

def destroy_stats
  ApplicationRequest.delete_all
  IncomingLink.delete_all
  UserVisit.delete_all
  UserProfileView.delete_all
  UserProfile.update_all(views: 0)
  PostAction.unscoped.delete_all
  EmailLog.delete_all
end

#destroy_topics(category, parent_category = nil, delete_system_topics = false) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/services/destroy_task.rb', line 8

def destroy_topics(category, parent_category = nil, delete_system_topics = false)
  c = Category.find_by_slug(category, parent_category)
  descriptive_slug = parent_category ? "#{parent_category}/#{category}" : category
  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"
  topics.find_each do |topic|
    @io.puts "Deleting #{topic.slug}..."
    first_post = topic.ordered_posts.first
    return @io.puts "Topic.ordered_posts.first was nil" if first_post.nil?
    @io.puts PostDestroyer.new(Discourse.system_user, first_post).destroy
  end
end

#destroy_topics_all_categoriesObject



44
45
46
47
# File 'app/services/destroy_task.rb', line 44

def destroy_topics_all_categories
  categories = Category.all
  categories.each { |c| @io.puts destroy_topics(c.slug, c.parent_category&.slug) }
end

#destroy_topics_in_category(category_id, delete_system_topics = false) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/services/destroy_task.rb', line 26

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"
  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?
    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"
end

#destroy_usersObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/services/destroy_task.rb', line 76

def destroy_users
  User
    .human_users
    .where(admin: false)
    .find_each do |user|
      begin
        if UserDestroyer.new(Discourse.system_user).destroy(
             user,
             delete_posts: true,
             context: "destroy task",
           )
          @io.puts "#{user.username} deleted"
        else
          @io.puts "#{user.username} not deleted"
        end
      rescue UserDestroyer::PostsExistError
        raise Discourse::InvalidAccess.new(
                "User #{user.username} has #{user.post_count} posts, so can't be deleted.",
              )
      rescue NoMethodError
        @io.puts "#{user.username} could not be deleted"
      rescue Discourse::InvalidAccess => e
        @io.puts "#{user.username} #{e.message}"
      end
    end
end