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.

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