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.

159 lines
4.4 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. class ProcessFeedService < BaseService
  2. # Create local statuses from an Atom feed
  3. # @param [String] body Atom feed
  4. # @param [Account] account Account this feed belongs to
  5. def call(body, account)
  6. xml = Nokogiri::XML(body)
  7. # If we got a full feed, make sure the account's profile is up to date
  8. unless xml.at_xpath('/xmlns:feed').nil?
  9. update_remote_profile_service.(xml.at_xpath('/xmlns:feed/xmlns:author'), account)
  10. end
  11. # Process entries
  12. xml.xpath('//xmlns:entry').each do |entry|
  13. next unless [:note, :comment, :activity].include? object_type(entry)
  14. status = Status.find_by(uri: activity_id(entry))
  15. next unless status.nil?
  16. status = Status.new(uri: activity_id(entry), url: activity_link(entry), account: account, text: content(entry), created_at: published(entry), updated_at: updated(entry))
  17. if object_type(entry) == :comment
  18. add_reply!(entry, status)
  19. elsif verb(entry) == :share
  20. add_reblog!(entry, status)
  21. else
  22. add_post!(entry, status)
  23. end
  24. # If we added a status, go through accounts it mentions and create respective relations
  25. unless status.new_record?
  26. entry.xpath('./xmlns:link[@rel="mentioned"]').each do |mention_link|
  27. # Here we have to do a reverse lookup of local accounts by their URL!
  28. # It's not pretty at all! I really wish all these protocols sticked to
  29. # using acct:username@domain only! It would make things so much easier
  30. # and tidier
  31. href = Addressable::URI.parse(mention_link.attribute('href').value)
  32. if href.host == Rails.configuration.x.local_domain
  33. mentioned_account = Account.find_by(username: href.path.gsub('/users/', ''), domain: nil)
  34. unless mentioned_account.nil?
  35. mentioned_account.mentions.first_or_create(status: status)
  36. end
  37. end
  38. end
  39. end
  40. end
  41. end
  42. private
  43. def add_post!(_entry, status)
  44. status.save!
  45. end
  46. def add_reblog!(entry, status)
  47. status.reblog = find_original_status(entry, target_id(entry))
  48. if status.reblog.nil?
  49. status.reblog = fetch_remote_status(entry)
  50. end
  51. status.save! unless status.reblog.nil?
  52. end
  53. def add_reply!(entry, status)
  54. status.thread = find_original_status(entry, thread_id(entry))
  55. status.save!
  56. end
  57. def find_original_status(_xml, id)
  58. return nil if id.nil?
  59. if local_id?(id)
  60. Status.find(unique_tag_to_local_id(id, 'Status'))
  61. else
  62. Status.find_by(uri: id)
  63. end
  64. end
  65. def fetch_remote_status(xml)
  66. username = xml.at_xpath('./activity:object/xmlns:author/xmlns:name').content
  67. url = xml.at_xpath('./activity:object/xmlns:author/xmlns:uri').content
  68. domain = Addressable::URI.parse(url).host
  69. account = Account.find_by(username: username, domain: domain)
  70. if account.nil?
  71. account = follow_remote_account_service.("acct:#{username}@#{domain}", false)
  72. return nil if account.nil?
  73. end
  74. Status.new(account: account, uri: target_id(xml), text: target_content(xml), url: target_url(xml))
  75. end
  76. def published(xml)
  77. xml.at_xpath('./xmlns:published').content
  78. end
  79. def updated(xml)
  80. xml.at_xpath('./xmlns:updated').content
  81. end
  82. def content(xml)
  83. xml.at_xpath('./xmlns:content').content
  84. end
  85. def thread_id(xml)
  86. xml.at_xpath('./thr:in-reply-to').attribute('ref').value
  87. rescue
  88. nil
  89. end
  90. def target_id(xml)
  91. xml.at_xpath('.//activity:object/xmlns:id').content
  92. rescue
  93. nil
  94. end
  95. def activity_id(xml)
  96. xml.at_xpath('./xmlns:id').content
  97. end
  98. def activity_link(xml)
  99. xml.at_xpath('./xmlns:link[@rel="alternate"]').attribute('href').value
  100. rescue
  101. ''
  102. end
  103. def target_content(xml)
  104. xml.at_xpath('.//activity:object/xmlns:content').content
  105. end
  106. def target_url(xml)
  107. xml.at_xpath('.//activity:object/xmlns:link[@rel="alternate"]').attribute('href').value
  108. end
  109. def object_type(xml)
  110. xml.at_xpath('./activity:object-type').content.gsub('http://activitystrea.ms/schema/1.0/', '').to_sym
  111. rescue
  112. :note
  113. end
  114. def verb(xml)
  115. xml.at_xpath('./activity:verb').content.gsub('http://activitystrea.ms/schema/1.0/', '').to_sym
  116. rescue
  117. :post
  118. end
  119. def follow_remote_account_service
  120. @follow_remote_account_service ||= FollowRemoteAccountService.new
  121. end
  122. def update_remote_profile_service
  123. @update_remote_profile_service ||= UpdateRemoteProfileService.new
  124. end
  125. end