mastodon/app/services/create_collection_service.rb
David Roetzel 48c33df8bf
First stab at creating collections via service
This does not yet include federation or validating the added
accounts' discoverability settings.
2025-11-26 12:42:29 +01:00

35 lines
761 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)
@collection.account = account
@collection.local = true
@collection.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