Federate Add when item is added to Collection (#37823)

This commit is contained in:
David Roetzel
2026-02-11 14:52:29 +01:00
committed by GitHub
parent f99c60a8f3
commit 3e1127d27b
6 changed files with 90 additions and 18 deletions

View File

@@ -0,0 +1,26 @@
# frozen_string_literal: true
class ActivityPub::AddFeaturedItemSerializer < ActivityPub::Serializer
include RoutingHelper
attributes :type, :actor, :target
has_one :object, serializer: ActivityPub::FeaturedItemSerializer
def type
'Add'
end
def actor
ActivityPub::TagManager.instance.uri_for(collection.account)
end
def target
ActivityPub::TagManager.instance.uri_for(collection)
end
private
def collection
@collection ||= object.collection
end
end

View File

@@ -1,22 +1,6 @@
# frozen_string_literal: true
class ActivityPub::FeaturedCollectionSerializer < ActivityPub::Serializer
class FeaturedItemSerializer < ActivityPub::Serializer
attributes :type, :featured_object, :featured_object_type
def type
'FeaturedItem'
end
def featured_object
ActivityPub::TagManager.instance.uri_for(object.account)
end
def featured_object_type
object.account.actor_type || 'Person'
end
end
attributes :id, :type, :total_items, :name, :attributed_to,
:sensitive, :discoverable, :published, :updated
@@ -25,7 +9,7 @@ class ActivityPub::FeaturedCollectionSerializer < ActivityPub::Serializer
has_one :tag, key: :topic, serializer: ActivityPub::NoteSerializer::TagSerializer
has_many :collection_items, key: :ordered_items, serializer: FeaturedItemSerializer
has_many :collection_items, key: :ordered_items, serializer: ActivityPub::FeaturedItemSerializer
def id
ActivityPub::TagManager.instance.uri_for(object)

View File

@@ -0,0 +1,17 @@
# frozen_string_literal: true
class ActivityPub::FeaturedItemSerializer < ActivityPub::Serializer
attributes :type, :featured_object, :featured_object_type
def type
'FeaturedItem'
end
def featured_object
ActivityPub::TagManager.instance.uri_for(object.account)
end
def featured_object_type
object.account.actor_type || 'Person'
end
end

View File

@@ -9,7 +9,11 @@ class AddAccountToCollectionService
raise Mastodon::NotPermittedError, I18n.t('accounts.errors.cannot_be_added_to_collections') unless AccountPolicy.new(@collection.account, @account).feature?
create_collection_item
@collection_item = create_collection_item
distribute_add_activity if @account.local? && Mastodon::Feature.collections_federation_enabled?
@collection_item
end
private
@@ -20,4 +24,12 @@ class AddAccountToCollectionService
state: :accepted
)
end
def distribute_add_activity
ActivityPub::AccountRawDistributionWorker.perform_async(activity_json, @collection.account_id)
end
def activity_json
ActiveModelSerializers::SerializableResource.new(@collection_item, serializer: ActivityPub::AddFeaturedItemSerializer, adapter: ActivityPub::Adapter).to_json
end
end

View File

@@ -0,0 +1,27 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe ActivityPub::AddFeaturedItemSerializer do
subject { serialized_record_json(object, described_class, adapter: ActivityPub::Adapter) }
let(:tag_manager) { ActivityPub::TagManager.instance }
let(:collection) { Fabricate(:collection) }
let(:object) { Fabricate(:collection_item, collection:) }
it 'serializes to the expected json' do
expect(subject).to include({
'type' => 'Add',
'actor' => tag_manager.uri_for(collection.account),
'target' => tag_manager.uri_for(collection),
'object' => a_hash_including({
'type' => 'FeaturedItem',
}),
})
expect(subject).to_not have_key('id')
expect(subject).to_not have_key('published')
expect(subject).to_not have_key('to')
expect(subject).to_not have_key('cc')
end
end

View File

@@ -20,6 +20,12 @@ RSpec.describe AddAccountToCollectionService do
expect(new_item.state).to eq 'accepted'
expect(new_item.account).to eq account
end
it 'federates an `Add` activity', feature: :collections_federation do
subject.call(collection, account)
expect(ActivityPub::AccountRawDistributionWorker).to have_enqueued_sidekiq_job
end
end
context 'when given an account that is not featureable' do