mirror of
https://framagit.org/tykayn/mastodon.git
synced 2023-08-25 08:33:12 +02:00
51 lines
1.3 KiB
Ruby
51 lines
1.3 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class ActivityPub::CollectionSerializer < ActivityPub::Serializer
|
|
def self.serializer_for(model, options)
|
|
case model.class.name
|
|
when 'Status'
|
|
ActivityPub::NoteSerializer
|
|
when 'Device'
|
|
ActivityPub::DeviceSerializer
|
|
when 'ActivityPub::CollectionPresenter'
|
|
ActivityPub::CollectionSerializer
|
|
else
|
|
super
|
|
end
|
|
end
|
|
|
|
attribute :id, if: -> { object.id.present? }
|
|
attribute :type
|
|
attribute :total_items, if: -> { object.size.present? }
|
|
attribute :next, if: -> { object.next.present? }
|
|
attribute :prev, if: -> { object.prev.present? }
|
|
attribute :part_of, if: -> { object.part_of.present? }
|
|
|
|
has_one :first, if: -> { object.first.present? }
|
|
has_one :last, if: -> { object.last.present? }
|
|
has_many :items, key: :items, if: -> { (!object.items.nil? || page?) && !ordered? }
|
|
has_many :items, key: :ordered_items, if: -> { (!object.items.nil? || page?) && ordered? }
|
|
|
|
def type
|
|
if page?
|
|
ordered? ? 'OrderedCollectionPage' : 'CollectionPage'
|
|
else
|
|
ordered? ? 'OrderedCollection' : 'Collection'
|
|
end
|
|
end
|
|
|
|
def total_items
|
|
object.size
|
|
end
|
|
|
|
private
|
|
|
|
def ordered?
|
|
object.type == :ordered
|
|
end
|
|
|
|
def page?
|
|
object.part_of.present? || object.page.present?
|
|
end
|
|
end
|