Compare commits

..

97 Commits

Author SHA1 Message Date
266d0068b1 Completed goss-restorethread.rb ready for use on Prod for topics needing (re)import / restoration. 2024-12-10 16:42:47 +11:00
75ec9e2725 Update topid, post, user values post topic restoration run 2024-12-09 21:38:48 +11:00
9c0f2fa5cc Finish taking the multiple issues which Discourse had with the very large number of posts in this particular topic 2024-12-09 20:56:51 +11:00
8787035865 Finish taking the multiple issues which Discourse had with the very large number of posts in this particular topic 2024-12-09 20:20:52 +11:00
553f70cdef Finish taking the multiple issues which Discourse had with the very large number of posts in this particular topic 2024-12-09 18:40:50 +11:00
f18016750c Finish taking the multiple issues which Discourse had with the very large number of posts in this particular topic 2024-12-09 18:29:54 +11:00
8b7ff8983e Finish taking the multiple issues which Discourse had with the very large number of posts in this particular topic 2024-12-09 18:27:22 +11:00
ecce7d4a35 Finish taking the multiple issues which Discourse had with the very large number of posts in this particular topic 2024-12-09 17:59:18 +11:00
89d3a21b78 Finish taking the multiple issues which Discourse had with the very large number of posts in this particular topic 2024-12-07 18:01:20 +11:00
f9b469b9b6 Finish taking the multiple issues which Discourse had with the very large number of posts in this particular topic 2024-12-07 18:00:35 +11:00
09bf758f42 Finish takling the multiple issues which Discourse had with the very large number of posts in this particular topic 2024-12-07 17:49:51 +11:00
a46d02ede8 Finish takling the multiple issues which Discourse had with the very large number of posts in this particular topic 2024-12-07 17:37:43 +11:00
ce865db09e Finish takling the multiple issues which Discourse had with the very large number of posts in this particular topic 2024-12-07 17:12:06 +11:00
1885af1a34 Finish taking the multiple issues which Discourse had with the very large number of posts in this particular topic 2024-12-07 17:10:36 +11:00
a6df981cae Tackle the root issue with Discourse not playing ball with post min entropy 0 / post unclear issue 2024-12-07 17:08:53 +11:00
9f1dcb3ee9 Tackle the root issue with Discourse not playing ball with post min entropy 0 / post unclear issue 2024-12-07 15:36:10 +11:00
33ef6ffbd5 Tackle the root issue with Discourse not playing ball with post min entropy 0 / post unclear issue 2024-12-07 15:16:53 +11:00
eeb5cb4798 Tackle the root issue with Discourse not playing ball with post min entropy 0 / post unclear issue 2024-12-07 15:08:35 +11:00
33a263b275 Tackle the root issue with Discourse not playing ball with post min entropy 0 / post unclear issue 2024-12-07 15:01:57 +11:00
6456556f14 Tackle the root issue with Discourse not playing ball with post min entropy 0 / post unclear issue 2024-12-07 14:51:39 +11:00
1bca1dee81 Tackle the root issue with Discourse not playing ball with post min entropy 0 / post unclear issue 2024-12-07 14:43:11 +11:00
5b5e572bb1 Fix for newer sqlite3 gem version that now enforces consistent paramter format where an array is used 2024-12-07 14:16:08 +11:00
af520c8bdb Improve goss-restorethread.rb to allow for restoring a given Gossamer Forums Forum Post / OP post_id 2024-12-07 13:29:10 +11:00
24b98174f0 Improve goss-restorethread.rb to allow for restoring a given Gossamer Forums Forum Post / OP post_id 2024-12-07 13:22:42 +11:00
204cf1788b Improve goss-restorethread.rb to allow for restoring a given Gossamer Forums Forum Post / OP post_id 2024-12-07 00:15:16 +11:00
79d9d44a3e Improve goss-restorethread.rb to allow for restoring a given Gossamer Forums Forum Post / OP post_id 2024-12-06 23:53:50 +11:00
e972153d01 Improve goss-restorethread.rb to allow for restoring a given Gossamer Forums Forum Post / OP post_id 2024-12-06 23:40:03 +11:00
a2506ae896 Add goss-restorethread.rb to allow for restoring a given Gossamer Forums Forum Post / OP post_id 2024-12-06 22:36:58 +11:00
dc0c347baf tidy up 2024-12-06 21:46:21 +11:00
71efb3af6b goss-resetemail.rb v0.1 -- script to reset email easily 2024-10-10 17:53:45 +11:00
b690b68f85 goss-resetpw.rb v0.2 -- full support 2024-10-10 17:26:59 +11:00
98814a1957 goss-resetpw.rb v0.1 -- for Lance, etc to reset passwords 2024-10-09 03:29:35 +11:00
4ade531ca7 goss-resetpw.rb v0.1 -- for Lance, etc to reset passwords 2024-10-09 03:26:12 +11:00
9a9c510d57 goss-resetpw.rb v0.1 -- for Lance, etc to reset passwords 2024-10-09 03:11:04 +11:00
15c1486e7c v0.3 Ready for actual soft-delete run on live-prod 2024-09-16 14:23:29 +10:00
0363787283 v0.2 Address where due to import issues there are some stray PostCustomField records for Posts that do not exist. 2024-09-11 15:32:02 +10:00
e18a7602a8 v0.2 Address where due to import issues there are some stray PostCustomField records for Posts that do not exist. 2024-09-11 15:04:17 +10:00
a85d33d1e3 v0.1 Add softdeldeletedposts.rb 2024-09-11 14:34:41 +10:00
5750dfded5 v0.1 Add softdeldeletedposts.rb 2024-09-11 14:26:15 +10:00
de0ce06cd7 v0.7 Further attempt to get this reverse dobule encoding right now 2024-09-09 22:39:28 +10:00
736d0afe06 v0.7 Further attempt to get this reverse dobule encoding right now 2024-09-09 22:29:30 +10:00
3cb70bb799 v0.6 Further attempt to get this reverse dobule encoding right now 2024-09-09 22:15:26 +10:00
535adfebba v0.6 Further attempt to get this reverse dobule encoding right now 2024-09-09 22:11:27 +10:00
ad518a1512 v0.6 Further attempt to get this reverse dobule encoding right now 2024-09-09 20:53:10 +10:00
9dd478697a v0.6 Further attempt to get this reverse dobule encoding right now 2024-09-09 20:45:05 +10:00
5f3708b307 v0.6 Further attempt to get this reverse dobule encoding right now 2024-09-09 20:30:15 +10:00
3331675fb2 v0.4 - Add debugging 2024-09-09 19:23:42 +10:00
fae7ef730a v0.4 - Add debugging 2024-09-09 17:43:30 +10:00
655a2619f8 v0.4 - Add debugging 2024-09-09 17:30:37 +10:00
15d6539bc1 v0.3 - Add debugging 2024-09-09 16:44:00 +10:00
1d5bae9d14 v0.3 - Add debugging 2024-09-09 16:37:26 +10:00
fe781117d0 v0.3 - Add debugging 2024-09-09 16:32:51 +10:00
0117991985 v0.3 - Add debugging 2024-09-09 16:30:30 +10:00
538185095d v0.2 - Add debugging 2024-09-09 16:29:14 +10:00
85c93af281 v0.1 - Added new script for encoding correction 2024-09-09 16:15:44 +10:00
6b4495943a v0.1 - Added new script for encoding correction 2024-09-09 16:14:48 +10:00
4f6d4ef5e7 v0.1 - Added new script for encoding correction 2024-09-09 16:13:48 +10:00
78b4613570 v0.5 We need to handle deletion of topic posts -- delete all posts in topic and then delete topic itself 2024-09-09 15:15:58 +10:00
393cea00c2 v0.4 Go back to simpler use of MySQL2 2024-09-09 14:56:58 +10:00
8ab138242a v0.4 Go back to simpler use of MySQL2 2024-09-09 14:52:35 +10:00
d60cdafbfe v0.4 Go back to simpler use of MySQL2 2024-09-09 14:47:06 +10:00
931d8cf8c0 v0.3 Other necessary fixes and tidyups 2024-09-09 14:34:13 +10:00
dcafdb4f44 v0.2 Fix class definition for goss-destroydeletedposts.rb 2024-09-09 14:28:53 +10:00
b61b2edbaa v0.2 Fix class definition for goss-destroydeletedposts.rb 2024-09-09 14:27:13 +10:00
145e956011 v0.1 goss-destroydeletedposts.rb added 2024-09-09 14:19:42 +10:00
309c2206f8 v0.60 Final tweak for topic and user count updating for last public posts 2024-09-02 18:04:16 +10:00
d52db70f96 v0.60 Final tweak for topic and user count updating for last public posts 2024-09-02 18:02:17 +10:00
8814b3f76c v0.60 Final tweak for topic and user count updating for last public posts 2024-09-02 17:47:18 +10:00
9f6f71d197 v0.60 Final tweak for topic and user count updating for last public posts 2024-09-02 17:36:55 +10:00
bd119a3000 v0.60 Final tweak for topic and user count updating for last public posts 2024-09-02 17:03:50 +10:00
a39e435ffe v0.59 Final topic-post import run -- add logic for appending counts since we are adding to a live Discourse host now 2024-09-02 16:27:07 +10:00
7bd8f0068a v0.59 Final topic-post import run -- add logic for appending counts since we are adding to a live Discourse host now 2024-09-02 15:26:40 +10:00
958e37771e v0.59 Final topic-post import run -- add logic for appending counts since we are adding to a live Discourse host now 2024-09-02 13:45:44 +10:00
c8e7ba97bb v0.58 Final user import and personal message import run 2024-09-01 01:24:10 +10:00
3bcc263282 v0.58 Final user import and personal message import run 2024-08-31 23:27:28 +10:00
b424be35c6 v0.58 Final user import and personal message import run 2024-08-31 23:22:10 +10:00
e468a356ff v0.58 Final user import and personal message import run 2024-08-31 23:13:23 +10:00
f27447bbc1 v0.58 Final user import and personal message import run 2024-08-31 22:28:48 +10:00
a61fa8a725 v0.57 Improve personal message handling and add commented line for doing specified blocks of PMs. 2024-08-30 16:10:09 +10:00
09a18ea6f1 v0.57 Improve personal message handling and add commented line for doing specified blocks of PMs. 2024-08-30 15:56:16 +10:00
94cdbfdf28 v0.57 Improve personal message handling and add commented line for doing specified blocks of PMs. 2024-08-30 15:35:32 +10:00
ea15d4b6e2 v0.57 Improve personal message handling and add commented line for doing specified blocks of PMs. 2024-08-30 15:28:53 +10:00
b0b7996af1 v0.56 Prep to run 20240829-2 MDT 2024-08-30 01:30:48 +10:00
6b4b698b99 v0.56 Prep to run 20240829-1 MDT 2024-08-29 23:34:04 +10:00
0ad3fad36c More goss-cleanup improvements 2024-08-29 23:24:36 +10:00
22175ae2c8 More goss-cleanup improvements 2024-08-29 23:11:29 +10:00
8d138d452a Add goss-search.rb for searching things, initially topics 2024-08-29 23:06:06 +10:00
e6f7d313ac Auxiliary script changes for testing in Prod 20240829 2024-08-29 20:52:55 +10:00
174e341973 Auxiliary script changes for testing in Prod 20240829 2024-08-29 20:46:26 +10:00
1856f822ba Auxiliary script changes for testing in Prod 20240829 2024-08-29 20:09:26 +10:00
07206daecb v0.56 Prep to run 20240828-2 MDT 2024-08-29 16:00:02 +10:00
e9612b6f1b v0.56 Prep to run 20240828 MDT 2024-08-29 01:50:55 +10:00
550da20a89 v0.55 Exception handling for topic or user entries in SQLite that are no (longer) in the Discourse DB 2024-08-28 21:02:47 +10:00
0368f27bee v0.55 Exception handling for topic or user entries in SQLite that are no (longer) in the Discourse DB 2024-08-28 20:58:46 +10:00
5b7d53a7d3 v0.54 Tweak trust levels 2024-08-28 14:06:10 +10:00
6b0b8b8c73 v0.54 Tweak trust levels 2024-08-28 14:05:15 +10:00
3fcba4d050 v0.54 Tweak trust levels 2024-08-27 08:21:12 +10:00
13 changed files with 3378 additions and 141 deletions

Binary file not shown.

View File

@ -1,12 +1,12 @@
# Load the Discourse environment # Load the Discourse environment
require File.expand_path("../../../config/environment", __FILE__) require File.expand_path("../../../../config/environment", __FILE__)
# require_relative '/var/www/discourse/config/environment' # require_relative '/var/www/discourse/config/environment'
def query_md5_password_custom_field(username) def query_md5_password_custom_field(username)
user = User.find_by(username: username) user = User.find_by(username: username)
if user if user
md5_password = user.custom_fields['custom_password_md5'] md5_password = user.custom_fields['md5_password']
if md5_password if md5_password
puts "MD5 password custom field for user #{username}: #{md5_password}" puts "MD5 password custom field for user #{username}: #{md5_password}"
else else
@ -26,3 +26,15 @@ end
username = 'davidpaulyoung' username = 'davidpaulyoung'
query_md5_password_custom_field(username) query_md5_password_custom_field(username)
username = 'Slowman'
query_md5_password_custom_field(username)
username = 'GT'
query_md5_password_custom_field(username)
username = 'Chimpking'
query_md5_password_custom_field(username)

View File

@ -1,7 +1,7 @@
# Federated Computer, Inc. # Federated Computer, Inc.
# David Sainty <saint@federated.computer> 2024 A.D. # David Sainty <saint@federated.computer> 2024 A.D.
# Gossamer Threads to Discourse -- CleanUp Script # Gossamer Threads to Discourse -- CleanUp Script
# v0.16 Add parallel deletion of posts. # v0.17 Add more cleanup options.
require 'concurrent-ruby' require 'concurrent-ruby'
require File.expand_path("../../../../config/environment", __FILE__) require File.expand_path("../../../../config/environment", __FILE__)
@ -75,6 +75,24 @@ class GossamerForumsCleaner
end end
end end
def cleanup_topics_with_invalid_posts_count
puts "Cleaning up topics with invalid posts_count..."
# Iterate through all topics
Topic.where("posts_count IS NULL OR posts_count = -1 OR posts_count = 0").find_each do |topic|
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
def cleanup_posts def cleanup_posts
puts "Cleaning up imported posts..." puts "Cleaning up imported posts..."
# Find all posts that were imported and delete them # Find all posts that were imported and delete them
@ -131,7 +149,8 @@ class GossamerForumsCleaner
puts "Cleanup beginning!" puts "Cleanup beginning!"
# cleanup_messages # cleanup_messages
# cleanup_topics # cleanup_topics
cleanup_topics_former_user # cleanup_topics_former_user
cleanup_topics_with_invalid_posts_count
# cleanup_posts_parallel # cleanup_posts_parallel
# cleanup_categories # cleanup_categories
# cleanup_users # cleanup_users

163
goss-correctencoding.rb Normal file
View File

@ -0,0 +1,163 @@
# Federated Computer, Inc.
# David Sainty <saint@federated.computer> 2024 A.D.
# Gossamer Threads to Discourse -- Correct Encoding
# v0.7 Further attempt to get this reverse dobule encoding right now
require 'mysql2'
require 'active_record'
require 'charlock_holmes'
# require 'concurrent-ruby'
require File.expand_path("../../../../config/environment", __FILE__)
require File.expand_path("../../../../script/import_scripts/base", __FILE__)
class GossamerForumsCorrectEncoding < 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}"
puts e.backtrace.join("\n") # Print the full stack trace
exit 1
end
@batch_size = 1000 # Number of posts to process in each batch
end
# Method to detect and fix text encoding
def fix_text_encoding(content)
begin
# # Treat as Windows-1252 (cp1252) and then decode into UTF-8
# corrected_content = content.encode('CP1252').force_encoding('UTF-8')
# Step 1: Treat content as CP1252 and convert it back to UTF-8
corrected_content = content.encode('CP1252').force_encoding('UTF-8').force_encoding('UTF-8')
# # Step 2: Check if there's still a problem (if still corrupted, apply the second pass)
# if corrected_content.valid_encoding?
# return corrected_content
# else
# # Step 3: If it's not valid UTF-8, re-encode and try to fix remaining issues
# corrected_content.encode('UTF-8', invalid: :replace, undef: :replace)
# end
rescue Encoding::UndefinedConversionError => e
puts "Error during encoding conversion: #{e.message}"
puts e.backtrace.join("\n") # Print the full stack trace
return content # Return the original content if conversion fails
end
corrected_content
end
# # Step 1: Try to detect encoding of the corrupted (double-encoded) content
# detection = CharlockHolmes::EncodingDetector.detect(broken_content)
# original_encoding = detection[:encoding]
# puts "Original encoding detected: #{original_encoding}"
#
# # Step 2: First decode the double-encoded content
# begin
# # Convert the content assuming it was double-encoded, so decode twice
# # First, convert from the detected encoding (ISO-8859-1 or windows-1252) to UTF-8
# first_pass = CharlockHolmes::Converter.convert(broken_content, original_encoding, 'UTF-8')
#
# # Step 3: Now re-interpret that output as if it's broken UTF-8 and convert it back to UTF-8
# fixed_content = CharlockHolmes::Converter.convert(first_pass, 'UTF-8', 'UTF-8')
#
# rescue => e
# puts "Error during encoding fix: #{e.message}"
# puts e.backtrace.join("\n") # Print the full stack trace
#
# fixed_content = broken_content # Fall back to the broken content if decoding fails
# end
#
# return fixed_content
#end
# # Detect encoding
# detection = CharlockHolmes::EncodingDetector.detect(raw_content)
# original_encoding = detection[:encoding]
# puts "Original encoding detected: #{original_encoding}"
# # Force the encoding to the detected one, then covnert to UTF-8
# if original_encoding == 'ISO-8859-1' || original_encoding == 'windows-1252'
# # For Windows-1252 or ISO-8859-1, force the encoding and convert to UTF-8
# # text.force_encoding('ISO-8859-1').encode('UTF-8')
# text.force_encoding(original_encoding).encode('UTF-8', invalid: :replace, undef: :replace, replace: '?')
# else
# # Try to convert from detected encoding to UTF-8
# text.encode('UTF-8', original_encoding, invalid: :replace, undef: :replace, replace: '?')
# end
# if original_encoding
# begin
# decoded_content = CharlockHolmes::Converter.convert(raw_content, original_encoding, 'UTF-8')
# rescue => e
# puts "Error during encoding conversion: #{e.message}"
# decoded_content = raw_content # Fall back to raw content if decoding fails
# end
# else
# decoded_content = raw_content # Fallback if encoding detection fails
# end
#
# # Step 3: Ensure the content is now correctly in UTF-8 (no need to encode again)
# return decoded_content
# end
# rescue StandardError => e
# puts "Error during encoding conversion: #{e.message}"
# puts e.backtrace.join("\n") # Print the full stack trace
# text
# end
# end
# Method to fix encoding issues in post content
def fix_encoding
offset = 0
loop do
puts "OFFSET: #{offset}"
begin
posts = Post.limit(@batch_size).offset(offset)
break if posts.empty?
posts.each do |post|
raw_content = post.raw
puts "--> NEXT POST: post.id: #{post.id}"
fixed_content = fix_text_encoding(raw_content)
if fixed_content != raw_content
puts "Updating post #{post.id}"
puts "------- raw_content:\n#{raw_content}"
puts "+++++++ fixed_content:\n#{fixed_content}"
puts "---------------------------------------------------------------------------------------------"
# post.update(raw: fixed_content)
# post.raw = fixed_content
# if post.save
# puts "Post ##{post.id} updated successfully."
# else
# puts "Failed to update Post ##{post.id}: #{post.errors.full_messages.join(', ')}"
# end
end
end
rescue
puts "Error: #{e.message}"
puts e.backtrace.join("\n") # Print the full stack trace
end
offset += @batch_size
end
end
def perform_encoding_correction
puts "Encoding Correction beginning!"
fix_encoding
puts "Encoding Correction complete!"
end
end
GossamerForumsCorrectEncoding.new.perform_encoding_correction

112
goss-destroydeletedposts.rb Normal file
View File

@ -0,0 +1,112 @@
# 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
require 'mysql2'
require 'active_record'
# require 'concurrent-ruby'
require File.expand_path("../../../../config/environment", __FILE__)
require File.expand_path("../../../../script/import_scripts/base", __FILE__)
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
# Define a method to find a post by custom field
def find_post_by_custom_field(post_id)
puts "DestroyDeletedPosts: Searching for post with original_gossamer_id: #{post_id}"
post_custom_field = PostCustomField.find_by(name: 'original_gossamer_id', value: post_id.to_s)
if post_custom_field
post = post_custom_field.post
puts "DestroyDeletedPosts: Found post with id: #{post.id}"
post
else
puts "DestroyDeletedPosts: No post found with original_gossamer_id: #{post_id}"
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
# Define the method to delete posts based on the Gossamer Forums flag
def destroy_deleted_posts_from_gossamer_with_user(username)
# Query the user ID from the legacy MySQL database
user_result = @mysql_client.query("SELECT user_id FROM gforum_User WHERE user_username = '#{username}' LIMIT 1")
user_id_row = user_result.first
if user_id_row.nil?
puts "DiscourseDeletedPosts: No user found with username: #{username}"
return
end
user_id = user_id_row['user_id']
# Find all posts marked as deleted by the given user
posts_result = @mysql_client.query("SELECT post_id FROM gforum_Post WHERE post_deleted = 1 AND user_id_fk = #{user_id}")
posts_result.each do |legacy_post|
post_id = legacy_post['post_id']
# Look for the post in Discourse by custom field
post = find_post_by_custom_field(post_id)
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
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

67
goss-resetemail.rb Normal file
View File

@ -0,0 +1,67 @@
# Federated Computer, Inc.
# David Sainty <saint@federated.computer> 2024 A.D.
# Gossamer Threads to Discourse -- Reset Discourse Email
# v0.1 First addition for resetting email address for user
require File.expand_path("../../../../config/environment", __FILE__)
class GossamerResetEmail
def reset_email(discourse_username, new_email)
puts "Resetting email for Discourse username: '#{discourse_username}'"
user = User.find_by(username: discourse_username)
if user
puts "New email: #{new_email} ..."
# Delete Gossamer MD5 Password
# user.custom_fields.delete('md5_password') # This removes the field completely
# user.save!
# puts " STEP 1 COMPLETED: Gossamer MD5 Password custom field removed for username: #{discourse_username}"
# Set the Discourse password with the `password=` method to properly hash the password
# user.password = new_password
# user.save!
# puts " STEP 2 COMPLETED: Discourse Password updated for username: #{discourse_username}"
user.email = new_email
user.save!
puts " STEP 1 COMPLETED: Email address updated for username: #{discourse_username}"
# Update other attributes (to be sure to remove issues with login)
user.active = true
user.approved = true
user.approved_at = Time.now
user.approved_by_id = 1
user.save!
puts " STEP 2 COMPLETED: Other attributes (active, approved, approved_at, approved_by_id) updated for username: #{discourse_username}"
# Generate a new token, hash it, and create a confirmed email token for the user
token = SecureRandom.hex(20)
token_hash = EmailToken.hash_token(token)
EmailToken.create!(
user_id: user.id,
email: user.email,
token_hash: token_hash,
confirmed: true
)
puts " STEP 3 COMPLETED: New token generated, hashed and set as confirmed email token for username: #{discourse_username}"
else
puts "User not found: #{discourse_username}"
end
end
end
# Main execution
if ARGV.length != 2
puts "Usage: #{$0} discourse_username email_address@domain.com"
exit 1
end
discourse_username = ARGV[0]
new_email = ARGV[1]
# search_topic_by_title(search_string)
GossamerResetEmail.new.reset_email(discourse_username, new_email)

63
goss-resetpw.rb Normal file
View File

@ -0,0 +1,63 @@
# Federated Computer, Inc.
# David Sainty <saint@federated.computer> 2024 A.D.
# Gossamer Threads to Discourse -- Reset Discourse Password and Clear Gossamer Password
# v0.2 Full support
require File.expand_path("../../../../config/environment", __FILE__)
class GossamerResetPassword
def reset_password(discourse_username, new_password)
puts "Resetting password for Discourse username: '#{discourse_username}'"
user = User.find_by(username: discourse_username)
if user
puts "New password: #{new_password} ..."
# Delete Gossamer MD5 Password
user.custom_fields.delete('md5_password') # This removes the field completely
user.save!
puts " STEP 1 COMPLETED: Gossamer MD5 Password custom field removed for username: #{discourse_username}"
# Set the Discourse password with the `password=` method to properly hash the password
user.password = new_password
user.save!
puts " STEP 2 COMPLETED: Discourse Password updated for username: #{discourse_username}"
# Update other attributes (other than password which is already correct)
user.active = true
user.approved = true
user.approved_at = Time.now
user.approved_by_id = 1
user.save!
puts " STEP 3 COMPLETED: Other attributes (active, approved, approved_at, approved_by_id) updated for username: #{discourse_username}"
# Generate a new token, hash it, and create a confirmed email token for the user
token = SecureRandom.hex(20)
token_hash = EmailToken.hash_token(token)
EmailToken.create!(
user_id: user.id,
email: user.email,
token_hash: token_hash,
confirmed: true
)
puts " STEP 4 COMPLETED: New token generated, hashed and set as confirmed email token for username: #{discourse_username}"
else
puts "User not found: #{discourse_username}"
end
end
end
# Main execution
if ARGV.length != 2
puts "Usage: #{$0} discourse_username n3w_p4ssword."
exit 1
end
discourse_username = ARGV[0]
new_password = ARGV[1]
# search_topic_by_title(search_string)
GossamerResetPassword.new.reset_password(discourse_username, new_password)

2305
goss-restorethread.rb Normal file

File diff suppressed because it is too large Load Diff

67
goss-search.rb Normal file
View File

@ -0,0 +1,67 @@
# Federated Computer, Inc.
# David Sainty <saint@federated.computer> 2024 A.D.
# Gossamer Threads to Discourse -- Search By Topic Title
# v0.1 Initial
require 'concurrent-ruby'
require File.expand_path("../../../../config/environment", __FILE__)
class GossamerForumsSearch
def search_topic_by_title(search_string)
puts "Searching for topics with title containing: '#{search_string}'"
# Search for topics with titles containing the search string
matching_topics = Topic.where('title LIKE ?', "%#{search_string}%")
# Print information about each matching topic
matching_topics.each do |topic|
puts "----------------------------------------"
puts "ID: #{topic.id}"
puts "Title: #{topic.title}"
puts "Created At: #{topic.created_at}"
puts "Updated At: #{topic.updated_at}"
puts "Category ID: #{topic.category_id}"
puts "Views: #{topic.views}"
puts "Posts Count: #{topic.posts_count}"
puts "Last Posted At: #{topic.last_posted_at}"
puts "Bumped At: #{topic.bumped_at}"
puts "Last Post User ID: #{topic.last_post_user_id}"
puts "----------------------------------------"
# Fetch and display all posts for the topic
topic.posts.each do |post|
puts " Post ID: #{post.id}"
puts " User ID: #{post.user_id}"
puts " Created At: #{post.created_at}"
puts " Updated At: #{post.updated_at}"
puts " Post Number: #{post.post_number}"
puts " Cooked: #{post.cooked.truncate(80)}" # Limit the display of post content to 100 characters
puts " Raw: #{post.raw.truncate(80)}" # Limit the display of raw post content to 100 characters
puts " ----------------------------------------"
end
end
if matching_topics.empty?
puts "No topics found with title containing: '#{search_string}'"
end
end
def perform_search
puts "Search beginning!"
search_topic_by_title("No father is more proud")
puts "Search complete!"
end
end
# Main execution
if ARGV.length != 1
puts "Usage: #{$0} SEARCH_STRING"
exit 1
end
search_string = ARGV[0]
# search_topic_by_title(search_string)
# GossamerForumsSearch.new.perform_search
GossamerForumsSearch.new.search_topic_by_title(search_string)

View File

@ -1,5 +1,5 @@
# Load the Discourse environment # Load the Discourse environment
require File.expand_path("../../../config/environment", __FILE__) require File.expand_path("../../../../config/environment", __FILE__)
# require_relative '/var/www/discourse/config/environment' # require_relative '/var/www/discourse/config/environment'

View File

@ -1,12 +1,12 @@
# Load the Discourse environment # Load the Discourse environment
require File.expand_path("../../../config/environment", __FILE__) require File.expand_path("../../../../config/environment", __FILE__)
# require_relative '/var/www/discourse/config/environment' # require_relative '/var/www/discourse/config/environment'
def set_md5_password_custom_field(username, md5_password) def set_md5_password_custom_field(username, md5_password)
user = User.find_by(username: username) user = User.find_by(username: username)
if user if user
user.custom_fields['custom_password_md5'] = md5_password user.custom_fields['md5_password'] = md5_password
user.save! user.save!
puts "MD5 password custom field set for user: #{username}" puts "MD5 password custom field set for user: #{username}"
else else

View File

@ -1,7 +1,7 @@
# Federated Computer, Inc. # Federated Computer, Inc.
# David Sainty <saint@federated.computer> 2024 A.D. # David Sainty <saint@federated.computer> 2024 A.D.
# Gossamer Threads to Discourse -- Migration-Import Script # Gossamer Threads to Discourse -- Migration-Import Script
# v0.53 Set to 2 threads for PostgreSQL pool size issue and remonitor. Need to consider memory consumption aspects as well of this after OOM. # v0.60 Final tweak for topic and user count updating for last public posts
require 'mysql2' require 'mysql2'
require 'open-uri' require 'open-uri'
@ -114,47 +114,47 @@ class GossamerForumsImporter < ImportScripts::Base
SQL SQL
# POST IMPORT - For each topic, the time of the last / most recent post/reply # POST IMPORT - For each topic, the time of the last / most recent post/reply
@db.execute <<-SQL @db.execute <<-SQL
CREATE TABLE IF NOT EXISTS topic_last_post_time ( CREATE TABLE IF NOT EXISTS topic_last_post_time_final (
topic_id INTEGER PRIMARY KEY, topic_id INTEGER PRIMARY KEY,
last_post_time INTEGER last_post_time INTEGER
); );
SQL SQL
# POST IMPORT - For each topic, increment post_count as we add posts. # POST IMPORT - For each topic, increment post_count as we add posts.
@db.execute <<-SQL @db.execute <<-SQL
CREATE TABLE IF NOT EXISTS topic_post_count ( CREATE TABLE IF NOT EXISTS topic_post_count_final (
topic_id INTEGER PRIMARY KEY, topic_id INTEGER PRIMARY KEY,
post_count INTEGER DEFAULT 0 post_count INTEGER DEFAULT 0
); );
SQL SQL
# POST IMPORT - For each user (_id), increment topic_count as we add topics (to see total topics per user) # POST IMPORT - For each user (_id), increment topic_count as we add topics (to see total topics per user)
@db.execute <<-SQL @db.execute <<-SQL
CREATE TABLE IF NOT EXISTS user_topic_count ( CREATE TABLE IF NOT EXISTS user_topic_count_final (
user_id INTEGER PRIMARY KEY, user_id INTEGER PRIMARY KEY,
topic_count INTEGER DEFAULT 0 topic_count INTEGER DEFAULT 0
); );
SQL SQL
# POST IMPORT - For each user (_id), increment post_count as we add posts (to see total posts per user) # POST IMPORT - For each user (_id), increment post_count as we add posts (to see total posts per user)
@db.execute <<-SQL @db.execute <<-SQL
CREATE TABLE IF NOT EXISTS user_post_count ( CREATE TABLE IF NOT EXISTS user_post_count_final (
user_id INTEGER PRIMARY KEY, user_id INTEGER PRIMARY KEY,
post_count INTEGER DEFAULT 0 post_count INTEGER DEFAULT 0
); );
SQL SQL
# POST IMPORT - For each topic, the user_id for the last poster / replier # POST IMPORT - For each topic, the user_id for the last poster / replier
@db.execute <<-SQL @db.execute <<-SQL
CREATE TABLE IF NOT EXISTS topic_last_post_user ( CREATE TABLE IF NOT EXISTS topic_last_post_user_final (
topic_id INTEGER PRIMARY KEY, topic_id INTEGER PRIMARY KEY,
user_id INTEGER user_id INTEGER
); );
SQL SQL
# POST IMPORT - The number of posts in a given topic, incremented as we add a new reply post to a topic. # POST IMPORT - The number of posts in a given topic, incremented as we add a new reply post to a topic.
@db.execute <<-SQL @db.execute <<-SQL
CREATE TABLE IF NOT EXISTS topic_post_numbers ( CREATE TABLE IF NOT EXISTS topic_post_numbers_final (
topic_id INTEGER PRIMARY KEY, topic_id INTEGER PRIMARY KEY,
post_number INTEGER DEFAULT 0 post_number INTEGER DEFAULT 0
); );
SQL SQL
# POST IMPORT - Record perssitent integer value for highest processed post id -- not used # POST IMPORT - Record perssitent integer value for highest processed post id
@db.execute <<-SQL @db.execute <<-SQL
CREATE TABLE IF NOT EXISTS highest_processed_post_id ( CREATE TABLE IF NOT EXISTS highest_processed_post_id (
id INTEGER PRIMARY KEY CHECK (id = 1), id INTEGER PRIMARY KEY CHECK (id = 1),
@ -229,7 +229,7 @@ class GossamerForumsImporter < ImportScripts::Base
end end
# Method to create Nginx rewrite rules file # Method to create Nginx rewrite rules file
def create_nginx_rewrite_rules(filename) def export_nginx_rewrite_rules(filename)
File.open(filename, "w") do |file| File.open(filename, "w") do |file|
@db.execute("SELECT old_post_id, new_url FROM url_map") do |row| @db.execute("SELECT old_post_id, new_url FROM url_map") do |row|
old_post_id, new_url = row old_post_id, new_url = row
@ -253,51 +253,51 @@ class GossamerForumsImporter < ImportScripts::Base
end end
def fetch_db_topic_last_post_time(topic_id) def fetch_db_topic_last_post_time(topic_id)
@db.get_first_value "SELECT last_post_time FROM topic_last_post_time WHERE topic_id = ?", topic_id @db.get_first_value "SELECT last_post_time FROM topic_last_post_time_final WHERE topic_id = ?", topic_id
end end
def fetch_db_topic_last_post_user(topic_id) def fetch_db_topic_last_post_user(topic_id)
@db.get_first_value "SELECT user_id FROM topic_last_post_user WHERE topic_id = ?", topic_id @db.get_first_value "SELECT user_id FROM topic_last_post_user_final WHERE topic_id = ?", topic_id
end end
def fetch_db_topic_post_count(topic_id) def fetch_db_topic_post_count(topic_id)
@db.get_first_value "SELECT post_count FROM topic_post_count WHERE topic_id = ?", topic_id @db.get_first_value "SELECT post_count FROM topic_post_count_final WHERE topic_id = ?", topic_id
end end
def fetch_db_user_topic_count(user_id) def fetch_db_user_topic_count(user_id)
@db.get_first_value "SELECT topic_count FROM user_topic_count WHERE user_id = ?", user_id @db.get_first_value "SELECT topic_count FROM user_topic_count_final WHERE user_id = ?", user_id
end end
def fetch_db_user_post_count(user_id) def fetch_db_user_post_count(user_id)
@db.get_first_value "SELECT post_count FROM user_post_count WHERE user_id = ?", user_id @db.get_first_value "SELECT post_count FROM user_post_count_final WHERE user_id = ?", user_id
end end
def fetch_db_topic_post_numbers(topic_id) def fetch_db_topic_post_numbers(topic_id)
@db.get_first_value "SELECT post_number FROM topic_post_numbers WHERE topic_id = ?", topic_id @db.get_first_value "SELECT post_number FROM topic_post_numbers_final WHERE topic_id = ?", topic_id
end end
def update_db_topic_last_post_time(topic_id, last_post_time) def update_db_topic_last_post_time(topic_id, last_post_time)
@db.execute "INSERT OR REPLACE INTO topic_last_post_time (topic_id, last_post_time) VALUES (?, ?)", topic_id, last_post_time @db.execute "INSERT OR REPLACE INTO topic_last_post_time_final (topic_id, last_post_time) VALUES (?, ?)", topic_id, last_post_time
end end
def update_db_topic_last_post_user(topic_id, user_id) def update_db_topic_last_post_user(topic_id, user_id)
@db.execute "INSERT OR REPLACE INTO topic_last_post_user (topic_id, user_id) VALUES (?, ?)", topic_id, user_id @db.execute "INSERT OR REPLACE INTO topic_last_post_user_final (topic_id, user_id) VALUES (?, ?)", topic_id, user_id
end end
def update_db_topic_post_count(topic_id, post_count) def update_db_topic_post_count(topic_id, post_count)
@db.execute "INSERT OR REPLACE INTO topic_post_count (topic_id, post_count) VALUES (?, ?)", topic_id, post_count @db.execute "INSERT OR REPLACE INTO topic_post_count_final (topic_id, post_count) VALUES (?, ?)", topic_id, post_count
end end
def update_db_user_topic_count(user_id, topic_count) def update_db_user_topic_count(user_id, topic_count)
@db.execute "INSERT OR REPLACE INTO user_topic_count (user_id, topic_count) VALUES (?, ?)", user_id, topic_count @db.execute "INSERT OR REPLACE INTO user_topic_count_final (user_id, topic_count) VALUES (?, ?)", user_id, topic_count
end end
def update_db_user_post_count(user_id, post_count) def update_db_user_post_count(user_id, post_count)
@db.execute "INSERT OR REPLACE INTO user_post_count (user_id, post_count) VALUES (?, ?)", user_id, post_count @db.execute "INSERT OR REPLACE INTO user_post_count_final (user_id, post_count) VALUES (?, ?)", user_id, post_count
end end
def update_db_topic_post_numbers(topic_id, post_number) def update_db_topic_post_numbers(topic_id, post_number)
@db.execute "INSERT OR REPLACE INTO topic_post_numbers (topic_id, post_number) VALUES (?, ?)", topic_id, post_number @db.execute "INSERT OR REPLACE INTO topic_post_numbers_final (topic_id, post_number) VALUES (?, ?)", topic_id, post_number
end end
# Fetch the highest processed post_id from the highest_processed_post_id table # Fetch the highest processed post_id from the highest_processed_post_id table
@ -624,11 +624,21 @@ class GossamerForumsImporter < ImportScripts::Base
users = [] users = []
# Fetch all users from Gossamer Forums # Fetch all users from Gossamer Forums
execute_query("SELECT * FROM gforum_User").each do |row| # execute_query("SELECT * FROM gforum_User").each do |row|
execute_query("SELECT user_id, user_username, user_email, user_real_name, user_registered, user_last_seen, user_title, user_about, user_homepage, user_location, user_password FROM gforum_User WHERE user_id > 123381").each do |row|
username = sanitize_username(row['user_username'], row['user_email'], row['user_real_name'])
email = row['user_email']
# Check if the user already exists in Discourse by username or email
# existing_user = User.find_by(username: username) || User.find_by(email: email)
existing_user = User.find_by(username: username) || User.joins(:user_emails).find_by(user_emails: { email: email })
# Only add the user if they do not already exist
unless existing_user
users << { users << {
id: row['user_id'], id: row['user_id'],
username: sanitize_username(row['user_username'], row['user_email'], row['user_real_name']), username: username,
email: row['user_email'], email: email,
created_at: Time.at(row['user_registered']), created_at: Time.at(row['user_registered']),
updated_at: Time.at(row['user_last_seen']), updated_at: Time.at(row['user_last_seen']),
name: row['user_real_name'], name: row['user_real_name'],
@ -643,8 +653,9 @@ class GossamerForumsImporter < ImportScripts::Base
} }
} }
end end
end
# Create or update users in Discourse # Create new users in Discourse
create_users(users) do |user| create_users(users) do |user|
# insert_user_id_mapping(user[:id], user.id) # insert_user_id_mapping(user[:id], user.id)
user user
@ -657,7 +668,8 @@ class GossamerForumsImporter < ImportScripts::Base
users = [] users = []
# Fetch all users from Gossamer Forums # Fetch all users from Gossamer Forums
execute_query("SELECT * FROM gforum_User").each do |row| # execute_query("SELECT * FROM gforum_User").each do |row|
execute_query("SELECT user_id, user_username, user_email, user_real_name, user_registered, user_last_seen, user_title, user_about, user_homepage, user_location, user_password FROM gforum_User WHERE user_id > 123381").each do |row|
users << { users << {
id: row['user_id'], id: row['user_id'],
username: sanitize_username(row['user_username'], row['user_email'], row['user_real_name']), username: sanitize_username(row['user_username'], row['user_email'], row['user_real_name']),
@ -701,7 +713,8 @@ class GossamerForumsImporter < ImportScripts::Base
users = [] users = []
# Fetch all users from Gossamer Forums # Fetch all users from Gossamer Forums
execute_query("SELECT * FROM gforum_User").each do |row| # execute_query("SELECT * FROM gforum_User").each do |row|
execute_query("SELECT user_id, user_username, user_email, user_real_name, user_registered, user_last_seen, user_title, user_about, user_homepage, user_location, user_password FROM gforum_User WHERE user_id > 123381").each do |row|
users << { users << {
id: row['user_id'], id: row['user_id'],
username: sanitize_username(row['user_username'], row['user_email'], row['user_real_name']), username: sanitize_username(row['user_username'], row['user_email'], row['user_real_name']),
@ -737,16 +750,16 @@ class GossamerForumsImporter < ImportScripts::Base
discourse_user.user_profile.bio_raw ||= "" discourse_user.user_profile.bio_raw ||= ""
# Append bio if it exists, otherwise set it to empty string to avoid nil errors # Append bio if it exists, otherwise set it to empty string to avoid nil errors
if discourse_user.user_profile.bio_raw.empty? ## if discourse_user.user_profile.bio_raw.empty?
discourse_user.user_profile.bio_raw = user[:bio_raw] discourse_user.user_profile.bio_raw = user[:bio_raw]
else ## else
discourse_user.user_profile.bio_raw += "\n\n" + user[:bio_raw] ## discourse_user.user_profile.bio_raw += "\n\n" + user[:bio_raw]
end ## end
# Ensure the bio does not exceed 3000 characters # Ensure the bio does not exceed 3000 characters
if discourse_user.user_profile.bio_raw.length > 3000 if discourse_user.user_profile.bio_raw.length > 2999
puts "Warning: About Me for user #{discourse_user.username} (ID: #{discourse_user.id}) exceeds 3000 characters. Truncating." puts "Warning: About Me for user #{discourse_user.username} (ID: #{discourse_user.id}) exceeds 3000 characters. Truncating."
discourse_user.user_profile.bio_raw = discourse_user.user_profile.bio_raw[0, 3000] discourse_user.user_profile.bio_raw = discourse_user.user_profile.bio_raw[0, 2999]
end end
discourse_user.user_profile.save! discourse_user.user_profile.save!
@ -1529,7 +1542,7 @@ class GossamerForumsImporter < ImportScripts::Base
# Import topics and posts from Gossamer Forums to Discourse # Import topics and posts from Gossamer Forums to Discourse
def import_topics_and_posts_with_attachments def import_topics_and_posts
puts "Importing topics and posts with attachments..." puts "Importing topics and posts with attachments..."
# topic_last_post_time = {} # topic_last_post_time = {}
@ -1540,16 +1553,18 @@ class GossamerForumsImporter < ImportScripts::Base
# topic_post_numbers = Hash.new { |hash, key| hash[key] = 0 } # topic_post_numbers = Hash.new { |hash, key| hash[key] = 0 }
# Fetch the highest old_post_id from the url_map table # Fetch the highest old_post_id from the url_map table
highest_old_post_id = fetch_highest_old_post_id.to_i # highest_old_post_id = fetch_highest_old_post_id.to_i
puts "Highest (OP) old_post_id in url_map: #{highest_old_post_id}" # puts "Highest (OP) old_post_id in url_map: #{highest_old_post_id}"
highest_processed_post_id = fetch_highest_processed_post_id.to_i highest_processed_post_id = fetch_highest_processed_post_id.to_i
puts "Highest processed post_id: #{highest_processed_post_id}" puts "Highest processed post_id: #{highest_processed_post_id}"
# OVERRIDE........ # OVERRIDE........
# Attachment example: highest_processed_post_id = 1359862 # Attachment example: highest_processed_post_id = 1359862
highest_processed_post_id = 8179621
puts "OVERRIDE Highest processed post_id: #{highest_processed_post_id}"
# Execute the query to get all posts ordered by post_id # Execute the query to get all posts ordered by post_id
execute_query("SELECT post_id, user_id_fk, forum_id_fk, post_root_id, post_subject, post_time, post_message, post_father_id, post_likes, post_replies FROM gforum_Post ORDER BY post_id").each do |row| execute_query("SELECT post_id, user_id_fk, forum_id_fk, post_root_id, post_subject, post_time, post_message, post_father_id, post_replies FROM gforum_Post WHERE post_id > #{highest_processed_post_id} ORDER BY post_id").each do |row|
post_id = row['post_id'].to_i post_id = row['post_id'].to_i
# Skip posts that have already been processed # Skip posts that have already been processed
@ -1557,11 +1572,21 @@ class GossamerForumsImporter < ImportScripts::Base
# next if post_id < highest_old_post_id # next if post_id < highest_old_post_id
next if post_id <= highest_processed_post_id next if post_id <= highest_processed_post_id
puts "Processing post_id #{row['post_id']} post_id #{row['post_root_id']} post_subject/title #{row['post_subject']} forum_id_fk/category_id #{row['forum_id_fk']}" puts "Processing post_id #{row['post_id']} post_root_id #{row['post_root_id']} post_subject/title #{row['post_subject']} forum_id_fk (category_id) #{row['forum_id_fk']}"
# discourse_user_id = @user_id_map[row['user_id_fk']] # discourse_user_id = @user_id_map[row['user_id_fk']]
# Fetch the Discourse user and category IP mappings # Fetch the Discourse user_id based on Gossamer data and mapping
discourse_user_id = fetch_user_id_mapping(row['user_id_fk']) discourse_user_id = fetch_user_id_mapping(row['user_id_fk'])
# Check to be certain user has not been deleted, etc.
if discourse_user_id.nil? || discourse_user_id == 0
puts "discourse_user_id is NIL/ZERO for post_id #{row['post_id']}"
discourse_former_user = User.find_by(username: 'Former_User')
discourse_user_id = discourse_former_user.id
puts "discourse_user_id is NOW Former_User id #{discourse_user_id} for post_id #{row['post_id']}"
end
# Fetch the Discourse category_id based on Gossamer data and mapping
discourse_category_id = fetch_category_id_mapping(row['forum_id_fk']) discourse_category_id = fetch_category_id_mapping(row['forum_id_fk'])
puts "discourse_user_id #{discourse_user_id} discourse_category_id #{discourse_category_id}" puts "discourse_user_id #{discourse_user_id} discourse_category_id #{discourse_category_id}"
next unless discourse_user_id && discourse_category_id next unless discourse_user_id && discourse_category_id
@ -1611,11 +1636,14 @@ class GossamerForumsImporter < ImportScripts::Base
end end
end end
end end
current_topic_id = topic.id
# Track last post time and user for the topic # Track last post time and user for the topic
# topic_last_post_time[topic.id] = Time.at(row['post_time']) # topic_last_post_time[topic.id] = Time.at(row['post_time'])
# topic_last_post_user[topic.id] = discourse_user_id # topic_last_post_user[topic.id] = discourse_user_id
update_db_topic_last_post_time(topic.id, Time.at(row['post_time']).to_i) update_db_topic_last_post_time(current_topic_id, Time.at(row['post_time']).to_i)
update_db_topic_last_post_user(topic.id, discourse_user_id) update_db_topic_last_post_user(current_topic_id, discourse_user_id)
# Increment the count of the number of topics created by each user # Increment the count of the number of topics created by each user
# user_topic_count[discourse_user_id] += 1 # user_topic_count[discourse_user_id] += 1
@ -1634,26 +1662,26 @@ class GossamerForumsImporter < ImportScripts::Base
# Sanitize the post message # Sanitize the post message
sanitized_post_message = sanitize_post_message(row['post_message']) sanitized_post_message = sanitize_post_message(row['post_message'])
puts "CREATE POST topic.id #{topic.id} discourse_user_id #{discourse_user_id}" puts "CREATE TOPIC POST current_topic_id #{current_topic_id} discourse_user_id #{discourse_user_id}"
# Increment the number of posts in the given topic. # Increment the number of posts in the given topic.
# topic_post_numbers[topic.id] += 1 # topic_post_numbers[topic.id] += 1
post_number = fetch_db_topic_post_numbers(topic.id).to_i + 1 post_number = fetch_db_topic_post_numbers(current_topic_id).to_i + 1
update_db_topic_post_numbers(topic.id, post_number) update_db_topic_post_numbers(current_topic_id, post_number)
# Create the initial post in the topic # Create the initial post in the topic
post = Post.create!( post = Post.create!(
topic_id: topic.id, topic_id: current_topic_id,
user_id: discourse_user_id, user_id: discourse_user_id,
# raw: import_attachments(row['post_message'], row['post_id']),
# raw: row['post_message'] || "",
raw: sanitized_post_message, raw: sanitized_post_message,
created_at: Time.at(row['post_time']), created_at: Time.at(row['post_time']),
updated_at: Time.at(row['post_time']), updated_at: Time.at(row['post_time']),
like_count: row['post_likes'] || 0,
reads: post_views || 0, reads: post_views || 0,
post_number: post_number post_number: post_number
) )
# like_count: row['post_likes'] || 0,
# raw: import_attachments(row['post_message'], row['post_id']),
# raw: row['post_message'] || "",
# post_number: topic_post_numbers[topic.id] # post_number: topic_post_numbers[topic.id]
post.custom_fields['original_gossamer_id'] = row['post_id'] post.custom_fields['original_gossamer_id'] = row['post_id']
post.save! post.save!
@ -1661,11 +1689,11 @@ class GossamerForumsImporter < ImportScripts::Base
# Track the number of posts in the topic and by the user # Track the number of posts in the topic and by the user
# topic_post_count[topic.id] += 1 # topic_post_count[topic.id] += 1
# user_post_count[discourse_user_id] += 1 # user_post_count[discourse_user_id] += 1
update_db_topic_post_count(topic.id, fetch_db_topic_post_count(topic.id).to_i + 1) update_db_topic_post_count(current_topic_id, fetch_db_topic_post_count(current_topic_id).to_i + 1)
update_db_user_post_count(discourse_user_id, fetch_db_user_post_count(discourse_user_id).to_i + 1) update_db_user_post_count(discourse_user_id, fetch_db_user_post_count(discourse_user_id).to_i + 1)
# Handle attachments for the post # Handle attachments for the post
handle_post_attachments(row['post_id'], post, discourse_user_id) handle_post_attachments(row['post_id'], post, discourse_user_id, @mysql_client)
# Create URL mappings # Create URL mappings
# old_url = "https://old/forum/#{row['forum_name']}/topics/#{row['post_id']}" # old_url = "https://old/forum/#{row['forum_name']}/topics/#{row['post_id']}"
@ -1705,25 +1733,43 @@ class GossamerForumsImporter < ImportScripts::Base
# topic_post_numbers[topic_id] += 1 # topic_post_numbers[topic_id] += 1
post_number = fetch_db_topic_post_numbers(topic_id).to_i + 1 post_number = fetch_db_topic_post_numbers(topic_id).to_i + 1
update_db_topic_post_numbers(topic_id, post_number)
puts "CREATE REPLY in topic_id #{topic_id} for reply post_id #{row['post_id']} with post_number #{post_number}"
# Create the post in the existing topic # Create the post in the existing topic
post_created = false
while ! post_created
begin
post = Post.create!( post = Post.create!(
topic_id: topic_id, topic_id: topic_id,
user_id: discourse_user_id, user_id: discourse_user_id,
# raw: import_attachments(row['post_message'], row['post_id']),
# raw: row['post_message'] || "",
raw: sanitized_post_message, raw: sanitized_post_message,
created_at: Time.at(row['post_time']), created_at: Time.at(row['post_time']),
updated_at: Time.at(row['post_time']), updated_at: Time.at(row['post_time']),
reply_to_post_number: reply_to_post_number, reads: post_views || 0,
like_count: row['post_replies'] || 0,
reads: post_views || fetch_db_topic_post_count(topic_id).to_i,
post_number: post_number post_number: post_number
) )
# raw: import_attachments(row['post_message'], row['post_id']),
# raw: row['post_message'] || "",
# reply_to_post_number: reply_to_post_number,
# like_count: row['post_replies'] || 0,
# reads: post_views || fetch_db_topic_post_count(topic_id).to_i,
post.custom_fields['original_gossamer_id'] = row['post_id'] post.custom_fields['original_gossamer_id'] = row['post_id']
post.save! post.save!
post_created = true
rescue ActiveRecord::RecordNotUnique => e
if e.message.include?("duplicate key value violates unique constraint")
post_number = post_number + 1
else
raise e
end
end
end
update_db_topic_post_numbers(topic_id, post_number)
# Track the number of posts in the topic and by the user # Track the number of posts in the topic and by the user
# topic_post_count[topic_id] += 1 # topic_post_count[topic_id] += 1
# user_post_count[discourse_user_id] += 1 # user_post_count[discourse_user_id] += 1
@ -1741,7 +1787,7 @@ class GossamerForumsImporter < ImportScripts::Base
end end
# Handle attachments for the post # Handle attachments for the post
handle_post_attachments(row['post_id'], post, discourse_user_id) handle_post_attachments(row['post_id'], post, discourse_user_id, @mysql_client)
# Update the highest processed post_id # Update the highest processed post_id
puts "Updated highest processed post_id #{post_id}" puts "Updated highest processed post_id #{post_id}"
@ -1772,7 +1818,9 @@ class GossamerForumsImporter < ImportScripts::Base
# end # end
@db.execute("SELECT * FROM topic_last_post_time").each do |row| @db.execute("SELECT * FROM topic_last_post_time").each do |row|
topic_id, last_post_time = row topic_id, last_post_time = row
# Validation error: Topic.find(topic_id).update!(
begin
# Validation error: Topic.find(topic_id).update!(
topic = Topic.find(topic_id) topic = Topic.find(topic_id)
# Ensure we are only updating necessary fields # Ensure we are only updating necessary fields
@ -1783,6 +1831,42 @@ class GossamerForumsImporter < ImportScripts::Base
bumped_at: Time.at(last_post_time), bumped_at: Time.at(last_post_time),
last_post_user_id: fetch_db_topic_last_post_user(topic_id).to_i last_post_user_id: fetch_db_topic_last_post_user(topic_id).to_i
) )
rescue ActiveRecord::RecordNotFound
puts "WARNING: Could not find Topic with id=#{topic_id}. Skipping..."
end
end
end
def update_topic_stats_final
# Update topics with the correct last post time, post count, and last post user
puts "Update topics with the correct last post time, post count, and last post user"
@db.execute("SELECT * FROM topic_last_post_time_final").each do |row|
topic_id, last_post_time = row
puts "update_topic_stats_final topic_id #{topic_id}"
begin
topic = Topic.find(topic_id)
# Calculate the new values based on the given conditions
new_updated_at = Time.at(last_post_time)
new_posts_count = topic.posts_count + fetch_db_topic_post_count(topic_id).to_i
new_last_posted_at = Time.at(last_post_time)
new_bumped_at = Time.at(last_post_time)
new_last_post_user_id = fetch_db_topic_last_post_user(topic_id).to_i
# Update the topic with the calculated values
topic.update_columns(
updated_at: new_updated_at,
posts_count: new_posts_count,
last_posted_at: new_last_posted_at,
bumped_at: new_bumped_at,
last_post_user_id: new_last_post_user_id
)
rescue ActiveRecord::RecordNotFound
puts "WARNING: Could not find Topic with id=#{topic_id}. Skipping..."
end
end end
end end
@ -1797,44 +1881,153 @@ class GossamerForumsImporter < ImportScripts::Base
# user = User.find(user_id) # user = User.find(user_id)
# user.update!(post_count: count) # user.update!(post_count: count)
# end # end
@db.execute("SELECT * FROM user_topic_count").each do |row| @db.execute("SELECT * FROM user_topic_count_final").each do |row|
user_id, count = row user_id, count = row
# user = User.find(user_id) # user = User.find(user_id)
# user.update!(topic_count: count) # user.update!(topic_count: count)
puts "update_user_stats user_id #{user_id} topic_count #{count}" puts "update_user_stats user_id #{user_id} topic_count #{count}"
begin
user_stat = UserStat.find_or_initialize_by(user_id: user_id) user_stat = UserStat.find_or_initialize_by(user_id: user_id)
user_stat.update_columns(topic_count: count) user_stat.update_columns(topic_count: count)
rescue
puts "WARNING: Could not find User with id=#{user_id}. Skipping..."
end
end end
@db.execute("SELECT * FROM user_post_count").each do |row| @db.execute("SELECT * FROM user_post_count_final").each do |row|
user_id, count = row user_id, count = row
# user = User.find(user_id) # user = User.find(user_id)
# user.update!(post_count: count) # user.update!(post_count: count)
puts "update_user_stats user_id #{user_id} post_count #{count}" puts "update_user_stats user_id #{user_id} post_count #{count}"
begin
user_stat = UserStat.find_or_initialize_by(user_id: user_id) user_stat = UserStat.find_or_initialize_by(user_id: user_id)
user_stat.update_columns(post_count: count) user_stat.update_columns(post_count: count)
# Determine the new Trust Level based on post_count
user = User.find(user_id)
new_trust_level = case count
when 0..2 then 1 # basic user
when 3..50 then 2 # member
else 3 # regular or above when 51..100
end
# Fetch the current user and check if Trust Level needs updating # Fetch the current user and check if Trust Level needs updating
new_trust_level = case count
when 0..29 then 1 # basic user
else 2 # member, regular reserved for now.
# when 3..50 then 2 # member
# else 3 # regular or above when 51..100
end
user = User.find(user_id) user = User.find(user_id)
current_trust_level = user.trust_level || 1 # default to 1 if not set current_trust_level = user.trust_level || 1 # default to 1 if not set
# Only update trust level if the new level is higher than the current one # Only update trust level if the new level is higher than the current one
if new_trust_level > current_trust_level if new_trust_level != current_trust_level
user.update!(trust_level: new_trust_level) user.update!(trust_level: new_trust_level)
puts "update_user_stats user_id #{user_id} trust_level updated to #{new_trust_level}" puts "update_user_stats user_id #{user_id} trust_level was #{current_trust_level}, now updated to #{new_trust_level}"
else else
puts "update_user_stats user_id #{user_id} trust_level remains at #{current_trust_level}" puts "update_user_stats user_id #{user_id} trust_level remains at #{current_trust_level}"
end end
rescue
puts "WARNING: Could not find or modify User with id=#{user_id}. Skipping..."
end end
end end
end
def update_user_stats_finalfinal
@db.execute("SELECT * FROM user_post_count_final").each do |row|
user_id, count = row
puts "update_user_stats user_id #{user_id}"
begin
user_stat = UserStat.find_or_initialize_by(user_id: user_id)
# Fetch the current post count for the user
post_count = user_stat.post_count.to_i
## user_stat.update_columns(post_count: new_post_count)
# Fetch the current user and check if Trust Level needs updating
# new_trust_level = case post_count
# when 0..29 then 1 # basic user
# else 2 # member, regular reserved for now.
# when 3..50 then 2 # member
# else 3 # regular or above when 51..100
# end
user = User.find(user_id)
current_trust_level = user.trust_level
if user.id == 101978
puts "USER ID 101978, so TRUST LEVEL 4"
new_trust_level = 4
elsif user.id == 2
puts "USER ID 2, so TRUST LEVEL 2"
new_trust_level = 2
elsif [111533, 161794, 163535, 172916, 177877, 183157, 189831, 191083, 192666, 195599, 198625, 200072, 200823, 201878, 201968].include?(user.id)
new_trust_level = 1
else
new_trust_level = 2
end
# Only update trust level if the new level is higher than the current one
if new_trust_level != current_trust_level
user.update!(trust_level: new_trust_level)
puts "update_user_stats user_id #{user_id} trust_level was #{current_trust_level}, now updated to #{new_trust_level}"
else
puts "update_user_stats user_id #{user_id} trust_level remains at #{current_trust_level}"
end
rescue
puts "WARNING: Could not find or modify User with id=#{user_id}: #{e.message} ... Skipping..."
puts e.backtrace.join("\n") # Print the full stack trace
end
end
end
def update_user_stats_final
# Update user profiles with the number of topics and posts created
puts "Update user profiles with the number of topics and posts created"
@db.execute("SELECT * FROM user_topic_count_final").each do |row|
user_id, count = row
puts "update_user_stats user_id #{user_id} topic_count #{count}"
begin
user_stat = UserStat.find_or_initialize_by(user_id: user_id)
# user_stat.update_columns(topic_count: count)
# Accumulate the current topic count with the new count
new_topic_count = user_stat.topic_count.to_i + count.to_i
user_stat.update_columns(topic_count: new_topic_count)
rescue
puts "WARNING: Could not find User with id=#{user_id}. Skipping..."
end
end
@db.execute("SELECT * FROM user_post_count_final").each do |row|
user_id, count = row
puts "update_user_stats user_id #{user_id} post_count #{count}"
begin
user_stat = UserStat.find_or_initialize_by(user_id: user_id)
# user_stat.update_columns(post_count: count)
# Accumulate the current post count with the new count
new_post_count = user_stat.post_count.to_i + count.to_i
user_stat.update_columns(post_count: new_post_count)
# Fetch the current user and check if Trust Level needs updating
new_trust_level = case count
when 0..29 then 1 # basic user
else 2 # member, regular reserved for now.
# when 3..50 then 2 # member
# else 3 # regular or above when 51..100
end
user = User.find(user_id)
current_trust_level = user.trust_level || 1 # default to 1 if not set
# Only update trust level if the new level is higher than the current one
if new_trust_level != current_trust_level
user.update!(trust_level: new_trust_level)
puts "update_user_stats user_id #{user_id} trust_level was #{current_trust_level}, now updated to #{new_trust_level}"
else
puts "update_user_stats user_id #{user_id} trust_level remains at #{current_trust_level}"
end
rescue
puts "WARNING: Could not find or modify User with id=#{user_id}. Skipping..."
end
end
end
# Import personal messages from gforum_Message table (both inbox and sent messages) # Import personal messages from gforum_Message table (both inbox and sent messages)
@ -1845,17 +2038,20 @@ class GossamerForumsImporter < ImportScripts::Base
highest_processed_personal_id = fetch_highest_processed_personal_id.to_i highest_processed_personal_id = fetch_highest_processed_personal_id.to_i
puts "Highest processed personal_id: #{highest_processed_personal_id}" puts "Highest processed personal_id: #{highest_processed_personal_id}"
# OVERRIDE - to speed getting to problem msg # OVERRIDE - to speed getting to lower limit msg
# # # highest_processed_personal_id = 1543840 # # # highest_processed_personal_id = 1543840
puts "Highest processed personal_id override: #{highest_processed_personal_id}" puts "Highest processed personal_id override: #{highest_processed_personal_id}"
execute_query("SELECT * FROM gforum_Message").each do |row| ## execute_query("SELECT * FROM gforum_Message").each do |row|
execute_query("SELECT * FROM gforum_Message WHERE msg_id > #{highest_processed_personal_id}").each do |row|
begin begin
msg_id = row['msg_id'].to_i msg_id = row['msg_id'].to_i
puts "msg_id #{msg_id}" puts "msg_id #{msg_id}"
# Skip posts that have already been processed # Skip posts that have already been processed
next if msg_id <= highest_processed_personal_id # OVERRIDE - upper limit msg
# # # next if msg_id <= highest_processed_personal_id || msg_id > 342443
## next if msg_id <= highest_processed_personal_id
from_user_id = fetch_user_id_mapping(row['from_user_id_fk']) from_user_id = fetch_user_id_mapping(row['from_user_id_fk'])
to_user_id = fetch_user_id_mapping(row['to_user_id_fk']) to_user_id = fetch_user_id_mapping(row['to_user_id_fk'])
@ -1937,8 +2133,9 @@ class GossamerForumsImporter < ImportScripts::Base
# Update the topic's last reply information # Update the topic's last reply information
topic.update!( topic.update!(
last_posted_at: post.updated_at, last_posted_at: post.created_at,
last_post_user_id: post.user_id last_post_user_id: post.user_id,
bumped_at: post.created_at
) )
update_highest_processed_personal_id(msg_id) update_highest_processed_personal_id(msg_id)
@ -1952,6 +2149,26 @@ class GossamerForumsImporter < ImportScripts::Base
end end
end end
def update_existing_personal_message_activity
puts "Updating Activity field for existing private / personal message topics..."
private_message_topics = Topic.where(archetype: Archetype.private_message)
private_message_topics.each do |topic|
last_post = topic.posts.order(created_at: :desc).first
if last_post
topic.update_columns(
last_posted_at: last_post.created_at,
last_post_user_id: last_post.user_id,
bumped_at: last_post.created_at
)
puts "Updated topic ID #{topic.id} with last activity at #{last_post.updated_at}"
else
puts "No posts found for topic ID #{topic.id}; skipping update."
end
end
end
# Main method to perform the import # Main method to perform the import
def perform_import def perform_import
@ -1983,25 +2200,30 @@ class GossamerForumsImporter < ImportScripts::Base
puts "Starting Gossamer Forums import... #{timestamp}" puts "Starting Gossamer Forums import... #{timestamp}"
# add_former_user ### add_former_user
# import_users ### import_users
# generate_user_id_mapping ### generate_user_id_mapping
# export_username_mapping_to_csv("/bitnami/discourse/sqlite/gossamer-migration-username-mapping#{timestamp}") ### export_username_mapping_to_csv("/bitnami/discourse/sqlite/gossamer-migration-username-mapping#{timestamp}")
## set_user_bio_images ### set_user_bio_images
# import_categories ### import_categories
####### import_topics_and_posts_with_attachments ### import_topics_and_posts
threaded_topic_import ### threaded_topic_import
update_topic_stats ### update_topic_stats
update_user_stats ### update_user_stats
export_url_mapping_to_csv("/bitnami/discourse/sqlite/gossamer-migration-url-mapping#{timestamp}")
create_nginx_rewrite_rules("/bitnami/discourse/sqlite/gossamer-redirects#{timestamp}.conf")
import_personal_messages ### update_topic_stats_final
update_user_stats_finalfinal
### export_url_mapping_to_csv("/bitnami/discourse/sqlite/gossamer-migration-url-mapping#{timestamp}")
### export_nginx_rewrite_rules("/bitnami/discourse/sqlite/gossamer-redirects#{timestamp}.conf")
#### update_existing_personal_message_activity
### import_personal_messages
puts "Gossamer Forums import complete! #{timestamp}" puts "Gossamer Forums import complete! #{timestamp}"
end end

207
softdeldeletedposts.rb Normal file
View File

@ -0,0 +1,207 @@
# Federated Computer, Inc.
# David Sainty <saint@federated.computer> 2024 A.D.
# Gossamer Threads to Discourse -- Soft Deletion of Marked-As-Deleted Posts
# v0.3 Ready for actual soft-delete run on live-prod
require 'mysql2'
require 'active_record'
# require 'concurrent-ruby'
require File.expand_path("../../../../config/environment", __FILE__)
require File.expand_path("../../../../script/import_scripts/base", __FILE__)
class GossamerForumsSoftDelDeletedPosts < 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
# Define a method to find a post by custom field
def find_post_by_custom_field(post_id)
puts "SoftDelDeletedPosts: Searching for post with original_gossamer_id: #{post_id}"
post_custom_field = PostCustomField.find_by(name: 'original_gossamer_id', value: post_id.to_s)
if post_custom_field
post = post_custom_field.post
puts "SoftDelDeletedPosts: Found post with id: #{post.id}"
post
else
puts "SoftDelDeletedPosts: No post found with original_gossamer_id: #{post_id}"
nil
end
end
# Soft delete the post, whether topic post (OP) or reply post
def soft_delete_post(post, deleted_by_id)
if post.deleted_at.nil?
# # Get the post's owner and topic information
# user = User.find_by(id: post.user_id) # Fetch user details
# topic = Topic.find_by(id: post.topic_id) # Fetch topic details
# # Display post, user, and topic information
# puts "DELETING POST: SoftDelDeletedPosts: Soft deleting post with id: #{post.id}"
# puts " - Post owner: #{user.username} (ID: #{user.id})"
# puts " - Topic title: '#{topic.title}' (Topic ID: #{topic.id})"
# Perform the soft delete by updating the fields
## post.deleted_at = Time.now
## post.deleted_by_id = deleted_by_id
## post.save
puts "... actual deleted_at step"
post.update(deleted_at: Time.now, deleted_by_id: deleted_by_id)
else
puts "SoftDelDeletedPosts: Post with id: #{post.id} is already soft deleted."
end
end
# Define the method to delete posts based on the Gossamer Forums flag
def soft_del_deleted_posts_from_gossamer_with_user(username)
# Query the user ID from the legacy MySQL database
user_result = @mysql_client.query("SELECT user_id FROM gforum_User WHERE user_username = '#{username}' LIMIT 1")
user_id_row = user_result.first
if user_id_row.nil?
puts "DiscourseDeletedPosts: No user found with username: #{username}"
return
end
user_id = user_id_row['user_id']
# Find all posts marked as deleted by the given user
posts_result = @mysql_client.query("SELECT post_id FROM gforum_Post WHERE post_deleted = 1 AND user_id_fk = #{user_id}")
# "admin" user is ID 1, so set as the one performing the deletions
deleted_by_id = 1
posts_result.each do |legacy_post|
post_id = legacy_post['post_id']
# Look for the post in Discourse by custom field
post = find_post_by_custom_field(post_id)
if post
# Soft delete only the individual post (whether topic post / OP or a reply post)
soft_delete_post(post, deleted_by_id)
end
end
end
# Define the method to reverse (soft undelete) the soft deletion
def soft_undelete_post(post)
if post.deleted_at.present?
# Get the post's owner and topic information
user = User.find_by(id: post.user_id) # Fetch user details
topic = Topic.find_by(id: post.topic_id) # Fetch topic details
# Display post, user, and topic information
puts "RESTORING POST: SoftDelDeletedPosts: Soft undeleting post with id: #{post.id}"
puts " - Post owner: #{user.username} (ID: #{user.id})"
puts " - Topic title: '#{topic.title}' (Topic ID: #{topic.id})"
# Reverse the soft delete by setting deleted_at and deleted_by_id to nil
### post.update(deleted_at: nil, deleted_by_id: nil)
else
puts "SoftDelDeletedPosts: Post with id: #{post.id} is not soft deleted."
end
end
# Define the method to reverse the soft deletion for all legacy posts
def soft_undelete_all_deleted_posts
# Query the legacy database for posts marked as deleted
posts_result = @mysql_client.query("SELECT post_id FROM gforum_Post WHERE post_deleted = 1")
posts_result.each do |legacy_post|
post_id = legacy_post['post_id']
# Look for the post in Discourse by custom field
post = find_post_by_custom_field(post_id)
if post
# Reverse the soft deletion (restore the post)
soft_undelete_post(post)
end
end
end
# Define the method to delete posts based on the Gossamer Forums flag
def soft_del_all_deleted_posts
# Find all posts marked as deleted by the given user
posts_result = @mysql_client.query("SELECT post_id FROM gforum_Post WHERE post_deleted = 1")
# "admin" user is ID 1, so set as the one performing the deletions
deleted_by_id = 1
posts_result.each do |legacy_post|
post_id = legacy_post['post_id']
# # Look for the post in Discourse by custom field
# post = find_post_by_custom_field(post_id)
#
# if post
# # Soft delete only the individual post (whether topic post / OP or a reply post)
# soft_delete_post(post, deleted_by_id)
# end
soft_del_all_deleted_posts_by_custom_field(post_id, deleted_by_id)
end
end
# Define a method to find all posts by the original Gossamer Forums post ID and soft delete them
def soft_del_all_deleted_posts_by_custom_field(post_id, deleted_by_id)
puts "SoftDelDeletedPosts: Searching for all posts with original_gossamer_id: #{post_id}"
# Find all PostCustomField records with the given Gossamer Forums post_id
post_custom_fields = PostCustomField.where(name: 'original_gossamer_id', value: post_id.to_s)
if post_custom_fields.any?
post_custom_fields.each do |post_custom_field|
post = post_custom_field.post
if post
puts "DELETING POST: SoftDelDeletedPosts: Soft deleting post with id: #{post.id} and user_id: #{post.user_id} and topic_id: #{post.topic_id}"
# Get the post's owner and topic information
# user = User.find_by(id: post.user_id) # Fetch user details
# topic = Topic.find_by(id: post.topic_id) # Fetch topic details
# Display post, user, and topic information
# puts " - Post owner: #{user.username} (ID: #{user.id})"
# puts " - Topic title: '#{topic.title}' (Topic ID: #{topic.id})"
# Soft delete the post if not already deleted
soft_delete_post(post, deleted_by_id)
else
puts "SoftDelDeletedPosts: Found PostCustomField with no corresponding post (ID: #{post_custom_field.id})"
end
end
else
puts "SoftDelDeletedPosts: No PostCustomField records found with original_gossamer_id: #{post_id}"
end
end
def perform_deleted_soft_del
puts "Soft Del Deleted Posts beginning!"
## soft_del_deleted_posts_from_gossamer_with_user('spudone')
## soft_undelete_all_deleted_posts
soft_del_all_deleted_posts
puts "Soft Del Deleted Posts complete!"
end
end
GossamerForumsSoftDelDeletedPosts.new.perform_deleted_soft_del