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.

43 lines
1.2 KiB

  1. # frozen_string_literal: true
  2. class ActivityPub::CollectionSerializer < ActivityPub::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. attribute :id, if: -> { object.id.present? }
  9. attribute :type
  10. attribute :total_items, if: -> { object.size.present? }
  11. attribute :next, if: -> { object.next.present? }
  12. attribute :prev, if: -> { object.prev.present? }
  13. attribute :part_of, if: -> { object.part_of.present? }
  14. has_one :first, if: -> { object.first.present? }
  15. has_one :last, if: -> { object.last.present? }
  16. has_many :items, key: :items, if: -> { (!object.items.nil? || page?) && !ordered? }
  17. has_many :items, key: :ordered_items, if: -> { (!object.items.nil? || page?) && ordered? }
  18. def type
  19. if page?
  20. ordered? ? 'OrderedCollectionPage' : 'CollectionPage'
  21. else
  22. ordered? ? 'OrderedCollection' : 'Collection'
  23. end
  24. end
  25. def total_items
  26. object.size
  27. end
  28. private
  29. def ordered?
  30. object.type == :ordered
  31. end
  32. def page?
  33. object.part_of.present? || object.page.present?
  34. end
  35. end