Discourse import IPS4 script

help shape the import of private messages

there is code written for the previous version of ipb3
in the new version of ips4, some tables have been renamed

Сводка

def import_private_messages
puts “”, “importing private messages…”

topic_count = mysql_query("SELECT COUNT(msg_id) count FROM #{TABLE_PREFIX}message_posts").first["count"]

last_private_message_topic_id = -1

batches(BATCH_SIZE) do |offset|
  private_messages = mysql_query(<<-SQL
      SELECT msg_id pmtextid,
             msg_topic_id topic_id,
             msg_author_id fromuserid,
             mt_title title,
             msg_post message,
             
             mt_invited_members touserarray,
             
             mt_to_member_id to_user_id,
             msg_is_first_post first_post,
             msg_date dateline
        FROM #{TABLE_PREFIX}message_topics, #{TABLE_PREFIX}message_posts
       WHERE msg_topic_id = mt_id
         AND msg_date > UNIX_TIMESTAMP(STR_TO_DATE('#{IMPORT_AFTER}', '%Y-%m-%d'))
    ORDER BY msg_topic_id, msg_id
       LIMIT #{BATCH_SIZE}
      OFFSET #{offset}
  SQL
                                )

  puts "Processing #{private_messages.count} messages"
  break if private_messages.count < 1
  puts "Processing . . . "
  private_messages = private_messages.reject { |pm| @lookup.post_already_imported?("pm-#{pm['pmtextid']}") }

  title_username_of_pm_first_post = {}

  create_posts(private_messages, total: topic_count, offset: offset) do |m|
    skip = false
    mapped = {}

    mapped[:id] = "pm-#{m['pmtextid']}"
    mapped[:user_id] = user_id_from_imported_user_id(m['fromuserid']) || Discourse::SYSTEM_USER_ID
    mapped[:raw] = clean_up(m['message']) rescue nil
    mapped[:created_at] = Time.zone.at(m['dateline'])
    title = @htmlentities.decode(m['title']).strip[0...255]
    topic_id = nil

    next if mapped[:raw].blank?

    # users who are part of this private message.
    target_usernames = []
    target_userids = []
    begin
      to_user_array = [ m['to_user_id'] ] + array_from_members_string(m['touserarray'])
    rescue
      puts "#{m['pmtextid']} -- #{m['touserarray']}"
      skip = true
    end

    begin
      to_user_array.each do |to_user|
        user_id = user_id_from_imported_user_id(to_user)
        username = User.find_by(id: user_id).try(:username)
        target_userids << user_id || Discourse::SYSTEM_USER_ID
        target_usernames << username if username
        if user_id
          puts "Found user: #{to_user} -- #{user_id} -- #{username}"
        else
          puts "Can't find user: #{to_user}"
        end
      end
    rescue
      puts "skipping pm-#{m['pmtextid']} `to_user_array` is broken -- #{to_user_array.inspect}"
      skip = true
    end

    participants = target_userids
    participants << mapped[:user_id]
    begin
      participants.sort!
    rescue
      puts "one of the participant's id is nil -- #{participants.inspect}"
    end

    if last_private_message_topic_id != m['topic_id']
      last_private_message_topic_id = m['topic_id']
      puts "New message: #{m['topic_id']}: #{title} from #{m['fromuserid']} (#{mapped[:user_id]})" unless QUIET
      # topic post message
      topic_id = m['topic_id']
      mapped[:title] = title
      mapped[:archetype] = Archetype.private_message
      mapped[:target_usernames] = target_usernames.join(',')
      if mapped[:target_usernames].size < 1 # pm with yourself?
        # skip = true
        mapped[:target_usernames] = "system"
        puts "pm-#{m['pmtextid']} has no target (#{m['touserarray']})"
      end
    else # reply
      topic_id = topic_lookup_from_imported_post_id("pm-#{topic_id}")
      if !topic_id
        skip = true
      end
      mapped[:topic_id] = topic_id
      puts "Reply message #{topic_id}: #{m['topic_id']}: from #{m['fromuserid']} (#{mapped[:user_id]})"  unless QUIET
    end
    #        puts "#{target_usernames} -- #{mapped[:target_usernames]}"
    #        puts "Adding #{mapped}"
    skip ? nil : mapped
    #        puts "#{'-'*50}> added"
  end
end

this is how the table looks now

the problem occurs with mt_invited_members

if you remove this line of the message from the request, it is imported but the chain of responses is lost. I hardly understand what editing needs to be made so that private messages are imported correctly.
tried to substitute mt_invited_members mt_starter_id in place

messages have not been imported at all

at the moment I managed to make one of two import scripts which imports from Ips 4.5 to Discourse

Users
Avatars
private messages partially
Categories
Forums
Topics

have some problems importing attachments. when the script is completed, I will definitely share it with the public

table contents