chore: Ability to Disable Gravatars (#5027)
fixes: #3853 - Introduced DISABLE_GRAVATAR Global Config, which will stop chatwoot from making API requests to gravatar - Cleaned up avatar-related logic and centralized it into the avatarable concern - Added specs for the missing cases - Added migration for existing installations to move the avatar to attachment, rather than making the API that results in 404.
This commit is contained in:
13
app/jobs/avatar/avatar_from_gravatar_job.rb
Normal file
13
app/jobs/avatar/avatar_from_gravatar_job.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
class Avatar::AvatarFromGravatarJob < ApplicationJob
|
||||
queue_as :low
|
||||
|
||||
def perform(avatarable, email)
|
||||
return if GlobalConfigService.load('DISABLE_GRAVATAR', '').present?
|
||||
return if email.blank?
|
||||
return if avatarable.avatar_url.present?
|
||||
|
||||
hash = Digest::MD5.hexdigest(email)
|
||||
gravatar_url = "https://www.gravatar.com/avatar/#{hash}?d=404"
|
||||
Avatar::AvatarFromUrlJob.perform_later(avatarable, gravatar_url)
|
||||
end
|
||||
end
|
||||
15
app/jobs/avatar/avatar_from_url_job.rb
Normal file
15
app/jobs/avatar/avatar_from_url_job.rb
Normal file
@@ -0,0 +1,15 @@
|
||||
class Avatar::AvatarFromUrlJob < ApplicationJob
|
||||
queue_as :default
|
||||
|
||||
def perform(avatarable, avatar_url)
|
||||
return unless avatarable.respond_to?(:avatar)
|
||||
|
||||
avatar_file = Down.download(
|
||||
avatar_url,
|
||||
max_size: 15 * 1024 * 1024
|
||||
)
|
||||
avatarable.avatar.attach(io: avatar_file, filename: avatar_file.original_filename, content_type: avatar_file.content_type)
|
||||
rescue Down::NotFound, Down::Error => e
|
||||
Rails.logger.error "Exception: invalid avatar url #{avatar_url} : #{e.message}"
|
||||
end
|
||||
end
|
||||
@@ -1,15 +0,0 @@
|
||||
class ContactAvatarJob < ApplicationJob
|
||||
queue_as :default
|
||||
|
||||
def perform(contact, avatar_url)
|
||||
avatar_file = Down.download(
|
||||
avatar_url,
|
||||
max_size: 15 * 1024 * 1024
|
||||
)
|
||||
contact.avatar.attach(io: avatar_file, filename: avatar_file.original_filename, content_type: avatar_file.content_type)
|
||||
rescue Down::NotFound
|
||||
contact.avatar.attachment.destroy! if contact.avatar.attached?
|
||||
rescue Down::Error => e
|
||||
Rails.logger.error "Exception: invalid avatar url #{avatar_url} : #{e.message}"
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user