Adding Import Support for Discourse Reactions Plugin

This commit is contained in:
Ruben Oussoren 2024-12-18 20:59:35 -05:00
parent 07efdaa32a
commit 9c38b650fd
No known key found for this signature in database
GPG Key ID: 85F2F1725142D8C2
2 changed files with 121 additions and 4 deletions

View File

@ -925,6 +925,18 @@ class BulkImport::Base
CHAT_MENTION_COLUMNS = %i[chat_message_id target_id type created_at updated_at]
REACTION_USER_COLUMNS = %i[reaction_id user_id created_at updated_at post_id]
REACTION_COLUMNS = %i[
id
post_id
reaction_type
reaction_value
reaction_users_count
created_at
updated_at
]
def create_groups(rows, &block)
create_records(rows, "group", GROUP_COLUMNS, &block)
end
@ -1157,6 +1169,14 @@ class BulkImport::Base
create_records(rows, "chat_mention", CHAT_MENTION_COLUMNS, &block)
end
def create_reaction_users(rows, &block)
create_records(rows, "discourse_reactions_reaction_user", REACTION_USER_COLUMNS, &block)
end
def create_reactions(rows, &block)
create_records(rows, "discourse_reactions_reaction", REACTION_COLUMNS, &block)
end
def process_group(group)
@groups[group[:imported_id].to_i] = group[:id] = @last_group_id += 1
@ -1962,6 +1982,19 @@ class BulkImport::Base
mention
end
def process_discourse_reactions_reaction_user(reaction_user)
reaction_user[:created_at] ||= NOW
reaction_user[:updated_at] ||= NOW
reaction_user
end
def process_discourse_reactions_reaction(reaction)
reaction[:created_at] ||= NOW
reaction[:updated_at] ||= NOW
reaction[:reaction_users_count] ||= 0
reaction
end
def create_records(all_rows, name, columns, &block)
start = Time.now
imported_ids = []

View File

@ -126,6 +126,9 @@ class BulkImport::Generic < BulkImport::Base
update_chat_threads
update_chat_membership_metadata
import_reaction_users
import_reactions
import_upload_references
end
@ -320,7 +323,10 @@ class BulkImport::Generic < BulkImport::Base
puts "", "Importing category permissions..."
permissions = query(<<~SQL)
SELECT c.id AS category_id, p.value -> 'group_id' AS group_id, p.value -> 'permission_type' AS permission_type
SELECT c.id AS category_id,
p.value -> 'group_id' AS group_id,
p.value -> 'existing_group_id' AS existing_group_id,
p.value -> 'permission_type' AS permission_type
FROM categories c,
JSON_EACH(c.permissions) p
SQL
@ -329,7 +335,7 @@ class BulkImport::Generic < BulkImport::Base
create_category_groups(permissions) do |row|
category_id = category_id_from_imported_id(row["category_id"])
group_id = group_id_from_imported_id(row["group_id"])
group_id = row["existing_group_id"] || group_id_from_imported_id(row["group_id"])
next if existing_category_group_ids.include?([category_id, group_id])
{ category_id: category_id, group_id: group_id, permission_type: row["permission_type"] }
@ -2943,6 +2949,84 @@ class BulkImport::Generic < BulkImport::Base
puts " Update took #{(Time.now - start_time).to_i} seconds."
end
def import_reaction_users
unless defined?(::DiscourseReactions)
puts "",
"Skipping reaction users import, because the Discourse Reactions plugin is not installed."
return
end
puts "", "Importing reaction users..."
reaction_users = query(<<~SQL)
SELECT *
FROM discourse_reactions_reaction_users
ORDER BY post_id
SQL
existing_reaction_users =
DiscourseReactions::ReactionUser.pluck(:reaction_id, :user_id, :post_id).to_set
create_reaction_users(reaction_users) do |row|
next if row["reaction_id"].blank?
user_id = user_id_from_imported_id(row["user_id"])
post_id = post_id_from_imported_id(row["post_id"])
next if post_id.blank? || user_id.blank?
next unless existing_reaction_users.add?([row["reaction_id"], user_id, post_id])
{
reaction_id: row["reaction_id"],
user_id: user_id,
created_at: to_datetime(row["created_at"]),
updated_at: to_datetime(row["updated_at"]),
post_id: post_id,
}
end
reaction_users.close
end
def import_reactions
unless defined?(::DiscourseReactions)
puts "", "Skipping reactions import, because the Discourse Reactions plugin is not installed."
return
end
puts "", "Importing reactions..."
reactions = query(<<~SQL)
SELECT *
FROM discourse_reactions_reactions
ORDER BY post_id
SQL
reaction_type_id = DiscourseReactions::Reaction.reaction_types["emoji"]
existing_reactions = DiscourseReactions::Reaction.pluck(:post_id, :reaction_value).to_set
create_reactions(reactions) do |row|
next if row["id"].blank?
post_id = post_id_from_imported_id(row["post_id"])
next if post_id.blank? || row["reaction_value"].blank?
next unless existing_reactions.add?([post_id, row["reaction_value"]])
{
id: row["id"],
post_id: post_id,
reaction_type: reaction_type_id,
reaction_value: row["reaction_value"],
reaction_users_count: row["reaction_users_count"],
created_at: to_datetime(row["created_at"]),
updated_at: to_datetime(row["updated_at"]),
}
end
reactions.close
end
def calculate_external_url(row)
external_url = row["external_url"].dup
placeholders = row["external_url_placeholders"]&.then { |json| JSON.parse(json) }