mirror of
https://github.com/glitch-soc/mastodon.git
synced 2026-03-29 03:00:33 +02:00
Federate Add when item is added to Collection (#37823)
This commit is contained in:
26
app/serializers/activitypub/add_featured_item_serializer.rb
Normal file
26
app/serializers/activitypub/add_featured_item_serializer.rb
Normal 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
|
||||
@@ -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)
|
||||
|
||||
17
app/serializers/activitypub/featured_item_serializer.rb
Normal file
17
app/serializers/activitypub/featured_item_serializer.rb
Normal 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
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user