Add support for FASP data sharing (#34415)

This commit is contained in:
David Roetzel 2025-05-16 14:24:02 +02:00 committed by GitHub
parent 3ea1f074ab
commit a5a2c6dc7e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
38 changed files with 1140 additions and 1 deletions

View file

@ -15,4 +15,5 @@ Fabricator(:account) do
user { |attrs| attrs[:domain].nil? ? Fabricate.build(:user, account: nil) : nil }
uri { |attrs| attrs[:domain].nil? ? '' : "https://#{attrs[:domain]}/users/#{attrs[:username]}" }
discoverable true
indexable true
end

View file

@ -0,0 +1,9 @@
# frozen_string_literal: true
Fabricator(:fasp_backfill_request, from: 'Fasp::BackfillRequest') do
category 'content'
max_count 10
cursor nil
fulfilled false
fasp_provider
end

View file

@ -0,0 +1,8 @@
# frozen_string_literal: true
Fabricator(:fasp_subscription, from: 'Fasp::Subscription') do
category 'content'
subscription_type 'lifecycle'
max_batch_size 10
fasp_provider
end

View 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

View 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

View 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

View 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

View 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

View file

@ -0,0 +1,41 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'Api::Fasp::DataSharing::V0::BackfillRequests', feature: :fasp do
include ProviderRequestHelper
describe 'POST /api/fasp/data_sharing/v0/backfill_requests' do
let(:provider) { Fabricate(:fasp_provider) }
context 'with valid parameters' do
it 'creates a new backfill request' do
params = { category: 'content', maxCount: 10 }
headers = request_authentication_headers(provider,
url: api_fasp_data_sharing_v0_backfill_requests_url,
method: :post,
body: params)
expect do
post api_fasp_data_sharing_v0_backfill_requests_path, headers:, params:, as: :json
end.to change(Fasp::BackfillRequest, :count).by(1)
expect(response).to have_http_status(201)
end
end
context 'with invalid parameters' do
it 'does not create a backfill request' do
params = { category: 'unknown', maxCount: 10 }
headers = request_authentication_headers(provider,
url: api_fasp_data_sharing_v0_backfill_requests_url,
method: :post,
body: params)
expect do
post api_fasp_data_sharing_v0_backfill_requests_path, headers:, params:, as: :json
end.to_not change(Fasp::BackfillRequest, :count)
expect(response).to have_http_status(422)
end
end
end
end

View file

@ -0,0 +1,22 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'Api::Fasp::DataSharing::V0::Continuations', feature: :fasp do
include ProviderRequestHelper
describe 'POST /api/fasp/data_sharing/v0/backfill_requests/:id/continuations' do
let(:backfill_request) { Fabricate(:fasp_backfill_request) }
let(:provider) { backfill_request.fasp_provider }
it 'queues a job to continue the given backfill request' do
headers = request_authentication_headers(provider,
url: api_fasp_data_sharing_v0_backfill_request_continuation_url(backfill_request),
method: :post)
post api_fasp_data_sharing_v0_backfill_request_continuation_path(backfill_request), headers:, as: :json
expect(response).to have_http_status(204)
expect(Fasp::BackfillWorker).to have_enqueued_sidekiq_job(backfill_request.id)
end
end
end

View file

@ -0,0 +1,57 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe 'Api::Fasp::DataSharing::V0::EventSubscriptions', feature: :fasp do
include ProviderRequestHelper
describe 'POST /api/fasp/data_sharing/v0/event_subscriptions' do
let(:provider) { Fabricate(:fasp_provider) }
context 'with valid parameters' do
it 'creates a new subscription' do
params = { category: 'content', subscriptionType: 'lifecycle', maxBatchSize: 10 }
headers = request_authentication_headers(provider,
url: api_fasp_data_sharing_v0_event_subscriptions_url,
method: :post,
body: params)
expect do
post api_fasp_data_sharing_v0_event_subscriptions_path, headers:, params:, as: :json
end.to change(Fasp::Subscription, :count).by(1)
expect(response).to have_http_status(201)
end
end
context 'with invalid parameters' do
it 'does not create a subscription' do
params = { category: 'unknown' }
headers = request_authentication_headers(provider,
url: api_fasp_data_sharing_v0_event_subscriptions_url,
method: :post,
body: params)
expect do
post api_fasp_data_sharing_v0_event_subscriptions_path, headers:, params:, as: :json
end.to_not change(Fasp::Subscription, :count)
expect(response).to have_http_status(422)
end
end
end
describe 'DELETE /api/fasp/data_sharing/v0/event_subscriptions/:id' do
let(:subscription) { Fabricate(:fasp_subscription) }
let(:provider) { subscription.fasp_provider }
it 'deletes the subscription' do
headers = request_authentication_headers(provider,
url: api_fasp_data_sharing_v0_event_subscription_url(subscription),
method: :delete)
expect do
delete api_fasp_data_sharing_v0_event_subscription_path(subscription), headers:, as: :json
end.to change(Fasp::Subscription, :count).by(-1)
expect(response).to have_http_status(204)
end
end
end

View file

@ -0,0 +1,34 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Fasp::AnnounceAccountLifecycleEventWorker do
include ProviderRequestHelper
let(:account_uri) { 'https://masto.example.com/accounts/1' }
let(:subscription) do
Fabricate(:fasp_subscription, category: 'account')
end
let(:provider) { subscription.fasp_provider }
let!(:stubbed_request) do
stub_provider_request(provider,
method: :post,
path: '/data_sharing/v0/announcements',
response_body: {
source: {
subscription: {
id: subscription.id.to_s,
},
},
category: 'account',
eventType: 'new',
objectUris: [account_uri],
})
end
it 'sends the account uri to subscribed providers' do
described_class.new.perform(account_uri, 'new')
expect(stubbed_request).to have_been_made
end
end

View file

@ -0,0 +1,34 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Fasp::AnnounceContentLifecycleEventWorker do
include ProviderRequestHelper
let(:status_uri) { 'https://masto.example.com/status/1' }
let(:subscription) do
Fabricate(:fasp_subscription)
end
let(:provider) { subscription.fasp_provider }
let!(:stubbed_request) do
stub_provider_request(provider,
method: :post,
path: '/data_sharing/v0/announcements',
response_body: {
source: {
subscription: {
id: subscription.id.to_s,
},
},
category: 'content',
eventType: 'new',
objectUris: [status_uri],
})
end
it 'sends the status uri to subscribed providers' do
described_class.new.perform(status_uri, 'new')
expect(stubbed_request).to have_been_made
end
end

View file

@ -0,0 +1,52 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Fasp::AnnounceTrendWorker do
include ProviderRequestHelper
let(:status) { Fabricate(:status) }
let(:subscription) do
Fabricate(:fasp_subscription,
category: 'content',
subscription_type: 'trends',
threshold_timeframe: 15,
threshold_likes: 2)
end
let(:provider) { subscription.fasp_provider }
let!(:stubbed_request) do
stub_provider_request(provider,
method: :post,
path: '/data_sharing/v0/announcements',
response_body: {
source: {
subscription: {
id: subscription.id.to_s,
},
},
category: 'content',
eventType: 'trending',
objectUris: [status.uri],
})
end
context 'when the configured threshold is met' do
before do
Fabricate.times(2, :favourite, status:)
end
it 'sends the account uri to subscribed providers' do
described_class.new.perform(status.id, 'favourite')
expect(stubbed_request).to have_been_made
end
end
context 'when the configured threshold is not met' do
it 'does not notify any provider' do
described_class.new.perform(status.id, 'favourite')
expect(stubbed_request).to_not have_been_made
end
end
end

View file

@ -0,0 +1,32 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Fasp::BackfillWorker do
include ProviderRequestHelper
let(:backfill_request) { Fabricate(:fasp_backfill_request) }
let(:provider) { backfill_request.fasp_provider }
let(:status) { Fabricate(:status) }
let!(:stubbed_request) do
stub_provider_request(provider,
method: :post,
path: '/data_sharing/v0/announcements',
response_body: {
source: {
backfillRequest: {
id: backfill_request.id.to_s,
},
},
category: 'content',
objectUris: [status.uri],
moreObjectsAvailable: false,
})
end
it 'sends status uri to provider that requested backfill' do
described_class.new.perform(backfill_request.id)
expect(stubbed_request).to have_been_made
end
end