Check "featureable" policy on creation of collections (#37254)

This commit is contained in:
David Roetzel 2025-12-15 16:29:28 +01:00 committed by GitHub
parent 807e1254e6
commit a3fa441e0c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 26 additions and 11 deletions

View File

@ -66,6 +66,6 @@ class AccountPolicy < ApplicationPolicy
end end
def feature? def feature?
record.featureable? && !current_account.blocking?(record) && !record.blocking?(current_account) record.featureable? && !current_account.blocking?(record) && !current_account.blocked_by?(record)
end end
end end

View File

@ -2,9 +2,10 @@
class CreateCollectionService class CreateCollectionService
def call(params, account) def call(params, account)
account_ids = params.delete(:account_ids) @account = account
@accounts_to_add = Account.find(params.delete(:account_ids) || [])
@collection = Collection.new(params.merge({ account:, local: true })) @collection = Collection.new(params.merge({ account:, local: true }))
build_items(account_ids) build_items
@collection.save! @collection.save!
@collection @collection
@ -12,13 +13,14 @@ class CreateCollectionService
private private
def build_items(account_ids) def build_items
return if account_ids.blank? return if @accounts_to_add.empty?
account_ids.each do |account_id| @account.preload_relations!(@accounts_to_add.map(&:id))
account = Account.find(account_id) @accounts_to_add.each do |account_to_add|
# TODO: validate preferences raise Mastodon::NotPermittedError, I18n.t('accounts.errors.cannot_be_added_to_collections') unless AccountPolicy.new(@account, account_to_add).feature?
@collection.collection_items.build(account:)
@collection.collection_items.build(account: account_to_add)
end end
end end
end end

View File

@ -30,9 +30,10 @@ RSpec.describe CreateCollectionService do
end end
context 'when given account ids' do context 'when given account ids' do
let(:account_ids) do let(:accounts) do
Fabricate.times(2, :account).map { |a| a.id.to_s } Fabricate.times(2, :account)
end end
let(:account_ids) { accounts.map { |a| a.id.to_s } }
let(:params) do let(:params) do
base_params.merge(account_ids:) base_params.merge(account_ids:)
end end
@ -42,6 +43,18 @@ RSpec.describe CreateCollectionService do
subject.call(params, author) subject.call(params, author)
end.to change(CollectionItem, :count).by(2) end.to change(CollectionItem, :count).by(2)
end end
context 'when one account may not be added' do
before do
accounts.last.update(discoverable: false)
end
it 'raises an error' do
expect do
subject.call(params, author)
end.to raise_error(Mastodon::NotPermittedError)
end
end
end end
context 'when given a tag' do context 'when given a tag' do