discourse/app/jobs/onceoff/clean_up_user_export_topics.rb
Gary Pendergast 7fc8d74f3e
FEATURE: Allow admins to export users (#30918)
The GDPR requires all users to be able to export their data, or request an export of their data. This is fine for active users as we have a data export button on user profiles, but suspended users have no way of accessing the data export function, and the workaround for admins to export data for suspended users involves temporarily unsuspending them, then impersonating the user to export the data as them.

Since suspended users no longer have access to their account, we can safely assume that the export request will be coming via a medium outside of Discourse (eg, email). This change is built with this workflow in mind.

This change adds a new "User exports" section to the admin user page, allowing admins to start a new export, and to download the latest export file.
2025-01-24 08:13:25 +11:00

38 lines
1.1 KiB
Ruby

# frozen_string_literal: true
module Jobs
class CleanUpUserExportTopics < ::Jobs::Onceoff
def execute_onceoff(args)
translated_keys =
I18n
.available_locales
.map do |l|
I18n.with_locale(:"#{l}") do
I18n.t("system_messages.csv_export_succeeded.subject_template")
end
end
.uniq
slugs = []
translated_keys.each { |k| slugs << "%-#{Slug.for(k.gsub("[%{export_title}]", ""))}" }
# "[%{export_title}] 資料匯出已完成" gets converted to "%-topic", do not match that slug.
slugs = slugs.reject { |s| s == "%-topic" }
topics = Topic.with_deleted.where(<<~SQL, slugs, UserExport::DESTROY_CREATED_BEFORE.ago)
slug LIKE ANY(ARRAY[?]) AND
archetype = 'private_message' AND
subtype = 'system_message' AND
posts_count = 1 AND
created_at < ? AND
user_id = -1
SQL
topics.each do |t|
Topic.transaction do
t.posts.first.destroy!
t.destroy!
end
end
end
end
end