2017-07-14 21:01:39 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-03-27 10:55:23 -04:00
|
|
|
class ActivityPub::CollectionSerializer < ActivityPub::Serializer
|
2017-07-14 21:01:39 -04:00
|
|
|
def self.serializer_for(model, options)
|
2020-06-02 13:24:53 -04:00
|
|
|
case model.class.name
|
|
|
|
when 'Status'
|
|
|
|
ActivityPub::NoteSerializer
|
|
|
|
when 'Device'
|
|
|
|
ActivityPub::DeviceSerializer
|
|
|
|
when 'ActivityPub::CollectionPresenter'
|
|
|
|
ActivityPub::CollectionSerializer
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
2017-07-14 21:01:39 -04:00
|
|
|
end
|
|
|
|
|
2019-02-28 09:22:21 -05:00
|
|
|
attribute :id, if: -> { object.id.present? }
|
|
|
|
attribute :type
|
2018-05-04 13:19:11 -04:00
|
|
|
attribute :total_items, if: -> { object.size.present? }
|
2017-09-19 10:37:06 -04:00
|
|
|
attribute :next, if: -> { object.next.present? }
|
|
|
|
attribute :prev, if: -> { object.prev.present? }
|
|
|
|
attribute :part_of, if: -> { object.part_of.present? }
|
2017-07-14 21:01:39 -04:00
|
|
|
|
2017-09-19 10:37:06 -04:00
|
|
|
has_one :first, if: -> { object.first.present? }
|
2018-05-04 13:19:11 -04:00
|
|
|
has_one :last, if: -> { object.last.present? }
|
2018-02-21 17:21:32 -05:00
|
|
|
has_many :items, key: :items, if: -> { (!object.items.nil? || page?) && !ordered? }
|
|
|
|
has_many :items, key: :ordered_items, if: -> { (!object.items.nil? || page?) && ordered? }
|
2017-07-14 21:01:39 -04:00
|
|
|
|
|
|
|
def type
|
2017-09-19 10:37:06 -04:00
|
|
|
if page?
|
|
|
|
ordered? ? 'OrderedCollectionPage' : 'CollectionPage'
|
2017-07-14 21:01:39 -04:00
|
|
|
else
|
2017-09-19 10:37:06 -04:00
|
|
|
ordered? ? 'OrderedCollection' : 'Collection'
|
2017-07-14 21:01:39 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def total_items
|
|
|
|
object.size
|
|
|
|
end
|
2017-09-19 10:37:06 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def ordered?
|
|
|
|
object.type == :ordered
|
|
|
|
end
|
|
|
|
|
|
|
|
def page?
|
2019-02-28 09:22:21 -05:00
|
|
|
object.part_of.present? || object.page.present?
|
2017-09-19 10:37:06 -04:00
|
|
|
end
|
2017-07-14 21:01:39 -04:00
|
|
|
end
|