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.

97 lines
2.7 KiB

8 years ago
8 years ago
  1. class ProcessInteractionService < BaseService
  2. # Record locally the remote interaction with our user
  3. # @param [String] envelope Salmon envelope
  4. # @param [Account] target_account Account the Salmon was addressed to
  5. def call(envelope, target_account)
  6. body = salmon.unpack(envelope)
  7. xml = Nokogiri::XML(body)
  8. return unless contains_author?(xml)
  9. username = xml.at_xpath('/xmlns:entry/xmlns:author/xmlns:name').content
  10. url = xml.at_xpath('/xmlns:entry/xmlns:author/xmlns:uri').content
  11. domain = Addressable::URI.parse(url).host
  12. account = Account.find_by(username: username, domain: domain)
  13. if account.nil?
  14. account = follow_remote_account_service.("acct:#{username}@#{domain}", false)
  15. return if account.nil?
  16. end
  17. if salmon.verify(envelope, account.keypair)
  18. update_remote_profile_service.(xml.at_xpath('/xmlns:entry/xmlns:author'), account)
  19. binding.pry
  20. case verb(xml)
  21. when :follow
  22. follow!(account, target_account)
  23. when :unfollow
  24. unfollow!(account, target_account)
  25. when :favorite
  26. favourite!(xml, account)
  27. when :post
  28. add_post!(body, account) if mentions_account?(xml, target_account)
  29. when :share
  30. add_post!(body, account) unless status(xml).nil?
  31. end
  32. end
  33. end
  34. private
  35. def contains_author?(xml)
  36. !(xml.at_xpath('/xmlns:entry/xmlns:author/xmlns:name').nil? || xml.at_xpath('/xmlns:entry/xmlns:author/xmlns:uri').nil?)
  37. end
  38. def mentions_account?(xml, account)
  39. xml.xpath('/xmlns:entry/xmlns:link[@rel="mentioned"]').each { |mention_link| return true if mention_link.attribute('href').value == url_for_target(account) }
  40. false
  41. end
  42. def verb(xml)
  43. xml.at_xpath('//activity:verb').content.gsub('http://activitystrea.ms/schema/1.0/', '').gsub('http://ostatus.org/schema/1.0/', '').to_sym
  44. rescue
  45. :post
  46. end
  47. def follow!(account, target_account)
  48. account.follow!(target_account)
  49. end
  50. def unfollow!(account, target_account)
  51. account.unfollow!(target_account)
  52. end
  53. def favourite!(xml, from_account)
  54. status(xml).favourites.first_or_create!(account: from_account)
  55. end
  56. def add_post!(body, account)
  57. process_feed_service.(body, account)
  58. end
  59. def status(xml)
  60. Status.find(unique_tag_to_local_id(activity_id(xml), 'Status'))
  61. end
  62. def activity_id(xml)
  63. xml.at_xpath('/xmlns:entry/xmlns:id').content
  64. end
  65. def salmon
  66. @salmon ||= OStatus2::Salmon.new
  67. end
  68. def follow_remote_account_service
  69. @follow_remote_account_service ||= FollowRemoteAccountService.new
  70. end
  71. def process_feed_service
  72. @process_feed_service ||= ProcessFeedService.new
  73. end
  74. def update_remote_profile_service
  75. @update_remote_profile_service ||= UpdateRemoteProfileService.new
  76. end
  77. end