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.

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