diff --git a/gossamer_forums.rb b/gossamer_forums.rb index 4546910..014ce2d 100644 --- a/gossamer_forums.rb +++ b/gossamer_forums.rb @@ -1,5 +1,5 @@ # gossamer threads migration-import code -# v0.30.1 +# v0.31 require 'mysql2' require 'open-uri' @@ -1221,20 +1221,43 @@ class GossamerForumsImporter < ImportScripts::Base # 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}" + + # Check for an existing private message topic between the same two users with a similar title + topic = Topic.joins(:topic_allowed_users, :custom_fields) + .where(archetype: Archetype.private_message) + .where("topic_allowed_users.user_id = ? AND topic_allowed_users.user_id = ?", from_user_id, to_user_id) + .where("title = ? OR title = ?", sanitized_title, "Re: #{sanitized_title}") + .where(topic_custom_fields: { name: 'original_gossamer_msg_id' }) + .first + + if topic.nil? + puts "IMPORTING new message topic sanitized: #{sanitized_title} 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']), + last_posted_at: Time.at(row['msg_time']), + last_post_user_id: from_user_id + ) + topic.custom_fields['original_gossamer_msg_id'] = row['msg_id'] + topic.save! + + # Add recipient user to the private message topic + topic.topic_allowed_users.create!(user_id: to_user_id) + + # Add sender user to the private message topic + topic.topic_allowed_users.create!(user_id: from_user_id) + else + puts "APPENDING to existing message topic sanitized: #{sanitized_title} user_id #{from_user_id} to_user_id #{to_user_id}" + + # Increment the number of replies for the topic + topic.increment!(:posts_count) + end - # 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, @@ -1245,14 +1268,17 @@ class GossamerForumsImporter < ImportScripts::Base ) 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 the topic's last reply information + topic.update!( + last_posted_at: post.updated_at, + last_post_user_id: post.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) + # N/A. These were never a thing in Slowtwitch... + # handle_post_attachments(row['msg_id'], post, from_user_id) end rescue StandardError => e puts "Error importing message #{row['msg_id']}: #{e.message}" @@ -1261,7 +1287,6 @@ class GossamerForumsImporter < ImportScripts::Base end - # Main method to perform the import def perform_import # Secret trick to disable RateLimiting protection in Discourse