闭社主体 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.

61 lines
1.6 KiB

  1. # frozen_string_literal: true
  2. class ActivityPub::CollectionSerializer < ActivityPub::Serializer
  3. class StringSerializer < ActiveModel::Serializer
  4. # Despite the name, it does not return a hash, but the same can be said of
  5. # the ActiveModel::Serializer::CollectionSerializer class which handles
  6. # arrays.
  7. def serializable_hash(*_args)
  8. object
  9. end
  10. end
  11. def self.serializer_for(model, options)
  12. case model.class.name
  13. when 'Status'
  14. ActivityPub::NoteSerializer
  15. when 'Device'
  16. ActivityPub::DeviceSerializer
  17. when 'ActivityPub::CollectionPresenter'
  18. ActivityPub::CollectionSerializer
  19. when 'String'
  20. StringSerializer
  21. else
  22. super
  23. end
  24. end
  25. attribute :id, if: -> { object.id.present? }
  26. attribute :type
  27. attribute :total_items, if: -> { object.size.present? }
  28. attribute :next, if: -> { object.next.present? }
  29. attribute :prev, if: -> { object.prev.present? }
  30. attribute :part_of, if: -> { object.part_of.present? }
  31. has_one :first, if: -> { object.first.present? }
  32. has_one :last, if: -> { object.last.present? }
  33. has_many :items, key: :items, if: -> { (!object.items.nil? || page?) && !ordered? }
  34. has_many :items, key: :ordered_items, if: -> { (!object.items.nil? || page?) && ordered? }
  35. def type
  36. if page?
  37. ordered? ? 'OrderedCollectionPage' : 'CollectionPage'
  38. else
  39. ordered? ? 'OrderedCollection' : 'Collection'
  40. end
  41. end
  42. def total_items
  43. object.size
  44. end
  45. private
  46. def ordered?
  47. object.type == :ordered
  48. end
  49. def page?
  50. object.part_of.present? || object.page.present?
  51. end
  52. end