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.

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