闭社主体 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.

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