Class: Jobs::MigrateCustomEmojis

Inherits:
Onceoff show all
Defined in:
app/jobs/onceoff/migrate_custom_emojis.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
# File 'app/jobs/onceoff/migrate_custom_emojis.rb', line 5

def execute_onceoff(args)
  return if Rails.env.test?

  Dir["#{Rails.root}/#{Emoji.base_directory}/*.{png,gif}"].each do |path|
    name = File.basename(path, File.extname(path))

    File.open(path) do |file|
      upload =
        UploadCreator.new(file, File.basename(path), type: "custom_emoji").create_for(
          Discourse.system_user.id,
        )

      if upload.persisted?
        custom_emoji = CustomEmoji.new(name: name, upload: upload)

        if !custom_emoji.save
          warn("Failed to create custom emoji '#{name}': #{custom_emoji.errors.full_messages}")
        end
      else
        warn(
          "Failed to create upload for '#{name}' custom emoji: #{upload.errors.full_messages}",
        )
      end
    end
  end

  Emoji.clear_cache

  Post.where("cooked LIKE ?", "%#{Emoji.base_url}%").find_each { |post| post.rebake! }
end

#warn(message) ⇒ Object



36
37
38
# File 'app/jobs/onceoff/migrate_custom_emojis.rb', line 36

def warn(message)
  Rails.logger.warn(message)
end