Federate creation of collections (#37618)
This commit is contained in:
parent
aa347708f5
commit
1d4c2c5670
@ -10,11 +10,13 @@ class ActivityPub::AddSerializer < ActivityPub::Serializer
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.serializer_for(model, options)
|
def self.serializer_for(model, options)
|
||||||
case model.class.name
|
case model
|
||||||
when 'Status'
|
when Status
|
||||||
UriSerializer
|
UriSerializer
|
||||||
when 'FeaturedTag'
|
when FeaturedTag
|
||||||
ActivityPub::HashtagSerializer
|
ActivityPub::HashtagSerializer
|
||||||
|
when Collection
|
||||||
|
ActivityPub::FeaturedCollectionSerializer
|
||||||
else
|
else
|
||||||
super
|
super
|
||||||
end
|
end
|
||||||
@ -38,6 +40,16 @@ class ActivityPub::AddSerializer < ActivityPub::Serializer
|
|||||||
end
|
end
|
||||||
|
|
||||||
def target
|
def target
|
||||||
ActivityPub::TagManager.instance.collection_uri_for(object.account, :featured)
|
case object
|
||||||
|
when Status, FeaturedTag
|
||||||
|
# Technically this is not correct, as tags have their own collection.
|
||||||
|
# But sadly we do not store the collection URI for tags anywhere so cannot
|
||||||
|
# handle `Add` activities to that properly (yet). The receiving code for
|
||||||
|
# this currently looks at the type of the contained objects to do the
|
||||||
|
# right thing.
|
||||||
|
ActivityPub::TagManager.instance.collection_uri_for(object.account, :featured)
|
||||||
|
when Collection
|
||||||
|
ap_account_featured_collections_url(object.account_id)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -8,11 +8,18 @@ class CreateCollectionService
|
|||||||
build_items
|
build_items
|
||||||
|
|
||||||
@collection.save!
|
@collection.save!
|
||||||
|
|
||||||
|
distribute_add_activity if Mastodon::Feature.collections_federation_enabled?
|
||||||
|
|
||||||
@collection
|
@collection
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def distribute_add_activity
|
||||||
|
ActivityPub::AccountRawDistributionWorker.perform_async(activity_json, @account.id)
|
||||||
|
end
|
||||||
|
|
||||||
def build_items
|
def build_items
|
||||||
return if @accounts_to_add.empty?
|
return if @accounts_to_add.empty?
|
||||||
|
|
||||||
@ -23,4 +30,8 @@ class CreateCollectionService
|
|||||||
@collection.collection_items.build(account: account_to_add)
|
@collection.collection_items.build(account: account_to_add)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def activity_json
|
||||||
|
ActiveModelSerializers::SerializableResource.new(@collection, serializer: ActivityPub::AddSerializer, adapter: ActivityPub::Adapter).to_json
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -18,10 +18,38 @@ RSpec.describe ActivityPub::AddSerializer do
|
|||||||
it { is_expected.to eq(ActivityPub::HashtagSerializer) }
|
it { is_expected.to eq(ActivityPub::HashtagSerializer) }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'with a Collection model' do
|
||||||
|
let(:model) { Collection.new }
|
||||||
|
|
||||||
|
it { is_expected.to eq(ActivityPub::FeaturedCollectionSerializer) }
|
||||||
|
end
|
||||||
|
|
||||||
context 'with an Array' do
|
context 'with an Array' do
|
||||||
let(:model) { [] }
|
let(:model) { [] }
|
||||||
|
|
||||||
it { is_expected.to eq(ActiveModel::Serializer::CollectionSerializer) }
|
it { is_expected.to eq(ActiveModel::Serializer::CollectionSerializer) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '#target' do
|
||||||
|
subject { described_class.new(object).target }
|
||||||
|
|
||||||
|
context 'when object is a Status' do
|
||||||
|
let(:object) { Fabricate(:status) }
|
||||||
|
|
||||||
|
it { is_expected.to match(%r{/#{object.account_id}/collections/featured$}) }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when object is a FeaturedTag' do
|
||||||
|
let(:object) { Fabricate(:featured_tag) }
|
||||||
|
|
||||||
|
it { is_expected.to match(%r{/#{object.account_id}/collections/featured$}) }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when object is a Collection' do
|
||||||
|
let(:object) { Fabricate(:collection) }
|
||||||
|
|
||||||
|
it { is_expected.to match(%r{/#{object.account_id}/featured_collections$}) }
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -29,6 +29,12 @@ RSpec.describe CreateCollectionService do
|
|||||||
expect(collection).to be_local
|
expect(collection).to be_local
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'federates an `Add` activity', feature: :collections_federation do
|
||||||
|
subject.call(base_params, author)
|
||||||
|
|
||||||
|
expect(ActivityPub::AccountRawDistributionWorker).to have_enqueued_sidekiq_job
|
||||||
|
end
|
||||||
|
|
||||||
context 'when given account ids' do
|
context 'when given account ids' do
|
||||||
let(:accounts) do
|
let(:accounts) do
|
||||||
Fabricate.times(2, :account)
|
Fabricate.times(2, :account)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user