闭社主体 forked from https://github.com/tootsuite/mastodon
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.

50 lines
1.3 KiB

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