Class: Jobs::CleanUpUserExportTopics

Inherits:
Onceoff show all
Defined in:
app/jobs/onceoff/clean_up_user_export_topics.rb

Instance Method Summary collapse

Methods inherited from Onceoff

enqueue_all, #execute, name_for, #running_key_name

Methods inherited from Base

acquire_cluster_concurrency_lock!, clear_cluster_concurrency_lock!, cluster_concurrency, cluster_concurrency_redis_key, delayed_perform, #error_context, #execute, get_cluster_concurrency, #last_db_duration, #log, #perform, #perform_immediately

Instance Method Details

#execute_onceoff(args) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/jobs/onceoff/clean_up_user_export_topics.rb', line 5

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)
    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