mastodon/app/services/create_collection_service.rb
David Roetzel 82dc11565e
Update app/services/create_collection_service.rb
Co-authored-by: Claire <claire.github-309c@sitedethib.com>
2025-11-27 09:04:35 +01:00

32 lines
715 B
Ruby

# frozen_string_literal: true
class CreateCollectionService
def call(params, account)
tag = params.delete(:tag)
account_ids = params.delete(:account_ids)
@collection = Collection.new(params.merge({ account:, local: true, tag: find_or_create_tag(tag) }))
build_items(account_ids)
@collection.save!
@collection
end
private
def find_or_create_tag(name)
return nil if name.blank?
Tag.find_or_create_by_names(name).first
end
def build_items(account_ids)
return if account_ids.blank?
account_ids.each do |account_id|
account = Account.find(account_id)
# TODO: validate preferences
@collection.collection_items.build(account:)
end
end
end