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.

95 lines
2.7 KiB

  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.("acct:#{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. end
  31. end
  32. end
  33. private
  34. def contains_author?(xml)
  35. !(xml.at_xpath('/xmlns:entry/xmlns:author/xmlns:name').nil? || xml.at_xpath('/xmlns:entry/xmlns:author/xmlns:uri').nil?)
  36. end
  37. def mentions_account?(xml, account)
  38. xml.xpath('/xmlns:entry/xmlns:link[@rel="mentioned"]').each { |mention_link| return true if mention_link.attribute('ref') == profile_url(name: account.username) }
  39. false
  40. end
  41. def verb(xml)
  42. xml.at_xpath('//activity:verb').content.gsub('http://activitystrea.ms/schema/1.0/', '').to_sym
  43. rescue
  44. :post
  45. end
  46. def follow!(account, target_account)
  47. account.follow!(target_account)
  48. end
  49. def unfollow!(account, target_account)
  50. account.unfollow!(target_account)
  51. end
  52. def favourite!(xml, from_account)
  53. status(xml).favourites.first_or_create!(account: from_account)
  54. end
  55. def add_post!(body, account)
  56. process_feed_service.(body, account)
  57. end
  58. def status(xml)
  59. Status.find(unique_tag_to_local_id(activity_id(xml), 'Status'))
  60. end
  61. def activity_id(xml)
  62. xml.at_xpath('/xmlns:entry/xmlns:id').content
  63. end
  64. def salmon
  65. @salmon ||= OStatus2::Salmon.new
  66. end
  67. def follow_remote_account_service
  68. @follow_remote_account_service ||= FollowRemoteAccountService.new
  69. end
  70. def process_feed_service
  71. @process_feed_service ||= ProcessFeedService.new
  72. end
  73. def update_remote_profile_service
  74. @update_remote_profile_service ||= UpdateRemoteProfileService.new
  75. end
  76. end