discourse-import_scripts/goss-destroydeletedposts.rb

113 lines
3.6 KiB
Ruby
Raw Normal View History

2024-09-09 04:19:42 +00:00
# Federated Computer, Inc.
# David Sainty <saint@federated.computer> 2024 A.D.
# Gossamer Threads to Discourse -- CleanUp Script
# v0.5 We need to handle deletion of topic posts -- delete all posts in topic and then delete topic itself
2024-09-09 04:19:42 +00:00
require 'mysql2'
require 'active_record'
# require 'concurrent-ruby'
require File.expand_path("../../../../config/environment", __FILE__)
2024-09-09 04:47:06 +00:00
require File.expand_path("../../../../script/import_scripts/base", __FILE__)
2024-09-09 04:19:42 +00:00
2024-09-09 04:47:06 +00:00
class GossamerForumsDestroyDeletedPosts < ImportScripts::Base
def initialize
super
begin
# Initialize MySQL client to connect to Gossamer Forums database
@mysql_client = Mysql2::Client.new(
host: "slowtwitch.northend.network",
username: "admin",
password: "yxnh93Ybbz2Nm8#mp28zCVv",
database: "slowtwitch"
)
rescue Mysql2::Error => e
puts "Error connecting to MySQL: #{e.message}"
exit 1
end
end
2024-09-09 04:19:42 +00:00
# Define a method to find a post by custom field
def find_post_by_custom_field(post_id)
2024-09-09 04:34:13 +00:00
puts "DestroyDeletedPosts: Searching for post with original_gossamer_id: #{post_id}"
2024-09-09 04:19:42 +00:00
post_custom_field = PostCustomField.find_by(name: 'original_gossamer_id', value: post_id.to_s)
if post_custom_field
post = post_custom_field.post
2024-09-09 04:34:13 +00:00
puts "DestroyDeletedPosts: Found post with id: #{post.id}"
2024-09-09 04:19:42 +00:00
post
else
2024-09-09 04:34:13 +00:00
puts "DestroyDeletedPosts: No post found with original_gossamer_id: #{post_id}"
2024-09-09 04:19:42 +00:00
nil
end
end
# Define a method to delete all posts in a topic
def delete_all_posts_in_topic(topic_id)
posts = Post.where(topic_id: topic_id)
posts.each do |post|
puts "DELETE ALL POSTS --- DiscourseDeletedPosts: Deleting post with id: #{post.id}"
# post.destroy
end
end
# Define a method to delete a topic
def delete_topic(topic_id)
topic = Topic.find_by(id: topic_id)
if topic
puts "DELETE TOPIC --- DiscourseDeletedPosts: Deleting topic with id: #{topic_id}"
# topic.destroy
end
end
2024-09-09 04:19:42 +00:00
# Define the method to delete posts based on the Gossamer Forums flag
def destroy_deleted_posts_from_gossamer_with_user(username)
2024-09-09 04:47:06 +00:00
# Query the user ID from the legacy MySQL database
2024-09-09 04:52:35 +00:00
user_result = @mysql_client.query("SELECT user_id FROM gforum_User WHERE user_username = '#{username}' LIMIT 1")
2024-09-09 04:47:06 +00:00
user_id_row = user_result.first
if user_id_row.nil?
puts "DiscourseDeletedPosts: No user found with username: #{username}"
2024-09-09 04:19:42 +00:00
return
end
2024-09-09 04:47:06 +00:00
user_id = user_id_row['user_id']
# Find all posts marked as deleted by the given user
2024-09-09 04:52:35 +00:00
posts_result = @mysql_client.query("SELECT post_id FROM gforum_Post WHERE post_deleted = 1 AND user_id_fk = #{user_id}")
2024-09-09 04:47:06 +00:00
posts_result.each do |legacy_post|
post_id = legacy_post['post_id']
2024-09-09 04:19:42 +00:00
# Look for the post in Discourse by custom field
2024-09-09 04:56:58 +00:00
post = find_post_by_custom_field(post_id)
2024-09-09 04:19:42 +00:00
if post
# Check if this post is the topic post
if post.post_number == 1
topic_id = post.topic_id
# Delete all posts in the topic
delete_all_posts_in_topic(topic_id)
# Delete the topic itself
delete_topic(topic_id)
else
# If not the topic post, just delete the individual post
puts "DELETE POST --- DestroyDeletedPosts: Deleting post with id: #{post.id}"
# post.destroy
end
2024-09-09 04:19:42 +00:00
end
end
end
def perform_deleted_destroy
puts "Destroy Deleted Posts beginning!"
# destroy_deleted_posts_from_gossamer
destroy_deleted_posts_from_gossamer_with_user('spudone')
puts "Destroy Deleted Posts complete!"
end
end
GossamerForumsDestroyDeletedPosts.new.perform_deleted_destroy