v0.31 Significant update of private message code - field population, threading of replies, visibility to sender

This commit is contained in:
David Sainty 2024-07-12 23:00:17 +10:00
parent d99bb7201f
commit 5395e7984e

View File

@ -1,5 +1,5 @@
# gossamer threads migration-import code # gossamer threads migration-import code
# v0.30.1 # v0.31
require 'mysql2' require 'mysql2'
require 'open-uri' require 'open-uri'
@ -1222,7 +1222,16 @@ class GossamerForumsImporter < ImportScripts::Base
sanitized_title = row['msg_subject']&.strip sanitized_title = row['msg_subject']&.strip
sanitized_title = "<no subject>" if sanitized_title.nil? || sanitized_title.empty? sanitized_title = "<no subject>" 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 # Create a private message topic in Discourse
topic = Topic.create!( topic = Topic.create!(
@ -1230,11 +1239,25 @@ class GossamerForumsImporter < ImportScripts::Base
user_id: from_user_id, user_id: from_user_id,
archetype: Archetype.private_message, archetype: Archetype.private_message,
created_at: Time.at(row['msg_time']), created_at: Time.at(row['msg_time']),
updated_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.custom_fields['original_gossamer_msg_id'] = row['msg_id']
topic.save! 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 the message as a post in the private topic # Create the message as a post in the private topic
post = Post.create!( post = Post.create!(
topic_id: topic.id, topic_id: topic.id,
@ -1246,8 +1269,11 @@ class GossamerForumsImporter < ImportScripts::Base
post.custom_fields['original_gossamer_msg_id'] = row['msg_id'] post.custom_fields['original_gossamer_msg_id'] = row['msg_id']
post.save! post.save!
# Add recipient user to the private message topic # Update the topic's last reply information
topic.topic_allowed_users.create!(user_id: to_user_id) topic.update!(
last_posted_at: post.updated_at,
last_post_user_id: post.user_id
)
update_highest_processed_personal_id(msg_id) update_highest_processed_personal_id(msg_id)
@ -1261,7 +1287,6 @@ class GossamerForumsImporter < ImportScripts::Base
end end
# Main method to perform the import # Main method to perform the import
def perform_import def perform_import
# Secret trick to disable RateLimiting protection in Discourse # Secret trick to disable RateLimiting protection in Discourse