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.

159 lines
5.2 KiB

  1. # frozen_string_literal: true
  2. class ProcessInteractionService < BaseService
  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. xml.encoding = 'utf-8'
  10. return unless contains_author?(xml)
  11. username = xml.at_xpath('/xmlns:entry/xmlns:author/xmlns:name', xmlns: TagManager::XMLNS).content
  12. url = xml.at_xpath('/xmlns:entry/xmlns:author/xmlns:uri', xmlns: TagManager::XMLNS).content
  13. domain = Addressable::URI.parse(url).host
  14. account = Account.find_by(username: username, domain: domain)
  15. if account.nil?
  16. account = follow_remote_account_service.call("#{username}@#{domain}")
  17. end
  18. return if account.suspended?
  19. if salmon.verify(envelope, account.keypair)
  20. RemoteProfileUpdateWorker.perform_async(account.id, body.force_encoding('UTF-8'), true)
  21. case verb(xml)
  22. when :follow
  23. follow!(account, target_account) unless target_account.locked? || target_account.blocking?(account)
  24. when :request_friend
  25. follow_request!(account, target_account) unless !target_account.locked? || target_account.blocking?(account)
  26. when :authorize
  27. authorize_follow_request!(account, target_account)
  28. when :reject
  29. reject_follow_request!(account, target_account)
  30. when :unfollow
  31. unfollow!(account, target_account)
  32. when :favorite
  33. favourite!(xml, account)
  34. when :unfavorite
  35. unfavourite!(xml, account)
  36. when :post
  37. add_post!(body, account) if mentions_account?(xml, target_account)
  38. when :share
  39. add_post!(body, account) unless status(xml).nil?
  40. when :delete
  41. delete_post!(xml, account)
  42. when :block
  43. reflect_block!(account, target_account)
  44. when :unblock
  45. reflect_unblock!(account, target_account)
  46. end
  47. end
  48. rescue Goldfinger::Error, HTTP::Error, OStatus2::BadSalmonError
  49. nil
  50. end
  51. private
  52. def contains_author?(xml)
  53. !(xml.at_xpath('/xmlns:entry/xmlns:author/xmlns:name', xmlns: TagManager::XMLNS).nil? || xml.at_xpath('/xmlns:entry/xmlns:author/xmlns:uri', xmlns: TagManager::XMLNS).nil?)
  54. end
  55. def mentions_account?(xml, account)
  56. xml.xpath('/xmlns:entry/xmlns:link[@rel="mentioned"]', xmlns: TagManager::XMLNS).each { |mention_link| return true if [TagManager.instance.uri_for(account), TagManager.instance.url_for(account)].include?(mention_link.attribute('href').value) }
  57. false
  58. end
  59. def verb(xml)
  60. raw = xml.at_xpath('//activity:verb', activity: TagManager::AS_XMLNS).content
  61. TagManager::VERBS.key(raw)
  62. rescue
  63. :post
  64. end
  65. def follow!(account, target_account)
  66. follow = account.follow!(target_account)
  67. NotifyService.new.call(target_account, follow)
  68. end
  69. def follow_request!(account, target_account)
  70. follow_request = FollowRequest.create!(account: account, target_account: target_account)
  71. NotifyService.new.call(target_account, follow_request)
  72. end
  73. def authorize_follow_request!(account, target_account)
  74. follow_request = FollowRequest.find_by(account: target_account, target_account: account)
  75. follow_request&.authorize!
  76. SubscribeService.new.call(account) unless account.subscribed?
  77. end
  78. def reject_follow_request!(account, target_account)
  79. follow_request = FollowRequest.find_by(account: target_account, target_account: account)
  80. follow_request&.reject!
  81. end
  82. def unfollow!(account, target_account)
  83. account.unfollow!(target_account)
  84. end
  85. def reflect_block!(account, target_account)
  86. UnfollowService.new.call(target_account, account) if target_account.following?(account)
  87. account.block!(target_account)
  88. end
  89. def reflect_unblock!(account, target_account)
  90. UnblockService.new.call(account, target_account)
  91. end
  92. def delete_post!(xml, account)
  93. status = Status.find(xml.at_xpath('//xmlns:id', xmlns: TagManager::XMLNS).content)
  94. return if status.nil?
  95. RemovalWorker.perform_async(status.id) if account.id == status.account_id
  96. end
  97. def favourite!(xml, from_account)
  98. current_status = status(xml)
  99. favourite = current_status.favourites.where(account: from_account).first_or_create!(account: from_account)
  100. NotifyService.new.call(current_status.account, favourite)
  101. end
  102. def unfavourite!(xml, from_account)
  103. current_status = status(xml)
  104. favourite = current_status.favourites.where(account: from_account).first
  105. favourite&.destroy
  106. end
  107. def add_post!(body, account)
  108. ProcessingWorker.perform_async(account.id, body.force_encoding('UTF-8'))
  109. end
  110. def status(xml)
  111. Status.find(TagManager.instance.unique_tag_to_local_id(activity_id(xml), 'Status'))
  112. end
  113. def activity_id(xml)
  114. xml.at_xpath('//activity:object', activity: TagManager::AS_XMLNS).at_xpath('./xmlns:id', xmlns: TagManager::XMLNS).content
  115. end
  116. def salmon
  117. @salmon ||= OStatus2::Salmon.new
  118. end
  119. def follow_remote_account_service
  120. @follow_remote_account_service ||= FollowRemoteAccountService.new
  121. end
  122. def process_feed_service
  123. @process_feed_service ||= ProcessFeedService.new
  124. end
  125. def remove_status_service
  126. @remove_status_service ||= RemoveStatusService.new
  127. end
  128. end