Class: Panda::Core::AttachAvatarService

Inherits:
Services::BaseService show all
Defined in:
app/services/panda/core/attach_avatar_service.rb

Constant Summary collapse

MAX_FILE_SIZE =
5.megabytes
MAX_DIMENSION =

Max width/height for optimization

800

Instance Method Summary collapse

Methods inherited from Services::BaseService

call

Constructor Details

#initialize(user:, avatar_url:) ⇒ AttachAvatarService

Returns a new instance of AttachAvatarService.



11
12
13
14
# File 'app/services/panda/core/attach_avatar_service.rb', line 11

def initialize(user:, avatar_url:)
  @user = user
  @avatar_url = avatar_url
end

Instance Method Details

#callObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/services/panda/core/attach_avatar_service.rb', line 16

def call
  return success if @avatar_url.blank?

  avatar_attached = begin
    @user.avatar.attached?
  rescue
    false
  end
  return success if @avatar_url == @user.oauth_avatar_url && avatar_attached

  begin
    download_and_attach_avatar
    # Record which OAuth URL was downloaded and clear image_url since
    # we now have a local ActiveStorage copy
    @user.update_columns(oauth_avatar_url: @avatar_url, image_url: nil)
    success(avatar_attached: true)
  rescue => e
    Rails.logger.error("Failed to attach avatar for user #{@user.id}: #{e.message}")
    failure(["Failed to attach avatar: #{e.message}"])
  end
end