FIX: Export all category names in user archives (#8790)

This commit is contained in:
Dan Ungureanu 2020-01-28 12:52:07 +02:00 committed by GitHub
parent 6e8a1ac09a
commit 5060811464
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 15 deletions

View File

@ -8,7 +8,7 @@ module Jobs
sidekiq_options retry: false
HEADER_ATTRS_FOR ||= HashWithIndifferentAccess.new(
user_archive: ['topic_title', 'category', 'sub_category', 'is_pm', 'post', 'like_count', 'reply_count', 'url', 'created_at'],
user_archive: ['topic_title', 'categories', 'is_pm', 'post', 'like_count', 'reply_count', 'url', 'created_at'],
user_list: ['id', 'name', 'username', 'email', 'title', 'created_at', 'last_seen_at', 'last_posted_at', 'last_emailed_at', 'trust_level', 'approved', 'suspended_at', 'suspended_till', 'silenced_till', 'active', 'admin', 'moderator', 'ip_address', 'staged', 'secondary_emails'],
user_stats: ['topics_entered', 'posts_read_count', 'time_read', 'topic_count', 'post_count', 'likes_given', 'likes_received'],
user_profile: ['location', 'website', 'views'],
@ -308,25 +308,22 @@ module Jobs
user_archive = user_archive.as_json
topic_data = Topic.with_deleted.find_by(id: user_archive['topic_id']) if topic_data.nil?
return user_archive_array if topic_data.nil?
category = topic_data.category
sub_category_name = "-"
if category
category_name = category.name
if category.parent_category_id.present?
# sub category
if parent_category = Category.find_by(id: category.parent_category_id)
category_name = parent_category.name
sub_category_name = category.name
end
all_categories = Category.all.to_h { |category| [category.id, category] }
categories = "-"
if topic_data.category_id && category = all_categories[topic_data.category_id]
categories = [category.name]
while category.parent_category_id && category = all_categories[category.parent_category_id]
categories << category.name
end
else
# PM
category_name = "-"
categories = categories.reverse.join("|")
end
is_pm = topic_data.archetype == "private_message" ? I18n.t("csv_export.boolean_yes") : I18n.t("csv_export.boolean_no")
url = "#{Discourse.base_url}/t/#{topic_data.slug}/#{topic_data.id}/#{user_archive['post_number']}"
topic_hash = { "post" => user_archive['raw'], "topic_title" => topic_data.title, "category" => category_name, "sub_category" => sub_category_name, "is_pm" => is_pm, "url" => url }
topic_hash = { "post" => user_archive['raw'], "topic_title" => topic_data.title, "categories" => categories, "is_pm" => is_pm, "url" => url }
user_archive.merge!(topic_hash)
HEADER_ATTRS_FOR['user_archive'].each do |attr|

View File

@ -42,6 +42,37 @@ describe Jobs::ExportCsvFile do
end
end
context '#user_archive_export' do
let(:user) { Fabricate(:user) }
let(:category) { Fabricate(:category_with_definition) }
let(:subcategory) { Fabricate(:category_with_definition, parent_category_id: category.id) }
let(:subsubcategory) { Fabricate(:category_with_definition, parent_category_id: subcategory.id) }
it 'works with sub-sub-categories' do
SiteSetting.max_category_nesting = 3
topic = Fabricate(:topic, category: subsubcategory)
post = Fabricate(:post, topic: topic, user: user)
exporter = Jobs::ExportCsvFile.new
exporter.instance_variable_set(:@current_user, User.find_by(id: user.id))
rows = []
exporter.user_archive_export { |row| rows << row }
expect(rows.length).to eq(1)
first_row = Jobs::ExportCsvFile::HEADER_ATTRS_FOR['user_archive'].zip(rows[0]).to_h
expect(first_row["topic_title"]).to eq(topic.title)
expect(first_row["categories"]).to eq("#{category.name}|#{subcategory.name}|#{subsubcategory.name}")
expect(first_row["is_pm"]).to eq(I18n.t("csv_export.boolean_no"))
expect(first_row["post"]).to eq(post.raw)
expect(first_row["like_count"]).to eq(0)
expect(first_row["reply_count"]).to eq(0)
end
end
context '.report_export' do
let(:user) { Fabricate(:admin) }