Class: Jobs::UpdateHotlinkedRaw

Inherits:
Base
  • Object
show all
Defined in:
app/jobs/regular/update_hotlinked_raw.rb

Instance Method Summary collapse

Methods inherited from Base

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

Instance Method Details

#execute(args) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/jobs/regular/update_hotlinked_raw.rb', line 7

def execute(args)
  @post_id = args[:post_id]
  raise Discourse::InvalidParameters.new(:post_id) if @post_id.blank?

  post = Post.find_by(id: @post_id)
  return if post.nil?
  return if post.cook_method == Post.cook_methods[:raw_html]
  return if post.topic.nil?

  hotlinked_map = post.post_hotlinked_media.preload(:upload).map { |r| [r.url, r] }.to_h

  raw =
    InlineUploads.replace_hotlinked_image_urls(raw: post.raw) do |match_src|
      normalized_match_src = PostHotlinkedMedia.normalize_src(match_src)
      hotlinked_map[normalized_match_src]&.upload
    end

  if post.raw != raw
    changes = { raw: raw, edit_reason: I18n.t("upload.edit_reason") }
    post.revise(Discourse.system_user, changes, bypass_bump: true, skip_staff_log: true)
  end
end