mirror of
https://github.com/yingziwu/mastodon.git
synced 2026-02-19 00:33:17 +00:00
Add support for FASP data sharing (#34415)
This commit is contained in:
parent
3ea1f074ab
commit
a5a2c6dc7e
38 changed files with 1140 additions and 1 deletions
83
spec/models/concerns/account/fasp_concern_spec.rb
Normal file
83
spec/models/concerns/account/fasp_concern_spec.rb
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Account::FaspConcern, feature: :fasp do
|
||||
describe '#create' do
|
||||
let(:discoverable_attributes) do
|
||||
Fabricate.attributes_for(:account).except('user_id')
|
||||
end
|
||||
let(:undiscoverable_attributes) do
|
||||
discoverable_attributes.merge('discoverable' => false)
|
||||
end
|
||||
|
||||
context 'when account is discoverable' do
|
||||
it 'queues a job to notify provider' do
|
||||
Account.create(discoverable_attributes)
|
||||
|
||||
expect(Fasp::AnnounceAccountLifecycleEventWorker).to have_enqueued_sidekiq_job
|
||||
end
|
||||
end
|
||||
|
||||
context 'when account is not discoverable' do
|
||||
it 'does not queue a job' do
|
||||
Account.create(undiscoverable_attributes)
|
||||
|
||||
expect(Fasp::AnnounceAccountLifecycleEventWorker).to_not have_enqueued_sidekiq_job
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#update' do
|
||||
before do
|
||||
# Create account and clear sidekiq queue so we only catch
|
||||
# jobs queued as part of the update
|
||||
account
|
||||
Sidekiq::Worker.clear_all
|
||||
end
|
||||
|
||||
context 'when account is discoverable' do
|
||||
let(:account) { Fabricate(:account, domain: 'example.com') }
|
||||
|
||||
it 'queues a job to notify provider' do
|
||||
expect { account.touch }.to enqueue_sidekiq_job(Fasp::AnnounceAccountLifecycleEventWorker)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when account was discoverable before' do
|
||||
let(:account) { Fabricate(:account, domain: 'example.com') }
|
||||
|
||||
it 'queues a job to notify provider' do
|
||||
expect do
|
||||
account.update(discoverable: false)
|
||||
end.to enqueue_sidekiq_job(Fasp::AnnounceAccountLifecycleEventWorker)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when account has not been discoverable' do
|
||||
let(:account) { Fabricate(:account, domain: 'example.com', discoverable: false) }
|
||||
|
||||
it 'does not queue a job' do
|
||||
expect { account.touch }.to_not enqueue_sidekiq_job(Fasp::AnnounceAccountLifecycleEventWorker)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#destroy' do
|
||||
context 'when account is discoverable' do
|
||||
let(:account) { Fabricate(:account, domain: 'example.com') }
|
||||
|
||||
it 'queues a job to notify provider' do
|
||||
expect { account.destroy }.to enqueue_sidekiq_job(Fasp::AnnounceAccountLifecycleEventWorker)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when account is not discoverable' do
|
||||
let(:account) { Fabricate(:account, domain: 'example.com', discoverable: false) }
|
||||
|
||||
it 'does not queue a job' do
|
||||
expect { account.destroy }.to_not enqueue_sidekiq_job(Fasp::AnnounceAccountLifecycleEventWorker)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
11
spec/models/concerns/favourite/fasp_concern_spec.rb
Normal file
11
spec/models/concerns/favourite/fasp_concern_spec.rb
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Favourite::FaspConcern, feature: :fasp do
|
||||
describe '#create' do
|
||||
it 'queues a job to notify provider' do
|
||||
expect { Fabricate(:favourite) }.to enqueue_sidekiq_job(Fasp::AnnounceTrendWorker)
|
||||
end
|
||||
end
|
||||
end
|
||||
123
spec/models/concerns/status/fasp_concern_spec.rb
Normal file
123
spec/models/concerns/status/fasp_concern_spec.rb
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Status::FaspConcern, feature: :fasp do
|
||||
describe '#create' do
|
||||
context 'when account is indexable' do
|
||||
let(:account) { Fabricate(:account, domain: 'example.com') }
|
||||
|
||||
context 'when status is public' do
|
||||
it 'queues a job to notify provider of new status' do
|
||||
expect do
|
||||
Fabricate(:status, account:)
|
||||
end.to enqueue_sidekiq_job(Fasp::AnnounceContentLifecycleEventWorker)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when status is not public' do
|
||||
it 'does not queue a job' do
|
||||
expect do
|
||||
Fabricate(:status, account:, visibility: :unlisted)
|
||||
end.to_not enqueue_sidekiq_job(Fasp::AnnounceContentLifecycleEventWorker)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when status is in reply to another' do
|
||||
it 'queues a job to notify provider of possible trend' do
|
||||
parent = Fabricate(:status)
|
||||
expect do
|
||||
Fabricate(:status, account:, thread: parent)
|
||||
end.to enqueue_sidekiq_job(Fasp::AnnounceTrendWorker)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when status is a reblog of another' do
|
||||
it 'queues a job to notify provider of possible trend' do
|
||||
original = Fabricate(:status, account:)
|
||||
expect do
|
||||
Fabricate(:status, account:, reblog: original)
|
||||
end.to enqueue_sidekiq_job(Fasp::AnnounceTrendWorker)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when account is not indexable' do
|
||||
let(:account) { Fabricate(:account, indexable: false) }
|
||||
|
||||
it 'does not queue a job' do
|
||||
expect do
|
||||
Fabricate(:status, account:)
|
||||
end.to_not enqueue_sidekiq_job(Fasp::AnnounceContentLifecycleEventWorker)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#update' do
|
||||
before do
|
||||
# Create status and clear sidekiq queues to only catch
|
||||
# jobs queued due to the update
|
||||
status
|
||||
Sidekiq::Worker.clear_all
|
||||
end
|
||||
|
||||
context 'when account is indexable' do
|
||||
let(:account) { Fabricate(:account, domain: 'example.com') }
|
||||
let(:status) { Fabricate(:status, account:, visibility:) }
|
||||
|
||||
context 'when status is public' do
|
||||
let(:visibility) { :public }
|
||||
|
||||
it 'queues a job to notify provider' do
|
||||
expect { status.touch }.to enqueue_sidekiq_job(Fasp::AnnounceContentLifecycleEventWorker)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when status has not been public' do
|
||||
let(:visibility) { :unlisted }
|
||||
|
||||
it 'does not queue a job' do
|
||||
expect do
|
||||
status.touch
|
||||
end.to_not enqueue_sidekiq_job(Fasp::AnnounceContentLifecycleEventWorker)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'when account is not indexable' do
|
||||
let(:account) { Fabricate(:account, domain: 'example.com', indexable: false) }
|
||||
let(:status) { Fabricate(:status, account:) }
|
||||
|
||||
it 'does not queue a job' do
|
||||
expect { status.touch }.to_not enqueue_sidekiq_job(Fasp::AnnounceContentLifecycleEventWorker)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#destroy' do
|
||||
let(:status) { Fabricate(:status, account:) }
|
||||
|
||||
before do
|
||||
# Create status and clear sidekiq queues to only catch
|
||||
# jobs queued due to the update
|
||||
status
|
||||
Sidekiq::Worker.clear_all
|
||||
end
|
||||
|
||||
context 'when account is indexable' do
|
||||
let(:account) { Fabricate(:account, domain: 'example.com') }
|
||||
|
||||
it 'queues a job to notify provider' do
|
||||
expect { status.destroy }.to enqueue_sidekiq_job(Fasp::AnnounceContentLifecycleEventWorker)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when account is not indexable' do
|
||||
let(:account) { Fabricate(:account, domain: 'example.com', indexable: false) }
|
||||
|
||||
it 'does not queue a job' do
|
||||
expect { status.destroy }.to_not enqueue_sidekiq_job(Fasp::AnnounceContentLifecycleEventWorker)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
93
spec/models/fasp/backfill_request_spec.rb
Normal file
93
spec/models/fasp/backfill_request_spec.rb
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Fasp::BackfillRequest do
|
||||
describe '#next_objects' do
|
||||
let(:account) { Fabricate(:account) }
|
||||
let!(:statuses) { Fabricate.times(3, :status, account:).sort_by(&:id) }
|
||||
|
||||
context 'with a new backfill request' do
|
||||
subject { Fabricate(:fasp_backfill_request, max_count: 2) }
|
||||
|
||||
it 'returns the newest two statuses' do
|
||||
expect(subject.next_objects).to eq [statuses[2], statuses[1]]
|
||||
end
|
||||
end
|
||||
|
||||
context 'with cursor set to second newest status' do
|
||||
subject do
|
||||
Fabricate(:fasp_backfill_request, max_count: 2, cursor: statuses[1].id)
|
||||
end
|
||||
|
||||
it 'returns the oldest status' do
|
||||
expect(subject.next_objects).to eq [statuses[0]]
|
||||
end
|
||||
end
|
||||
|
||||
context 'when all statuses are not `indexable`' do
|
||||
subject { Fabricate(:fasp_backfill_request) }
|
||||
|
||||
let(:account) { Fabricate(:account, indexable: false) }
|
||||
|
||||
it 'returns no statuses' do
|
||||
expect(subject.next_objects).to be_empty
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#next_uris' do
|
||||
subject { Fabricate(:fasp_backfill_request) }
|
||||
|
||||
let(:statuses) { Fabricate.times(2, :status) }
|
||||
|
||||
it 'returns uris of the next objects' do
|
||||
uris = statuses.map(&:uri)
|
||||
|
||||
expect(subject.next_uris).to match_array(uris)
|
||||
end
|
||||
end
|
||||
|
||||
describe '#more_objects_available?' do
|
||||
subject { Fabricate(:fasp_backfill_request, max_count: 2) }
|
||||
|
||||
context 'when more objects are available' do
|
||||
before { Fabricate.times(3, :status) }
|
||||
|
||||
it 'returns `true`' do
|
||||
expect(subject.more_objects_available?).to be true
|
||||
end
|
||||
end
|
||||
|
||||
context 'when no more objects are available' do
|
||||
before { Fabricate.times(2, :status) }
|
||||
|
||||
it 'returns `false`' do
|
||||
expect(subject.more_objects_available?).to be false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#advance!' do
|
||||
subject { Fabricate(:fasp_backfill_request, max_count: 2) }
|
||||
|
||||
context 'when more objects are available' do
|
||||
before { Fabricate.times(3, :status) }
|
||||
|
||||
it 'updates `cursor`' do
|
||||
expect { subject.advance! }.to change(subject, :cursor)
|
||||
expect(subject).to be_persisted
|
||||
end
|
||||
end
|
||||
|
||||
context 'when no more objects are available' do
|
||||
before { Fabricate.times(2, :status) }
|
||||
|
||||
it 'sets `fulfilled` to `true`' do
|
||||
expect { subject.advance! }.to change(subject, :fulfilled)
|
||||
.from(false).to(true)
|
||||
expect(subject).to be_persisted
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
33
spec/models/fasp/subscription_spec.rb
Normal file
33
spec/models/fasp/subscription_spec.rb
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Fasp::Subscription do
|
||||
describe '#threshold=' do
|
||||
subject { described_class.new }
|
||||
|
||||
it 'allows setting all threshold values at once' do
|
||||
subject.threshold = {
|
||||
'timeframe' => 30,
|
||||
'shares' => 5,
|
||||
'likes' => 8,
|
||||
'replies' => 7,
|
||||
}
|
||||
|
||||
expect(subject.threshold_timeframe).to eq 30
|
||||
expect(subject.threshold_shares).to eq 5
|
||||
expect(subject.threshold_likes).to eq 8
|
||||
expect(subject.threshold_replies).to eq 7
|
||||
end
|
||||
end
|
||||
|
||||
describe '#timeframe_start' do
|
||||
subject { described_class.new(threshold_timeframe: 45) }
|
||||
|
||||
it 'returns a Time representing the beginning of the timeframe' do
|
||||
travel_to Time.zone.local(2025, 4, 7, 16, 40) do
|
||||
expect(subject.timeframe_start).to eq Time.zone.local(2025, 4, 7, 15, 55)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue