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