From 61e12aaebe191fac7f59afdeccdd77928838d34f Mon Sep 17 00:00:00 2001 From: Selase Krakani <849886+s3lase@users.noreply.github.com> Date: Fri, 17 May 2024 13:45:20 +0200 Subject: [PATCH] FEATURE: Extend PM recipient bulk imports (#27063) * FIX: Support multiple topic allowed user imports * FEATURE: Add topic allowed groups import support --- script/bulk_import/base.rb | 12 ++++++ script/bulk_import/generic_bulk.rb | 65 +++++++++++++++++++++++------- 2 files changed, 62 insertions(+), 15 deletions(-) diff --git a/script/bulk_import/base.rb b/script/bulk_import/base.rb index dd3afaa57a1..5db199f1848 100644 --- a/script/bulk_import/base.rb +++ b/script/bulk_import/base.rb @@ -636,6 +636,8 @@ class BulkImport::Base TOPIC_ALLOWED_USER_COLUMNS ||= %i[topic_id user_id created_at updated_at] + TOPIC_ALLOWED_GROUP_COLUMNS ||= %i[topic_id group_id created_at updated_at] + TOPIC_TAG_COLUMNS ||= %i[topic_id tag_id created_at updated_at] TOPIC_USER_COLUMNS ||= %i[ @@ -879,6 +881,10 @@ class BulkImport::Base create_records(rows, "topic_allowed_user", TOPIC_ALLOWED_USER_COLUMNS, &block) end + def create_topic_allowed_groups(rows, &block) + create_records(rows, "topic_allowed_group", TOPIC_ALLOWED_GROUP_COLUMNS, &block) + end + def create_topic_tags(rows, &block) create_records(rows, "topic_tag", TOPIC_TAG_COLUMNS, &block) end @@ -1307,6 +1313,12 @@ class BulkImport::Base topic_allowed_user end + def process_topic_allowed_group(topic_allowed_group) + topic_allowed_group[:created_at] = NOW + topic_allowed_group[:updated_at] = NOW + topic_allowed_group + end + def process_topic_tag(topic_tag) topic_tag[:created_at] = NOW topic_tag[:updated_at] = NOW diff --git a/script/bulk_import/generic_bulk.rb b/script/bulk_import/generic_bulk.rb index 3830e682fa4..d241f2c22dc 100644 --- a/script/bulk_import/generic_bulk.rb +++ b/script/bulk_import/generic_bulk.rb @@ -86,6 +86,7 @@ class BulkImport::Generic < BulkImport::Base import_topic_tags import_topic_allowed_users + import_topic_allowed_groups import_likes import_votes @@ -676,30 +677,30 @@ class BulkImport::Generic < BulkImport::Base end def import_topic_allowed_users - # FIXME: This is not working correctly because it imports only the first user from the list! - # Groups are ignored completely. And there is no check for existing records. - puts "", "Importing topic_allowed_users..." topics = query(<<~SQL) - SELECT * - FROM topics - WHERE private_message IS NOT NULL - ORDER BY id + SELECT + t.id, + user_ids.value AS user_id + FROM topics t, JSON_EACH(t.private_message, '$.user_ids') AS user_ids + WHERE t.private_message IS NOT NULL + ORDER BY t.id SQL added = 0 + existing_topic_allowed_users = TopicAllowedUser.pluck(:topic_id, :user_id).to_set create_topic_allowed_users(topics) do |row| - next unless (topic_id = topic_id_from_imported_id(row["id"])) - imported_user_id = JSON.parse(row["private_message"])["user_ids"].first - user_id = user_id_from_imported_id(imported_user_id) + topic_id = topic_id_from_imported_id(row["id"]) + user_id = user_id_from_imported_id(row["user_id"]) + + next unless topic_id && user_id + next unless existing_topic_allowed_users.add?([topic_id, user_id]) + added += 1 - { - # FIXME: missing imported_id - topic_id: topic_id, - user_id: user_id, - } + + { topic_id: topic_id, user_id: user_id } end topics.close @@ -707,6 +708,40 @@ class BulkImport::Generic < BulkImport::Base puts " Added #{added} topic_allowed_users records." end + def import_topic_allowed_groups + puts "", "Importing topic_allowed_groups..." + + topics = query(<<~SQL) + SELECT + t.id, + group_ids.value AS group_id + FROM topics t, JSON_EACH(t.private_message, '$.group_ids') AS group_ids + WHERE t.private_message IS NOT NULL + ORDER BY t.id + SQL + + added = 0 + existing_topic_allowed_groups = TopicAllowedGroup.pluck(:topic_id, :group_id).to_set + + create_topic_allowed_groups(topics) do |row| + topic_id = topic_id_from_imported_id(row["id"]) + group_id = group_id_from_imported_id(row["group_id"]) + + next unless topic_id && group_id + next unless existing_topic_allowed_groups.add?([topic_id, group_id]) + + added += 1 + + { topic_id: topic_id, group_id: group_id } + end + + # TODO: Add support for special group names + + topics.close + + puts " Added #{added} topic_allowed_groups records." + end + def import_posts puts "", "Importing posts..."