payloadable.rb 1.1 KB

123456789101112131415161718192021222324252627282930
  1. # frozen_string_literal: true
  2. module Payloadable
  3. include AuthorizedFetchHelper
  4. # @param [ActiveModelSerializers::Model] record
  5. # @param [ActiveModelSerializers::Serializer] serializer
  6. # @param [Hash] options
  7. # @option options [Account] :signer
  8. # @option options [String] :sign_with
  9. # @option options [Boolean] :always_sign
  10. # @return [Hash]
  11. def serialize_payload(record, serializer, options = {})
  12. signer = options.delete(:signer)
  13. sign_with = options.delete(:sign_with)
  14. always_sign = options.delete(:always_sign)
  15. payload = ActiveModelSerializers::SerializableResource.new(record, options.merge(serializer: serializer, adapter: ActivityPub::Adapter)).as_json
  16. object = record.respond_to?(:virtual_object) ? record.virtual_object : record
  17. if (object.respond_to?(:sign?) && object.sign?) && signer && (always_sign || signing_enabled?)
  18. ActivityPub::LinkedDataSignature.new(payload).sign!(signer, sign_with: sign_with)
  19. else
  20. payload
  21. end
  22. end
  23. def signing_enabled?
  24. !authorized_fetch_mode?
  25. end
  26. end