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.

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