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.

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