mirror of
https://github.com/discourse/discourse.git
synced 2025-06-18 08:13:49 +08:00
FEATURE: Extend PM recipient bulk imports (#27063)
* FIX: Support multiple topic allowed user imports * FEATURE: Add topic allowed groups import support
This commit is contained in:
@ -636,6 +636,8 @@ class BulkImport::Base
|
|||||||
|
|
||||||
TOPIC_ALLOWED_USER_COLUMNS ||= %i[topic_id user_id created_at updated_at]
|
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_TAG_COLUMNS ||= %i[topic_id tag_id created_at updated_at]
|
||||||
|
|
||||||
TOPIC_USER_COLUMNS ||= %i[
|
TOPIC_USER_COLUMNS ||= %i[
|
||||||
@ -879,6 +881,10 @@ class BulkImport::Base
|
|||||||
create_records(rows, "topic_allowed_user", TOPIC_ALLOWED_USER_COLUMNS, &block)
|
create_records(rows, "topic_allowed_user", TOPIC_ALLOWED_USER_COLUMNS, &block)
|
||||||
end
|
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)
|
def create_topic_tags(rows, &block)
|
||||||
create_records(rows, "topic_tag", TOPIC_TAG_COLUMNS, &block)
|
create_records(rows, "topic_tag", TOPIC_TAG_COLUMNS, &block)
|
||||||
end
|
end
|
||||||
@ -1307,6 +1313,12 @@ class BulkImport::Base
|
|||||||
topic_allowed_user
|
topic_allowed_user
|
||||||
end
|
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)
|
def process_topic_tag(topic_tag)
|
||||||
topic_tag[:created_at] = NOW
|
topic_tag[:created_at] = NOW
|
||||||
topic_tag[:updated_at] = NOW
|
topic_tag[:updated_at] = NOW
|
||||||
|
@ -86,6 +86,7 @@ class BulkImport::Generic < BulkImport::Base
|
|||||||
|
|
||||||
import_topic_tags
|
import_topic_tags
|
||||||
import_topic_allowed_users
|
import_topic_allowed_users
|
||||||
|
import_topic_allowed_groups
|
||||||
|
|
||||||
import_likes
|
import_likes
|
||||||
import_votes
|
import_votes
|
||||||
@ -676,30 +677,30 @@ class BulkImport::Generic < BulkImport::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def import_topic_allowed_users
|
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..."
|
puts "", "Importing topic_allowed_users..."
|
||||||
|
|
||||||
topics = query(<<~SQL)
|
topics = query(<<~SQL)
|
||||||
SELECT *
|
SELECT
|
||||||
FROM topics
|
t.id,
|
||||||
WHERE private_message IS NOT NULL
|
user_ids.value AS user_id
|
||||||
ORDER BY 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
|
SQL
|
||||||
|
|
||||||
added = 0
|
added = 0
|
||||||
|
existing_topic_allowed_users = TopicAllowedUser.pluck(:topic_id, :user_id).to_set
|
||||||
|
|
||||||
create_topic_allowed_users(topics) do |row|
|
create_topic_allowed_users(topics) do |row|
|
||||||
next unless (topic_id = topic_id_from_imported_id(row["id"]))
|
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(row["user_id"])
|
||||||
user_id = user_id_from_imported_id(imported_user_id)
|
|
||||||
|
next unless topic_id && user_id
|
||||||
|
next unless existing_topic_allowed_users.add?([topic_id, user_id])
|
||||||
|
|
||||||
added += 1
|
added += 1
|
||||||
{
|
|
||||||
# FIXME: missing imported_id
|
{ topic_id: topic_id, user_id: user_id }
|
||||||
topic_id: topic_id,
|
|
||||||
user_id: user_id,
|
|
||||||
}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
topics.close
|
topics.close
|
||||||
@ -707,6 +708,40 @@ class BulkImport::Generic < BulkImport::Base
|
|||||||
puts " Added #{added} topic_allowed_users records."
|
puts " Added #{added} topic_allowed_users records."
|
||||||
end
|
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
|
def import_posts
|
||||||
puts "", "Importing posts..."
|
puts "", "Importing posts..."
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user