Federate deletion of collection item (#37837)
This commit is contained in:
@@ -27,7 +27,7 @@ class Api::V1Alpha::CollectionItemsController < Api::BaseController
|
||||
def destroy
|
||||
authorize @collection, :update?
|
||||
|
||||
@collection_item.destroy
|
||||
DeleteCollectionItemService.new.call(@collection_item)
|
||||
|
||||
head 200
|
||||
end
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class ActivityPub::RemoveFeaturedItemSerializer < ActivityPub::Serializer
|
||||
include RoutingHelper
|
||||
|
||||
attributes :type, :actor, :target
|
||||
has_one :object, serializer: ActivityPub::FeaturedItemSerializer
|
||||
|
||||
def type
|
||||
'Remove'
|
||||
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
|
||||
21
app/services/delete_collection_item_service.rb
Normal file
21
app/services/delete_collection_item_service.rb
Normal file
@@ -0,0 +1,21 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class DeleteCollectionItemService
|
||||
def call(collection_item)
|
||||
@collection_item = collection_item
|
||||
@collection = collection_item.collection
|
||||
@collection_item.destroy!
|
||||
|
||||
distribute_remove_activity if Mastodon::Feature.collections_federation_enabled?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def distribute_remove_activity
|
||||
ActivityPub::AccountRawDistributionWorker.perform_async(activity_json, @collection.account.id)
|
||||
end
|
||||
|
||||
def activity_json
|
||||
ActiveModelSerializers::SerializableResource.new(@collection_item, serializer: ActivityPub::RemoveFeaturedItemSerializer, adapter: ActivityPub::Adapter).to_json
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,27 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe ActivityPub::RemoveFeaturedItemSerializer 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' => 'Remove',
|
||||
'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
|
||||
22
spec/services/delete_collection_item_service_spec.rb
Normal file
22
spec/services/delete_collection_item_service_spec.rb
Normal file
@@ -0,0 +1,22 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe DeleteCollectionItemService do
|
||||
subject { described_class.new }
|
||||
|
||||
let(:collection_item) { Fabricate(:collection_item) }
|
||||
let(:collection) { collection_item.collection }
|
||||
|
||||
describe '#call' do
|
||||
it 'destroys the collection' do
|
||||
expect { subject.call(collection_item) }.to change(collection.collection_items, :count).by(-1)
|
||||
end
|
||||
|
||||
it 'federates a `Remove` activity', feature: :collections_federation do
|
||||
subject.call(collection_item)
|
||||
|
||||
expect(ActivityPub::AccountRawDistributionWorker).to have_enqueued_sidekiq_job
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user