This commit is contained in:
dsainty 2024-06-17 19:34:34 +10:00
parent 7d2907f3d0
commit d2edc3f39f

View File

@ -34,8 +34,10 @@ class GossamerForumsImporter < ImportScripts::Base
existing_user = User.find_by(username: sanitized)
if existing_user
# If email and name match, do not modify the username
if existing_user.email == email && existing_user.name == name
# If email and name match, do not modify the username
# puts "existing_user.email: '#{existing_user.email}' vs. email: '#{email}'"
# puts "existing_user.name: '#{existing_user.name}' vs. email: '#{name}'"
if existing_user.email.downcase == email.downcase && existing_user.name == name
return sanitized
else
# Ensure the username is unique
@ -51,8 +53,8 @@ class GossamerForumsImporter < ImportScripts::Base
# Print the original and sanitized usernames if they differ
if original_username != sanitized
puts "Sanitized username: '#{original_username}' --> '#{sanitized}'"
else
puts "UNsanitized username: '#{original_username}' --> '#{sanitized}'"
# else
# puts "UNsanitized username: '#{original_username}' --> '#{sanitized}'"
end
sanitized
@ -60,12 +62,11 @@ class GossamerForumsImporter < ImportScripts::Base
# Sanitize email to replace restricted domains
def sanitize_email(email)
# mailinator.com is not allowed by
restricted_domains = ['mailinator.com', 'example.com'] # Add more restricted domains as needed
domain = email.split('@').last
if restricted_domains.include?(domain)
sanitized_email = email.gsub(domain, 'email.invalid') # Change to a permissible domain
sanitized_email = email.gsub(domain, 'example.org') # Change to a permissible domain
puts "Sanitized email: '#{email}' --> '#{sanitized_email}'"
return sanitized_email
end
@ -79,17 +80,25 @@ class GossamerForumsImporter < ImportScripts::Base
users = []
execute_query("SELECT * FROM gforum_User").each do |row|
sanitized_username = sanitize_username(row['user_username'], row['user_email'], row['user_real_name'])
# sanitized_email = sanitize_email(row['user_email'])
users << {
id: row['user_id'],
username: sanitize_username(row['user_username'], row['user_email'], row['user_real_name']),
username: sanitized_username,
email: row['user_email'],
created_at: Time.at(row['user_registered']),
updated_at: Time.at(row['user_last_seen']),
name: row['user_real_name'],
title: row['user_title'],
bio_raw: row['user_about'],
bio_raw: row['user_about'] || "",
website: row['user_homepage'],
location: row['user_location']
location: row['user_location'],
custom_fields: {
'md5_password' => row['user_password'],
'original_gossamer_username' => row['user_username'],
'original_gossamer_id' => row['user_id']
}
}
end
@ -98,24 +107,17 @@ class GossamerForumsImporter < ImportScripts::Base
user
end
# Update user passwords and import user files
# Import user files after creating users
users.each do |user|
discourse_username = sanitize_username(user[:username], user[:email], user[:name])
discourse_user = User.find_by(username: discourse_username)
discourse_user = User.find_by(username: user[:username])
if discourse_user.nil?
puts "User #{user[:username]} --> #{discourse_username} not found in Discourse. Skipping password update."
puts "User #{user[:username]} not found in Discourse. Skipping file import."
next
end
if discourse_user.custom_fields.nil?
discourse_user.custom_fields = {}
end
execute_query("SELECT * FROM gforum_User WHERE user_id = #{user[:id]}").each do |row|
discourse_user.custom_fields['md5_password'] = row['user_password']
discourse_user.save_custom_fields
end
# Update user profile bio_raw with user_about
discourse_user.user_profile.bio_raw = (discourse_user.user_profile.bio_raw || "") + "\n\n" + (user[:bio_raw] || "")
discourse_user.user_profile.save!
import_user_files(discourse_user)
end
@ -124,14 +126,27 @@ class GossamerForumsImporter < ImportScripts::Base
# Import user files and append to user's bio
def import_user_files(user)
print "\rImporting files for user #{user.username}..."
execute_query("SELECT * FROM gforum_User_Files WHERE ForeignColName = 'user_id' AND ForeignColKey = #{user.id}").each do |file|
original_gossamer_id = user.custom_fields['original_gossamer_id']
if original_gossamer_id.nil? || original_gossamer_id.empty?
puts "User #{user.username} does not have a valid original_gossamer_id. Skipping file import."
return
end
# puts "Original Gossamer ID for user #{user.username}: #{original_gossamer_id}"
execute_query("SELECT * FROM gforum_User_Files WHERE ForeignColKey = #{original_gossamer_id}").each do |file|
# execute_query("SELECT * FROM gforum_User_Files WHERE ForeignColName = 'user_id' AND ForeignColKey = #{user.id}").each do |file|
# execute_query("SELECT * FROM gforum_User_Files WHERE ForeignColKey = #{user.custom_fields['original_gossamer_id']}").each do |file|
# Construct file URL
file_url = "https://forum.slowtwitch.com/images/users/images/#{file['ID'] % 10}/#{file['ID']}-#{file['File_Name']}"
puts "User #{user.username} User ID: #{user.id} original_gossamer_id: #{user.custom_fields['original_gossamer_id']} file_url: #{file_url}"
# Append image link to user's bio
user.bio_raw += "\n\n![#{file['File_Name']}](#{file_url})"
user.save!
user.user_profile.bio_raw += "\n\n![#{file['File_Name']}](#{file_url})"
user.user_profile.save!
end
print "\rImporting files for user #{user.username}... Done.\n"
print "Importing files for user #{user.username}... Done.\n"
end
# Import categories from gforum_Category table