about summary refs log tree commit diff
path: root/app/serializers/activitypub/collection_serializer.rb
blob: ea7af543320dca3b74b0e38185e35e534dfa4d27 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# frozen_string_literal: true

class ActivityPub::CollectionSerializer < ActivityPub::Serializer
  class StringSerializer < ActiveModel::Serializer
    # Despite the name, it does not return a hash, but the same can be said of
    # the ActiveModel::Serializer::CollectionSerializer class which handles
    # arrays.
    def serializable_hash(*_args)
      object
    end
  end

  def self.serializer_for(model, options)
    case model.class.name
    when 'Status'
      ActivityPub::NoteSerializer
    when 'Device'
      ActivityPub::DeviceSerializer
    when 'ActivityPub::CollectionPresenter'
      ActivityPub::CollectionSerializer
    when 'String'
      StringSerializer
    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