Class: UpdateMetadataFromLinkJob

Inherits:
ApplicationJob show all
Defined in:
app/jobs/update_metadata_from_link_job.rb

Instance Method Summary collapse

Instance Method Details

#import_files(data, model) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'app/jobs/update_metadata_from_link_job.rb', line 33

def import_files(data, model)
  data.dig(:file_urls)&.each do
    model.create_or_update_file_from_url(url: it[:url], filename: it[:filename])
  rescue ActiveRecord::RecordInvalid
    Rails.logger.info("Couldn't add file #{it[:url]} to model #{model.to_param}")
  end
  # Select preview file
  model.update!(preview_file: model.model_files.find_by(filename: data[:preview_filename])) if data[:preview_filename].present?
end

#perform(link:, organize: false, apply_permissions_after_sync: nil) ⇒ 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
# File 'app/jobs/update_metadata_from_link_job.rb', line 5

def perform(link:, organize: false, apply_permissions_after_sync: nil)
  return unless (data = link.deserializer&.deserialize)
  linkable = link.linkable
  # Preserve existing tags
  data[:tag_list]&.concat(linkable.tag_list)&.uniq
  # Update object
  linkable.update! data.except(:file_urls, :preview_filename, :models)
  # Import files for models
  if linkable.is_a? Model
    linkable.organize! if organize
    import_files(data, linkable)
  end
  # Import models for collections
  if linkable.is_a?(Collection) && data[:models]
    data[:models].each { CreateObjectFromUrlJob.perform_later(url: it, collection_id: linkable.id) }
  end
  # Apply default permissions
  if apply_permissions_after_sync
    linkable.update(permission_preset: apply_permissions_after_sync)
  end
  # If successful, set sync time
  link.update!(synced_at: Time.now.utc)
  Problem.create_or_clear(link, :http_error, false)
rescue Faraday::Error => err
  Rails.logger.error err
  Problem.create_or_clear(link, :http_error, true, note: err.to_s)
end