You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.1 KiB

  1. class ProcessInteractionService
  2. def call(envelope, target_account)
  3. body = salmon.unpack(envelope)
  4. xml = Nokogiri::XML(body)
  5. return if !involves_target_account(xml, target_account) || xml.at_xpath('//author/name').nil? || xml.at_xpath('//author/uri').nil?
  6. username = xml.at_xpath('//author/name').content
  7. url = xml.at_xpath('//author/uri').content
  8. domain = Addressable::URI.parse(url).host
  9. account = Account.find_by(username: username, domain: domain)
  10. if account.nil?
  11. account = follow_remote_account_service.("acct:#{username}@#{domain}")
  12. end
  13. if salmon.verify(envelope, account.keypair)
  14. verb = xml.at_path('//activity:verb').content
  15. case verb
  16. when 'http://activitystrea.ms/schema/1.0/follow', 'follow'
  17. account.follow!(target_account)
  18. when 'http://activitystrea.ms/schema/1.0/unfollow', 'unfollow'
  19. account.unfollow!(target_account)
  20. end
  21. end
  22. end
  23. private
  24. def involves_target_account(target_account)
  25. end
  26. def salmon
  27. OStatus2::Salmon.new
  28. end
  29. def follow_remote_account_service
  30. FollowRemoteAccountService.new
  31. end
  32. end