2024-08-15 16:49:02 +00:00
|
|
|
# Federated Computer, Inc.
|
|
|
|
# David Sainty <saint@federated.computer> 2024 A.D.
|
|
|
|
# Gossamer Threads to Discourse -- CleanUp Script
|
2024-08-18 09:36:56 +00:00
|
|
|
# v0.16 Add parallel deletion of posts.
|
2024-08-15 16:49:02 +00:00
|
|
|
|
2024-08-18 09:36:56 +00:00
|
|
|
require 'concurrent-ruby'
|
2024-08-15 16:49:02 +00:00
|
|
|
require File.expand_path("../../../../config/environment", __FILE__)
|
2024-06-17 09:42:33 +00:00
|
|
|
|
|
|
|
class GossamerForumsCleaner
|
2024-06-17 09:43:33 +00:00
|
|
|
def cleanup_users
|
2024-06-17 09:42:33 +00:00
|
|
|
puts "Cleaning up imported users..."
|
|
|
|
# Find all users imported from Gossamer Forums and delete them
|
|
|
|
UserCustomField.where(name: 'original_gossamer_id').each do |field|
|
|
|
|
user = User.find_by(id: field.user_id)
|
|
|
|
if user
|
|
|
|
puts "Deleting user #{user.username} (ID: #{user.id})"
|
|
|
|
user.destroy
|
|
|
|
end
|
|
|
|
end
|
2024-06-17 09:43:33 +00:00
|
|
|
end
|
2024-06-17 09:42:33 +00:00
|
|
|
|
2024-06-17 09:43:33 +00:00
|
|
|
def cleanup_categories
|
2024-06-17 09:42:33 +00:00
|
|
|
puts "Cleaning up imported categories..."
|
|
|
|
# Find all categories that were imported and delete them
|
|
|
|
CategoryCustomField.where(name: 'original_gossamer_id').each do |field|
|
|
|
|
category = Category.find_by(id: field.category_id)
|
|
|
|
if category
|
|
|
|
puts "Deleting category #{category.name} (ID: #{category.id})"
|
|
|
|
category.destroy
|
|
|
|
end
|
|
|
|
end
|
2024-06-17 09:43:33 +00:00
|
|
|
end
|
2024-06-17 09:42:33 +00:00
|
|
|
|
2024-06-17 09:43:33 +00:00
|
|
|
def cleanup_topics
|
|
|
|
puts "Cleaning up imported topics..."
|
2024-06-17 09:42:33 +00:00
|
|
|
# Find all topics that were imported and delete them
|
|
|
|
TopicCustomField.where(name: 'original_gossamer_id').each do |field|
|
|
|
|
topic = Topic.find_by(id: field.topic_id)
|
|
|
|
if topic
|
|
|
|
puts "Deleting topic #{topic.title} (ID: #{topic.id})"
|
2024-08-18 09:16:03 +00:00
|
|
|
# topic.posts.each do |post|
|
|
|
|
# puts "Deleting post #{post.id} in topic #{topic.id}"
|
|
|
|
# post.destroy
|
|
|
|
# end
|
2024-06-17 09:42:33 +00:00
|
|
|
topic.destroy
|
|
|
|
end
|
|
|
|
end
|
2024-06-17 09:43:33 +00:00
|
|
|
end
|
2024-06-17 09:42:33 +00:00
|
|
|
|
2024-08-19 10:00:59 +00:00
|
|
|
def cleanup_topics_former_user
|
|
|
|
puts "Cleaning up imported topics..."
|
|
|
|
|
|
|
|
# Find all topics that were imported
|
|
|
|
TopicCustomField.where(name: 'original_gossamer_id').each do |field|
|
|
|
|
topic = Topic.find_by(id: field.topic_id)
|
|
|
|
|
|
|
|
next unless topic
|
|
|
|
|
|
|
|
# Fetch the first post in the topic
|
|
|
|
first_post = topic.posts.order(:created_at).first
|
|
|
|
|
|
|
|
# Check if the first post has user_id 2
|
|
|
|
if first_post && first_post.user_id == 2
|
|
|
|
puts "Deleting topic #{topic.title} (ID: #{topic.id})"
|
|
|
|
|
|
|
|
# Destroy all posts in the topic
|
|
|
|
topic.posts.each do |post|
|
|
|
|
puts "Deleting post #{post.id} in topic #{topic.id}"
|
|
|
|
post.destroy
|
|
|
|
end
|
|
|
|
|
|
|
|
# Destroy the topic itself
|
|
|
|
topic.destroy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-08-29 10:09:26 +00:00
|
|
|
def cleanup_topics_with_invalid_posts_count
|
|
|
|
puts "Cleaning up topics with invalid posts_count..."
|
|
|
|
|
|
|
|
# Find all topics that were imported
|
|
|
|
TopicCustomField.where(name: 'original_gossamer_id').each do |field|
|
|
|
|
topic = Topic.find_by(id: field.topic_id)
|
|
|
|
|
|
|
|
next unless topic
|
|
|
|
|
|
|
|
# Check if the posts_count is nil or -1
|
|
|
|
if topic.posts_count.nil? || topic.posts_count == -1
|
|
|
|
# first_post = topic.posts.order(:created_at).first
|
|
|
|
# user_name = first_post&.user&.username || 'Unknown'
|
|
|
|
|
|
|
|
puts "Identified topic for deletion: Title: #{topic.title} Topic ID: #{topic.id}"
|
|
|
|
|
|
|
|
# Destroy all posts in the topic
|
|
|
|
# topic.posts.each do |post|
|
|
|
|
# puts "Deleting post #{post.id} in topic #{topic.id}"
|
|
|
|
# post.destroy
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# # Destroy the topic itself
|
|
|
|
# topic.destroy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-06-17 09:43:33 +00:00
|
|
|
def cleanup_posts
|
|
|
|
puts "Cleaning up imported posts..."
|
2024-06-17 09:42:33 +00:00
|
|
|
# Find all posts that were imported and delete them
|
|
|
|
PostCustomField.where(name: 'original_gossamer_id').each do |field|
|
|
|
|
post = Post.find_by(id: field.post_id)
|
|
|
|
if post
|
|
|
|
puts "Deleting post #{post.id} (ID: #{post.id})"
|
|
|
|
post.destroy
|
|
|
|
end
|
|
|
|
end
|
2024-06-17 09:43:33 +00:00
|
|
|
end
|
2024-06-17 09:42:33 +00:00
|
|
|
|
2024-08-18 09:36:56 +00:00
|
|
|
def cleanup_posts_parallel
|
|
|
|
puts "Cleaning up imported posts..."
|
|
|
|
|
|
|
|
# Define the number of threads to use
|
|
|
|
num_threads = 8
|
|
|
|
pool = Concurrent::FixedThreadPool.new(num_threads)
|
|
|
|
|
|
|
|
PostCustomField.where(name: 'original_gossamer_id').in_batches(of: 1000) do |batch|
|
|
|
|
batch.each do |field|
|
|
|
|
pool.post do
|
|
|
|
post = Post.find_by(id: field.post_id)
|
|
|
|
if post
|
|
|
|
puts "Deleting post #{post.id} (ID: #{post.id})"
|
|
|
|
post.destroy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Wait for all threads to complete
|
|
|
|
pool.shutdown
|
|
|
|
pool.wait_for_termination
|
|
|
|
end
|
|
|
|
|
2024-06-17 09:43:33 +00:00
|
|
|
def cleanup_messages
|
2024-06-17 09:42:33 +00:00
|
|
|
puts "Cleaning up imported personal messages..."
|
|
|
|
# Find all personal messages (inbox) that were imported and delete them
|
|
|
|
TopicCustomField.where(name: 'original_gossamer_msg_id').each do |field|
|
|
|
|
topic = Topic.find_by(id: field.topic_id)
|
|
|
|
if topic
|
|
|
|
puts "Deleting personal message topic #{topic.title} (ID: #{topic.id})"
|
|
|
|
topic.posts.each do |post|
|
|
|
|
puts "Deleting post #{post.id} in personal message topic #{topic.id}"
|
|
|
|
post.destroy
|
|
|
|
end
|
|
|
|
topic.destroy
|
|
|
|
end
|
|
|
|
end
|
2024-06-17 09:43:33 +00:00
|
|
|
end
|
2024-06-17 09:42:33 +00:00
|
|
|
|
2024-06-17 09:43:33 +00:00
|
|
|
def perform_cleanup
|
|
|
|
puts "Cleanup beginning!"
|
2024-08-17 08:56:30 +00:00
|
|
|
# cleanup_messages
|
2024-08-19 10:00:59 +00:00
|
|
|
# cleanup_topics
|
2024-08-29 10:09:26 +00:00
|
|
|
# cleanup_topics_former_user
|
|
|
|
cleanup_topics_with_invalid_posts_count
|
2024-08-19 10:00:59 +00:00
|
|
|
# cleanup_posts_parallel
|
2024-08-17 08:56:30 +00:00
|
|
|
# cleanup_categories
|
|
|
|
# cleanup_users
|
2024-06-17 09:42:33 +00:00
|
|
|
puts "Cleanup complete!"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
GossamerForumsCleaner.new.perform_cleanup
|
2024-06-17 09:43:09 +00:00
|
|
|
|