Use the same condition for stale refresh (#39026)
This commit is contained in:
@@ -265,8 +265,14 @@ class Account < ApplicationRecord
|
||||
last_webfingered_at.nil? || last_webfingered_at <= STALE_THRESHOLD.ago
|
||||
end
|
||||
|
||||
def needs_background_refresh?
|
||||
return false if local?
|
||||
|
||||
last_webfingered_at.blank? || last_webfingered_at <= BACKGROUND_REFRESH_INTERVAL.ago
|
||||
end
|
||||
|
||||
def schedule_refresh_if_stale!
|
||||
return unless last_webfingered_at.present? && last_webfingered_at <= BACKGROUND_REFRESH_INTERVAL.ago
|
||||
return unless needs_background_refresh?
|
||||
|
||||
AccountRefreshWorker.perform_in(rand(REFRESH_DEADLINE), id)
|
||||
end
|
||||
|
||||
@@ -7,7 +7,7 @@ class AccountRefreshWorker
|
||||
|
||||
def perform(account_id)
|
||||
account = Account.find_by(id: account_id)
|
||||
return if account.nil? || account.last_webfingered_at > Account::BACKGROUND_REFRESH_INTERVAL.ago
|
||||
return unless account&.needs_background_refresh?
|
||||
|
||||
ResolveAccountService.new.call(account)
|
||||
end
|
||||
|
||||
@@ -840,4 +840,36 @@ RSpec.describe Account do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#needs_background_refresh?' do
|
||||
subject { account.needs_background_refresh? }
|
||||
|
||||
context 'when account is local' do
|
||||
let(:account) { Fabricate(:account) }
|
||||
|
||||
it { is_expected.to be false }
|
||||
end
|
||||
|
||||
context 'when account is remote' do
|
||||
let(:account) { Fabricate(:remote_account, last_webfingered_at:) }
|
||||
|
||||
context 'when account has never been updated or last update is unknown' do
|
||||
let(:last_webfingered_at) { nil }
|
||||
|
||||
it { is_expected.to be true }
|
||||
end
|
||||
|
||||
context 'when account has not been updated for over a week' do
|
||||
let(:last_webfingered_at) { 8.days.ago }
|
||||
|
||||
it { is_expected.to be true }
|
||||
end
|
||||
|
||||
context 'when account has been updated in the last week' do
|
||||
let(:last_webfingered_at) { 4.days.ago }
|
||||
|
||||
it { is_expected.to be false }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -19,7 +19,7 @@ RSpec.describe AccountRefreshWorker do
|
||||
|
||||
context 'when account exists' do
|
||||
context 'when account does not need refreshing' do
|
||||
let(:account) { Fabricate(:account, last_webfingered_at: recent_webfinger_at) }
|
||||
let(:account) { Fabricate(:remote_account, last_webfingered_at: recent_webfinger_at) }
|
||||
|
||||
it 'returns immediately without processing' do
|
||||
worker.perform(account.id)
|
||||
@@ -29,9 +29,9 @@ RSpec.describe AccountRefreshWorker do
|
||||
end
|
||||
|
||||
context 'when account needs refreshing' do
|
||||
let(:account) { Fabricate(:account, last_webfingered_at: outdated_webfinger_at) }
|
||||
let(:account) { Fabricate(:remote_account, last_webfingered_at: outdated_webfinger_at) }
|
||||
|
||||
it 'schedules an account update' do
|
||||
it 'performs an account update' do
|
||||
worker.perform(account.id)
|
||||
|
||||
expect(service).to have_received(:call)
|
||||
|
||||
Reference in New Issue
Block a user