collection_serializer.rb 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # frozen_string_literal: true
  2. class ActivityPub::CollectionSerializer < ActiveModel::Serializer
  3. def self.serializer_for(model, options)
  4. return ActivityPub::ActivitySerializer if model.class.name == 'Status'
  5. return ActivityPub::CollectionSerializer if model.class.name == 'ActivityPub::CollectionPresenter'
  6. super
  7. end
  8. attributes :id, :type, :total_items
  9. attribute :next, if: -> { object.next.present? }
  10. attribute :prev, if: -> { object.prev.present? }
  11. attribute :part_of, if: -> { object.part_of.present? }
  12. has_one :first, if: -> { object.first.present? }
  13. has_many :items, key: :items, if: -> { (!object.items.nil? || page?) && !ordered? }
  14. has_many :items, key: :ordered_items, if: -> { (!object.items.nil? || page?) && ordered? }
  15. def type
  16. if page?
  17. ordered? ? 'OrderedCollectionPage' : 'CollectionPage'
  18. else
  19. ordered? ? 'OrderedCollection' : 'Collection'
  20. end
  21. end
  22. def total_items
  23. object.size
  24. end
  25. private
  26. def ordered?
  27. object.type == :ordered
  28. end
  29. def page?
  30. object.part_of.present?
  31. end
  32. end