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) existing_user = User.find_by(username: sanitized)
if existing_user if existing_user
# If email and name match, do not modify the username # If email and name match, do not modify the username
if existing_user.email == email && existing_user.name == name # 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 return sanitized
else else
# Ensure the username is unique # Ensure the username is unique
@ -51,8 +53,8 @@ class GossamerForumsImporter < ImportScripts::Base
# Print the original and sanitized usernames if they differ # Print the original and sanitized usernames if they differ
if original_username != sanitized if original_username != sanitized
puts "Sanitized username: '#{original_username}' --> '#{sanitized}'" puts "Sanitized username: '#{original_username}' --> '#{sanitized}'"
else # else
puts "UNsanitized username: '#{original_username}' --> '#{sanitized}'" # puts "UNsanitized username: '#{original_username}' --> '#{sanitized}'"
end end
sanitized sanitized
@ -60,12 +62,11 @@ class GossamerForumsImporter < ImportScripts::Base
# Sanitize email to replace restricted domains # Sanitize email to replace restricted domains
def sanitize_email(email) def sanitize_email(email)
# mailinator.com is not allowed by
restricted_domains = ['mailinator.com', 'example.com'] # Add more restricted domains as needed restricted_domains = ['mailinator.com', 'example.com'] # Add more restricted domains as needed
domain = email.split('@').last domain = email.split('@').last
if restricted_domains.include?(domain) 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}'" puts "Sanitized email: '#{email}' --> '#{sanitized_email}'"
return sanitized_email return sanitized_email
end end
@ -79,17 +80,25 @@ class GossamerForumsImporter < ImportScripts::Base
users = [] users = []
execute_query("SELECT * FROM gforum_User").each do |row| 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 << { users << {
id: row['user_id'], id: row['user_id'],
username: sanitize_username(row['user_username'], row['user_email'], row['user_real_name']), username: sanitized_username,
email: row['user_email'], email: row['user_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'],
title: row['user_title'], title: row['user_title'],
bio_raw: row['user_about'], bio_raw: row['user_about'] || "",
website: row['user_homepage'], 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 end
@ -98,24 +107,17 @@ class GossamerForumsImporter < ImportScripts::Base
user user
end end
# Update user passwords and import user files # Import user files after creating users
users.each do |user| users.each do |user|
discourse_username = sanitize_username(user[:username], user[:email], user[:name]) discourse_user = User.find_by(username: user[:username])
discourse_user = User.find_by(username: discourse_username)
if discourse_user.nil? 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 next
end end
if discourse_user.custom_fields.nil? # Update user profile bio_raw with user_about
discourse_user.custom_fields = {} discourse_user.user_profile.bio_raw = (discourse_user.user_profile.bio_raw || "") + "\n\n" + (user[:bio_raw] || "")
end discourse_user.user_profile.save!
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
import_user_files(discourse_user) import_user_files(discourse_user)
end end
@ -124,14 +126,27 @@ class GossamerForumsImporter < ImportScripts::Base
# Import user files and append to user's bio # Import user files and append to user's bio
def import_user_files(user) def import_user_files(user)
print "\rImporting files for user #{user.username}..." 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 # Construct file URL
file_url = "https://forum.slowtwitch.com/images/users/images/#{file['ID'] % 10}/#{file['ID']}-#{file['File_Name']}" 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 # Append image link to user's bio
user.bio_raw += "\n\n![#{file['File_Name']}](#{file_url})" user.user_profile.bio_raw += "\n\n![#{file['File_Name']}](#{file_url})"
user.save! user.user_profile.save!
end end
print "\rImporting files for user #{user.username}... Done.\n" print "Importing files for user #{user.username}... Done.\n"
end end
# Import categories from gforum_Category table # Import categories from gforum_Category table