You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.1 KiB

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