payloadable.rb 1.1 KB

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