diff --git a/gossamer_forums.rb b/gossamer_forums.rb index 6092efa..cf4f2fc 100644 --- a/gossamer_forums.rb +++ b/gossamer_forums.rb @@ -1,5 +1,5 @@ # gossamer threads migration-import code -# v0.28.1 +# v0.29 require 'mysql2' require 'open-uri' @@ -1160,75 +1160,79 @@ class GossamerForumsImporter < ImportScripts::Base # Import personal messages from gforum_Message table (both inbox and sent messages) def import_personal_messages - puts "Importing personal (inbox and sendmail) messages..." + puts "Importing personal (inbox and sentmail) messages..." # Fetch the highest_processed_personal_id highest_processed_personal_id = fetch_highest_processed_personal_id.to_i puts "Highest processed personal_id: #{highest_processed_personal_id}" -# OVERRIDE - to get to problem msg - highest_processed_personal_id = 350 +# OVERRIDE - to speed getting to problem msg +# highest_processed_personal_id = 350 puts "Highest processed personal_id override: #{highest_processed_personal_id}" execute_query("SELECT * FROM gforum_Message").each do |row| - msg_id = row['msg_id'].to_i - puts "msg_id #{msg_id}" - - # Skip posts that have already been processed - next if msg_id <= highest_processed_personal_id - - from_user_id = fetch_user_id_mapping(row['from_user_id_fk']) - to_user_id = fetch_user_id_mapping(row['to_user_id_fk']) + begin + msg_id = row['msg_id'].to_i + puts "msg_id #{msg_id}" - next unless from_user_id && to_user_id + # Skip posts that have already been processed + next if msg_id <= highest_processed_personal_id - # Skip if the message already exists - unless TopicCustomField.exists?(name: 'original_gossamer_msg_id', value: row['msg_id']) + from_user_id = fetch_user_id_mapping(row['from_user_id_fk']) + to_user_id = fetch_user_id_mapping(row['to_user_id_fk']) + + next unless from_user_id && to_user_id + + # Skip if the message already exists + unless TopicCustomField.exists?(name: 'original_gossamer_msg_id', value: row['msg_id']) + + # Sanitize the message, ensuring we have an empty string or the content without any \0 + sanitized_message = sanitize_post_message(row['msg_body']) + + # Set default message body if the sanitized message is blank + sanitized_message = "" if sanitized_message.strip.empty? || sanitized_message.split.size < 3 + + # If we do not change the "min personal message post length" to 1, we need this. ... We may need this anyway. + sanitized_message = sanitized_message.ljust(10, '.') if sanitized_message.length < 10 + + # Check and set a default title if the original title is nil or empty + sanitized_title = row['msg_subject']&.strip + sanitized_title = "" if sanitized_title.nil? || sanitized_title.empty? + + puts "IMPORTING msg. sanitized: #{sanitized_title} user_id #{from_user_id} to_user_id #{to_user_id}" - # Sanitize the message, ensuring we have an empty string or the content without any \0 - sanitized_message = sanitize_post_message(row['msg_body']) + # Create a private message topic in Discourse + topic = Topic.create!( + title: sanitized_title, + user_id: from_user_id, + archetype: Archetype.private_message, + created_at: Time.at(row['msg_time']), + updated_at: Time.at(row['msg_time']) + ) + topic.custom_fields['original_gossamer_msg_id'] = row['msg_id'] + topic.save! + + # Create the message as a post in the private topic + post = Post.create!( + topic_id: topic.id, + user_id: from_user_id, + raw: sanitized_message, + created_at: Time.at(row['msg_time']), + updated_at: Time.at(row['msg_time']) + ) + post.custom_fields['original_gossamer_msg_id'] = row['msg_id'] + post.save! + + # Add recipient user to the private message topic + topic.topic_allowed_users.create!(user_id: to_user_id) - # Set default message body if the sanitized message is blank - sanitized_message = "" if sanitized_message.strip.empty? + update_highest_processed_personal_id(msg_id) - # If we do not change the "min personal message post length" to 1, we need this. ... We may need this anyway. - sanitized_message = sanitized_message.ljust(10, '.') if sanitized_message.length < 10 - - # Check and set a default title if the original title is nil or empty - sanitized_title = row['msg_subject']&.strip - sanitized_title = "" if sanitized_title.nil? || sanitized_title.empty? - - puts "IMPORTING msg. sanitized_title: #{sanitized_title} orig title: #{row['msg_subject']} user_id #{from_user_id} to_user_id #{to_user_id}" - - # Create a private message topic in Discourse - topic = Topic.create!( - title: sanitized_title, - user_id: from_user_id, - archetype: Archetype.private_message, - created_at: Time.at(row['msg_time']), - updated_at: Time.at(row['msg_time']) - ) - topic.custom_fields['original_gossamer_msg_id'] = row['msg_id'] - topic.save! - - # Create the message as a post in the private topic - post = Post.create!( - topic_id: topic.id, - user_id: from_user_id, - raw: sanitized_message, - created_at: Time.at(row['msg_time']), - updated_at: Time.at(row['msg_time']) - ) - post.custom_fields['original_gossamer_msg_id'] = row['msg_id'] - post.save! - - # Add recipient user to the private message topic - topic.topic_allowed_users.create!(user_id: to_user_id) - - update_highest_processed_personal_id(msg_id) - # N/A. These were never a thing in Slowtwitch... -# handle_post_attachments(row['msg_id'], post, from_user_id) +# handle_post_attachments(row['msg_id'], post, from_user_id) + end + rescue StandardError => e + puts "Error importing message #{row['msg_id']}: #{e.message}" end end end @@ -1250,11 +1254,11 @@ class GossamerForumsImporter < ImportScripts::Base # import_categories -# import_topics_and_posts_with_attachments -# update_topic_stats + import_topics_and_posts_with_attachments + update_topic_stats update_user_stats -# export_url_mapping_to_csv("gossamer-migration-url-mapping#{timestamp}") -# create_nginx_rewrite_rules("gossamer-redirects#{timestamp}.conf") + export_url_mapping_to_csv("gossamer-migration-url-mapping#{timestamp}") + create_nginx_rewrite_rules("gossamer-redirects#{timestamp}.conf") import_personal_messages