The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

121 lines
3.6 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. 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)
  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. end
  35. end
  36. rescue Goldfinger::Error, HTTP::Error, OStatus2::BadSalmonError
  37. nil
  38. end
  39. private
  40. def contains_author?(xml)
  41. !(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?)
  42. end
  43. def mentions_account?(xml, account)
  44. 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) }
  45. false
  46. end
  47. def verb(xml)
  48. raw = xml.at_xpath('//activity:verb', activity: TagManager::AS_XMLNS).content
  49. TagManager::VERBS.key(raw)
  50. rescue
  51. :post
  52. end
  53. def follow!(account, target_account)
  54. follow = account.follow!(target_account)
  55. NotifyService.new.call(target_account, follow)
  56. end
  57. def unfollow!(account, target_account)
  58. account.unfollow!(target_account)
  59. end
  60. def delete_post!(xml, account)
  61. status = Status.find(xml.at_xpath('//xmlns:id', xmlns: TagManager::XMLNS).content)
  62. return if status.nil?
  63. remove_status_service.call(status) if account.id == status.account_id
  64. end
  65. def favourite!(xml, from_account)
  66. current_status = status(xml)
  67. favourite = current_status.favourites.where(account: from_account).first_or_create!(account: from_account)
  68. NotifyService.new.call(current_status.account, favourite)
  69. end
  70. def add_post!(body, account)
  71. process_feed_service.call(body, account)
  72. end
  73. def status(xml)
  74. Status.find(TagManager.instance.unique_tag_to_local_id(activity_id(xml), 'Status'))
  75. end
  76. def activity_id(xml)
  77. xml.at_xpath('//activity:object', activity: TagManager::AS_XMLNS).at_xpath('./xmlns:id', xmlns: TagManager::XMLNS).content
  78. end
  79. def salmon
  80. @salmon ||= OStatus2::Salmon.new
  81. end
  82. def follow_remote_account_service
  83. @follow_remote_account_service ||= FollowRemoteAccountService.new
  84. end
  85. def process_feed_service
  86. @process_feed_service ||= ProcessFeedService.new
  87. end
  88. def update_remote_profile_service
  89. @update_remote_profile_service ||= UpdateRemoteProfileService.new
  90. end
  91. def remove_status_service
  92. @remove_status_service ||= RemoveStatusService.new
  93. end
  94. end