send_interaction_service.rb 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # frozen_string_literal: true
  2. class SendInteractionService < BaseService
  3. # Send an Atom representation of an interaction to a remote Salmon endpoint
  4. # @param [String] Entry XML
  5. # @param [Account] source_account
  6. # @param [Account] target_account
  7. def call(xml, source_account, target_account)
  8. @xml = xml
  9. @source_account = source_account
  10. @target_account = target_account
  11. return if !target_account.ostatus? || block_notification?
  12. delivery = build_request.perform
  13. raise Mastodon::UnexpectedResponseError, delivery unless delivery.code > 199 && delivery.code < 300
  14. end
  15. private
  16. def build_request
  17. request = Request.new(:post, @target_account.salmon_url, body: envelope)
  18. request.add_headers('Content-Type' => 'application/magic-envelope+xml')
  19. request
  20. end
  21. def envelope
  22. salmon.pack(@xml, @source_account.keypair)
  23. end
  24. def block_notification?
  25. DomainBlock.blocked?(@target_account.domain)
  26. end
  27. def salmon
  28. @salmon ||= OStatus2::Salmon.new
  29. end
  30. end