闭社主体 forked from https://github.com/tootsuite/mastodon
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.

133 lines
4.1 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. update_remote_profile_service.call(xml.at_xpath('/xmlns:entry', xmlns: TagManager::XMLNS), account, true)
  21. case verb(xml)
  22. when :follow
  23. follow!(account, target_account) unless target_account.locked? || target_account.blocking?(account)
  24. when :unfollow
  25. unfollow!(account, target_account)
  26. when :favorite
  27. favourite!(xml, account)
  28. when :post
  29. add_post!(body, account) if mentions_account?(xml, target_account)
  30. when :share
  31. add_post!(body, account) unless status(xml).nil?
  32. when :delete
  33. delete_post!(xml, account)
  34. when :block
  35. reflect_block!(account, target_account)
  36. when :unblock
  37. reflect_unblock!(account, target_account)
  38. end
  39. end
  40. rescue Goldfinger::Error, HTTP::Error, OStatus2::BadSalmonError
  41. nil
  42. end
  43. private
  44. def contains_author?(xml)
  45. !(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?)
  46. end
  47. def mentions_account?(xml, account)
  48. xml.xpath('/xmlns:entry/xmlns:link[@rel="mentioned"]', xmlns: TagManager::XMLNS).each { |mention_link| return true if mention_link.attribute('href').value == TagManager.instance.url_for(account) }
  49. false
  50. end
  51. def verb(xml)
  52. raw = xml.at_xpath('//activity:verb', activity: TagManager::AS_XMLNS).content
  53. TagManager::VERBS.key(raw)
  54. rescue
  55. :post
  56. end
  57. def follow!(account, target_account)
  58. follow = account.follow!(target_account)
  59. NotifyService.new.call(target_account, follow)
  60. end
  61. def unfollow!(account, target_account)
  62. account.unfollow!(target_account)
  63. end
  64. def reflect_block!(account, target_account)
  65. UnfollowService.new.call(target_account, account) if target_account.following?(account)
  66. account.block!(target_account)
  67. end
  68. def reflect_unblock!(account, target_account)
  69. UnblockService.new.call(account, target_account)
  70. end
  71. def delete_post!(xml, account)
  72. status = Status.find(xml.at_xpath('//xmlns:id', xmlns: TagManager::XMLNS).content)
  73. return if status.nil?
  74. remove_status_service.call(status) if account.id == status.account_id
  75. end
  76. def favourite!(xml, from_account)
  77. current_status = status(xml)
  78. favourite = current_status.favourites.where(account: from_account).first_or_create!(account: from_account)
  79. NotifyService.new.call(current_status.account, favourite)
  80. end
  81. def add_post!(body, account)
  82. process_feed_service.call(body, account)
  83. end
  84. def status(xml)
  85. Status.find(TagManager.instance.unique_tag_to_local_id(activity_id(xml), 'Status'))
  86. end
  87. def activity_id(xml)
  88. xml.at_xpath('//activity:object', activity: TagManager::AS_XMLNS).at_xpath('./xmlns:id', xmlns: TagManager::XMLNS).content
  89. end
  90. def salmon
  91. @salmon ||= OStatus2::Salmon.new
  92. end
  93. def follow_remote_account_service
  94. @follow_remote_account_service ||= FollowRemoteAccountService.new
  95. end
  96. def process_feed_service
  97. @process_feed_service ||= ProcessFeedService.new
  98. end
  99. def update_remote_profile_service
  100. @update_remote_profile_service ||= UpdateRemoteProfileService.new
  101. end
  102. def remove_status_service
  103. @remove_status_service ||= RemoveStatusService.new
  104. end
  105. end