Fix adding items without a position (#38368)

This commit is contained in:
David Roetzel 2026-03-24 15:43:50 +01:00 committed by GitHub
parent 4559e4ed1a
commit 7788281759
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 8 deletions

View File

@ -62,7 +62,7 @@ class CollectionItem < ApplicationRecord
private private
def set_position def set_position
return if position_changed? return if position.present? && position_changed?
self.position = self.class.where(collection_id:).maximum(:position).to_i + 1 self.position = self.class.where(collection_id:).maximum(:position).to_i + 1
end end

View File

@ -62,6 +62,12 @@ RSpec.describe CollectionItem do
expect(custom_item.position).to eq 7 expect(custom_item.position).to eq 7
end end
it 'automatically sets the position if excplicitly set to `nil`' do
item = collection.collection_items.create!(account:, position: nil)
expect(item.position).to eq 1
end
it 'automatically sets `activity_uri` when account is remote' do it 'automatically sets `activity_uri` when account is remote' do
item = collection.collection_items.create(account: Fabricate(:remote_account)) item = collection.collection_items.create(account: Fabricate(:remote_account))

View File

@ -47,6 +47,7 @@ RSpec.describe ActivityPub::ProcessFeaturedItemService do
it_behaves_like 'non-matching URIs' it_behaves_like 'non-matching URIs'
context 'when item does not yet exist' do context 'when item does not yet exist' do
context 'when a position is given' do
it 'creates and verifies the item' do it 'creates and verifies the item' do
expect { subject.call(collection, object, position:) }.to change(collection.collection_items, :count).by(1) expect { subject.call(collection, object, position:) }.to change(collection.collection_items, :count).by(1)
@ -59,6 +60,16 @@ RSpec.describe ActivityPub::ProcessFeaturedItemService do
end end
end end
context 'when no position is given' do
it 'creates the item' do
expect { subject.call(collection, object) }.to change(collection.collection_items, :count).by(1)
new_item = collection.collection_items.last
expect(new_item.position).to eq 1
end
end
end
context 'when item exists at a different position' do context 'when item exists at a different position' do
let!(:collection_item) do let!(:collection_item) do
Fabricate(:collection_item, collection:, uri: featured_item_json['id'], position: 2) Fabricate(:collection_item, collection:, uri: featured_item_json['id'], position: 2)