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.

86 lines
2.2 KiB

  1. class ProcessInteractionService
  2. include ApplicationHelper
  3. def call(envelope, target_account)
  4. body = salmon.unpack(envelope)
  5. xml = Nokogiri::XML(body)
  6. return unless contains_author?(xml)
  7. username = xml.at_xpath('/xmlns:entry/xmlns:author/xmlns:name').content
  8. url = xml.at_xpath('/xmlns:entry/xmlns:author/xmlns:uri').content
  9. domain = Addressable::URI.parse(url).host
  10. account = Account.find_by(username: username, domain: domain)
  11. if account.nil?
  12. account = follow_remote_account_service.("acct:#{username}@#{domain}")
  13. return if account.nil?
  14. end
  15. if salmon.verify(envelope, account.keypair)
  16. case verb(xml)
  17. when :follow
  18. follow!(account, target_account)
  19. when :unfollow
  20. unfollow!(account, target_account)
  21. when :favorite
  22. favourite!(xml, account)
  23. when :post
  24. add_post!(body, account) if mentions_account?(xml, target_account)
  25. when :share
  26. add_post!(body, account) unless status.nil?
  27. end
  28. end
  29. end
  30. private
  31. def contains_author?(xml)
  32. !(xml.at_xpath('/xmlns:entry/xmlns:author/xmlns:name').nil? || xml.at_xpath('/xmlns:entry/xmlns:author/xmlns:uri').nil?)
  33. end
  34. def mentions_account?(xml, account)
  35. xml.xpath('/xmlns:entry/xmlns:link[@rel="mentioned"]').each { |mention_link| return true if mention_link.attribute('ref') == profile_url(name: account.username) }
  36. false
  37. end
  38. def verb(xml)
  39. xml.at_xpath('//activity:verb').content.gsub('http://activitystrea.ms/schema/1.0/', '').to_sym
  40. end
  41. def follow!(account, target_account)
  42. account.follow!(target_account)
  43. end
  44. def unfollow!(account, target_account)
  45. account.unfollow!(target_account)
  46. end
  47. def favourite!(xml, from_account)
  48. status.favourites.first_or_create!(account: from_account)
  49. end
  50. def add_post!(body, account)
  51. process_feed_service.(body, account)
  52. end
  53. def status(xml)
  54. Status.find(unique_tag_to_local_id(activity_id, 'Status'))
  55. end
  56. def activity_id(xml)
  57. xml.at_xpath('/xmlns:entry/xmlns:id').content
  58. end
  59. def salmon
  60. OStatus2::Salmon.new
  61. end
  62. def follow_remote_account_service
  63. FollowRemoteAccountService.new
  64. end
  65. def process_feed_service
  66. ProcessFeedService.new
  67. end
  68. end