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