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.

119 lines
3.6 KiB

  1. class ProcessInteractionService < BaseService
  2. ACTIVITY_NS = 'http://activitystrea.ms/spec/1.0/'.freeze
  3. # Record locally the remote interaction with our user
  4. # @param [String] envelope Salmon envelope
  5. # @param [Account] target_account Account the Salmon was addressed to
  6. def call(envelope, target_account)
  7. body = salmon.unpack(envelope)
  8. xml = Nokogiri::XML(body)
  9. return unless contains_author?(xml)
  10. username = xml.at_xpath('/xmlns:entry/xmlns:author/xmlns:name').content
  11. url = xml.at_xpath('/xmlns:entry/xmlns:author/xmlns:uri').content
  12. domain = Addressable::URI.parse(url).host
  13. account = Account.find_by(username: username, domain: domain)
  14. return if DomainBlock.blocked?(domain)
  15. if account.nil?
  16. account = follow_remote_account_service.call("#{username}@#{domain}")
  17. end
  18. if salmon.verify(envelope, account.keypair)
  19. update_remote_profile_service.call(xml.at_xpath('/xmlns:entry/xmlns:author'), account)
  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. when :delete
  32. delete_post!(xml, account)
  33. end
  34. end
  35. rescue Goldfinger::Error, HTTP::Error, OStatus2::BadSalmonError
  36. nil
  37. end
  38. private
  39. def contains_author?(xml)
  40. !(xml.at_xpath('/xmlns:entry/xmlns:author/xmlns:name').nil? || xml.at_xpath('/xmlns:entry/xmlns:author/xmlns:uri').nil?)
  41. end
  42. def mentions_account?(xml, account)
  43. xml.xpath('/xmlns:entry/xmlns:link[@rel="mentioned"]').each { |mention_link| return true if mention_link.attribute('href').value == TagManager.instance.url_for(account) }
  44. false
  45. end
  46. def verb(xml)
  47. xml.at_xpath('//activity:verb', activity: ACTIVITY_NS).content.gsub('http://activitystrea.ms/schema/1.0/', '').gsub('http://ostatus.org/schema/1.0/', '').to_sym
  48. rescue
  49. :post
  50. end
  51. def follow!(account, target_account)
  52. account.follow!(target_account)
  53. NotificationMailer.follow(target_account, account).deliver_later unless target_account.blocking?(account)
  54. end
  55. def unfollow!(account, target_account)
  56. account.unfollow!(target_account)
  57. end
  58. def delete_post!(xml, account)
  59. status = Status.find(activity_id(xml))
  60. return if status.nil?
  61. if account.id == status.account_id
  62. remove_status_service.call(status)
  63. end
  64. end
  65. def favourite!(xml, from_account)
  66. current_status = status(xml)
  67. current_status.favourites.where(account: from_account).first_or_create!(account: from_account)
  68. NotificationMailer.favourite(current_status, from_account).deliver_later unless current_status.account.blocking?(from_account)
  69. end
  70. def add_post!(body, account)
  71. process_feed_service.call(body, account)
  72. end
  73. def status(xml)
  74. Status.find(TagManager.instance.unique_tag_to_local_id(activity_id(xml), 'Status'))
  75. end
  76. def activity_id(xml)
  77. xml.at_xpath('//activity:object', activity: ACTIVITY_NS).at_xpath('./xmlns:id').content
  78. end
  79. def salmon
  80. @salmon ||= OStatus2::Salmon.new
  81. end
  82. def follow_remote_account_service
  83. @follow_remote_account_service ||= FollowRemoteAccountService.new
  84. end
  85. def process_feed_service
  86. @process_feed_service ||= ProcessFeedService.new
  87. end
  88. def update_remote_profile_service
  89. @update_remote_profile_service ||= UpdateRemoteProfileService.new
  90. end
  91. def remove_status_service
  92. @remove_status_service ||= RemoveStatusService.new
  93. end
  94. end