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.

98 lines
2.9 KiB

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.("#{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. case verb(xml)
  20. when :follow
  21. follow!(account, target_account)
  22. when :unfollow
  23. unfollow!(account, target_account)
  24. when :favorite
  25. favourite!(xml, account)
  26. when :post
  27. add_post!(body, account) if mentions_account?(xml, target_account)
  28. when :share
  29. add_post!(body, account) unless status(xml).nil?
  30. end
  31. end
  32. end
  33. private
  34. def contains_author?(xml)
  35. !(xml.at_xpath('/xmlns:entry/xmlns:author/xmlns:name').nil? || xml.at_xpath('/xmlns:entry/xmlns:author/xmlns:uri').nil?)
  36. end
  37. def mentions_account?(xml, account)
  38. xml.xpath('/xmlns:entry/xmlns:link[@rel="mentioned"]').each { |mention_link| return true if mention_link.attribute('href').value == url_for_target(account) }
  39. false
  40. end
  41. def verb(xml)
  42. xml.at_xpath('//activity:verb').content.gsub('http://activitystrea.ms/schema/1.0/', '').gsub('http://ostatus.org/schema/1.0/', '').to_sym
  43. rescue
  44. :post
  45. end
  46. def follow!(account, target_account)
  47. account.follow!(target_account)
  48. NotificationMailer.follow(target_account, account).deliver_later
  49. end
  50. def unfollow!(account, target_account)
  51. account.unfollow!(target_account)
  52. end
  53. def favourite!(xml, from_account)
  54. current_status = status(xml)
  55. current_status.favourites.where(account: from_account).first_or_create!(account: from_account)
  56. NotificationMailer.favourite(current_status, from_account).deliver_later
  57. end
  58. def add_post!(body, account)
  59. process_feed_service.(body, account)
  60. end
  61. def status(xml)
  62. Status.find(unique_tag_to_local_id(activity_id(xml), 'Status'))
  63. end
  64. def activity_id(xml)
  65. xml.at_xpath('/xmlns:entry/xmlns:id').content
  66. end
  67. def salmon
  68. @salmon ||= OStatus2::Salmon.new
  69. end
  70. def follow_remote_account_service
  71. @follow_remote_account_service ||= FollowRemoteAccountService.new
  72. end
  73. def process_feed_service
  74. @process_feed_service ||= ProcessFeedService.new
  75. end
  76. def update_remote_profile_service
  77. @update_remote_profile_service ||= UpdateRemoteProfileService.new
  78. end
  79. end