about summary refs log tree commit diff
path: root/app/services/process_interaction_service.rb
blob: dd9e769569da85b339e12ec1298dd730b95dcc5f (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
30
31
32
33
34
35
36
37
38
39
40
41
42
class ProcessInteractionService
  def call(envelope, target_account)
    body = salmon.unpack(envelope)
    xml  = Nokogiri::XML(body)

    return if !involves_target_account(xml, target_account) || xml.at_xpath('//xmlns:author/xmlns:name').nil? || xml.at_xpath('//xmlns:author/xmlns:uri').nil?

    username = xml.at_xpath('//xmlns:author/xmlns:name').content
    url      = xml.at_xpath('//xmlns:author/xmlns:uri').content
    domain   = Addressable::URI.parse(url).host
    account  = Account.find_by(username: username, domain: domain)

    if account.nil?
      account = follow_remote_account_service.("acct:#{username}@#{domain}")
    end

    if salmon.verify(envelope, account.keypair)
      verb = xml.at_path('//activity:verb').content

      case verb
      when 'http://activitystrea.ms/schema/1.0/follow', 'follow'
        account.follow!(target_account)
      when 'http://activitystrea.ms/schema/1.0/unfollow', 'unfollow'
        account.unfollow!(target_account)
      end
    end
  end

  private

  def involves_target_account(target_account)
    # todo
  end

  def salmon
    OStatus2::Salmon.new
  end

  def follow_remote_account_service
    FollowRemoteAccountService.new
  end
end