File: activity_serializer.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (40 lines) | stat: -rw-r--r-- 1,468 bytes parent folder | download
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
# frozen_string_literal: true

module ActivityPub
  # Serializer for the `Activity` ActivityStreams model.
  # Reference: https://www.w3.org/TR/activitystreams-core/#activities
  class ActivitySerializer < ObjectSerializer
    MissingActorError = Class.new(StandardError)
    MissingObjectError = Class.new(StandardError)
    IntransitiveWithObjectError = Class.new(StandardError)

    private

    def validate_response(serialized, opts)
      response = super(serialized, opts)

      unless response[:actor].present?
        raise MissingActorError, "The serializer does not provide the mandatory 'actor' field."
      end

      if opts[:intransitive] && response[:object].present?
        raise IntransitiveWithObjectError, <<~ERROR
          The serializer does provide both the 'object' field and the :intransitive option.
          Intransitive activities are meant precisely for when no object is available.
          Please remove either of those.
          See https://www.w3.org/TR/activitystreams-vocabulary/#activity-types
        ERROR
      end

      unless opts[:intransitive] || response[:object].present?
        raise MissingObjectError, <<~ERROR
          The serializer does not provide the mandatory 'object' field.
          Pass the :intransitive option to #represent if this is an intransitive activity.
          See https://www.w3.org/TR/activitystreams-vocabulary/#activity-types
        ERROR
      end

      response
    end
  end
end