Skip to content

Commit

Permalink
Add RedownloadAvatarWorker and RedownloadHeaderWorker
Browse files Browse the repository at this point in the history
  • Loading branch information
noellabo committed May 11, 2021
1 parent 35e9318 commit 43a0cfa
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/models/concerns/account_avatar.rb
Expand Up @@ -21,7 +21,7 @@ def avatar_styles(file)
has_attached_file :avatar, styles: ->(f) { avatar_styles(f) }, convert_options: { all: '-strip' }, processors: [:lazy_thumbnail]
validates_attachment_content_type :avatar, content_type: IMAGE_MIME_TYPES
validates_attachment_size :avatar, less_than: LIMIT
remotable_attachment :avatar, LIMIT
remotable_attachment :avatar, LIMIT, suppress_errors: false
end

def avatar_original_url
Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/account_header.rb
Expand Up @@ -22,7 +22,7 @@ def header_styles(file)
has_attached_file :header, styles: ->(f) { header_styles(f) }, convert_options: { all: '-strip' }, processors: [:lazy_thumbnail]
validates_attachment_content_type :header, content_type: IMAGE_MIME_TYPES
validates_attachment_size :header, less_than: LIMIT
remotable_attachment :header, LIMIT
remotable_attachment :header, LIMIT, suppress_errors: false
end

def header_original_url
Expand Down
12 changes: 10 additions & 2 deletions app/services/activitypub/process_account_service.rb
Expand Up @@ -106,8 +106,16 @@ def set_fetchable_key!
end

def set_fetchable_attributes!
@account.avatar_remote_url = image_url('icon') || '' unless skip_download?
@account.header_remote_url = image_url('image') || '' unless skip_download?
begin
@account.avatar_remote_url = image_url('icon') || '' unless skip_download?
rescue Mastodon::UnexpectedResponseError, HTTP::TimeoutError, HTTP::ConnectionError, OpenSSL::SSL::SSLError
RedownloadAvatarWorker.perform_in(rand(30..600).seconds, @account.id)
end
begin
@account.header_remote_url = image_url('image') || '' unless skip_download?
rescue Mastodon::UnexpectedResponseError, HTTP::TimeoutError, HTTP::ConnectionError, OpenSSL::SSL::SSLError
RedownloadHeaderWorker.perform_in(rand(30..600).seconds, @account.id)
end
@account.statuses_count = outbox_total_items if outbox_total_items.present?
@account.following_count = following_total_items if following_total_items.present?
@account.followers_count = followers_total_items if followers_total_items.present?
Expand Down
29 changes: 29 additions & 0 deletions app/workers/redownload_avatar_worker.rb
@@ -0,0 +1,29 @@
# frozen_string_literal: true

class RedownloadAvatarWorker
include Sidekiq::Worker
include ExponentialBackoff
include JsonLdHelper

sidekiq_options queue: 'pull', retry: 7

def perform(id)
account = Account.find(id)

return if account.suspended? || DomainBlock.rule_for(account.domain)&.reject_media?
return if account.avatar_remote_url.blank? || account.avatar_file_name.present?

account.reset_avatar!
account.save!
rescue ActiveRecord::RecordNotFound
# Do nothing
rescue Mastodon::UnexpectedResponseError => e
response = e.response

if response_error_unsalvageable?(response)
# Give up
else
raise e
end
end
end
29 changes: 29 additions & 0 deletions app/workers/redownload_header_worker.rb
@@ -0,0 +1,29 @@
# frozen_string_literal: true

class RedownloadHeaderWorker
include Sidekiq::Worker
include ExponentialBackoff
include JsonLdHelper

sidekiq_options queue: 'pull', retry: 7

def perform(id)
account = Account.find(id)

return if account.suspended? || DomainBlock.rule_for(account.domain)&.reject_media?
return if account.header_remote_url.blank? || account.header_file_name.present?

account.reset_header!
account.save!
rescue ActiveRecord::RecordNotFound
# Do nothing
rescue Mastodon::UnexpectedResponseError => e
response = e.response

if response_error_unsalvageable?(response)
# Give up
else
raise e
end
end
end

0 comments on commit 43a0cfa

Please sign in to comment.