about summary refs log tree commit diff
path: root/app/services/send_interaction_service.rb
blob: 05a1e77e39d7c598d3481f2b3f40b8faccc074b0 (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
# frozen_string_literal: true

class SendInteractionService < BaseService
  # Send an Atom representation of an interaction to a remote Salmon endpoint
  # @param [StreamEntry] stream_entry
  # @param [Account] target_account
  def call(stream_entry, target_account)
    envelope = salmon.pack(entry_xml(stream_entry), stream_entry.account.keypair)
    salmon.post(target_account.salmon_url, envelope)
  end

  private

  def entry_xml(stream_entry)
    Nokogiri::XML::Builder.new do |xml|
      entry(xml, true) do
        author(xml) do
          include_author xml, stream_entry.account
        end

        include_entry xml, stream_entry
      end
    end.to_xml
  end

  def salmon
    @salmon ||= OStatus2::Salmon.new
  end
end