DEV: refactor cleaning up of user export topics

This commit is contained in:
Arpit Jalan 2019-05-30 10:24:45 +05:30
parent dc43828905
commit f54a865846
2 changed files with 7 additions and 7 deletions

View File

@ -3,11 +3,9 @@
module Jobs
class CleanUpUserExportTopics < Jobs::Onceoff
def execute_onceoff(args)
translated_keys = []
I18n.available_locales.each do |l|
translated_keys << I18n.with_locale(l.to_sym) { I18n.t("system_messages.csv_export_succeeded.subject_template") }
end
translated_keys = translated_keys.uniq
translated_keys = I18n.available_locales.map do |l|
I18n.with_locale(:"#{l}") { I18n.t("system_messages.csv_export_succeeded.subject_template") }
end.uniq
slugs = []
translated_keys.each do |k|
@ -16,7 +14,7 @@ module Jobs
# "[%{export_title}] 資料匯出已完成" gets converted to "%-topic", do not match that slug.
slugs = slugs.reject { |s| s == "%-topic" }
topics = Topic.with_deleted.where(<<~SQL, slugs, 2.days.ago)
topics = Topic.with_deleted.where(<<~SQL, slugs, UserExport::DESTROY_CREATED_BEFORE)
slug LIKE ANY(ARRAY[?]) AND
archetype = 'private_message' AND
subtype = 'system_message' AND

View File

@ -7,6 +7,8 @@ class UserExport < ActiveRecord::Base
around_destroy :ignore_missing_post_uploads
DESTROY_CREATED_BEFORE = 2.days.ago
def ignore_missing_post_uploads
post_ids = upload.post_uploads.pluck(:post_id)
yield
@ -14,7 +16,7 @@ class UserExport < ActiveRecord::Base
end
def self.remove_old_exports
UserExport.where('created_at < ?', 2.days.ago).find_each do |user_export|
UserExport.where('created_at < ?', DESTROY_CREATED_BEFORE).find_each do |user_export|
UserExport.transaction do
begin
Post.where(topic_id: user_export.topic_id).find_each { |p| p.destroy! }