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.

181 lines
5.0 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 !status.nil? && 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. NotificationMailer.mention(mentioned_account, status).deliver_later
  44. end
  45. end
  46. end
  47. fan_out_on_write_service.(status)
  48. end
  49. end
  50. end
  51. private
  52. def add_post!(_entry, status)
  53. status.save!
  54. end
  55. def add_reblog!(entry, status)
  56. status.reblog = find_original_status(entry, target_id(entry))
  57. if status.reblog.nil?
  58. status.reblog = fetch_remote_status(entry)
  59. end
  60. if !status.reblog.nil?
  61. status.save!
  62. NotificationMailer.reblog(status.reblog, status.account).deliver_later
  63. end
  64. end
  65. def add_reply!(entry, status)
  66. status.thread = find_original_status(entry, thread_id(entry))
  67. status.save!
  68. end
  69. def delete_post!(status)
  70. status.destroy!
  71. end
  72. def find_original_status(_xml, id)
  73. return nil if id.nil?
  74. if local_id?(id)
  75. Status.find(unique_tag_to_local_id(id, 'Status'))
  76. else
  77. Status.find_by(uri: id)
  78. end
  79. end
  80. def fetch_remote_status(xml)
  81. username = xml.at_xpath('./activity:object/xmlns:author/xmlns:name').content
  82. url = xml.at_xpath('./activity:object/xmlns:author/xmlns:uri').content
  83. domain = Addressable::URI.parse(url).host
  84. account = Account.find_by(username: username, domain: domain)
  85. if account.nil?
  86. account = follow_remote_account_service.("#{username}@#{domain}", false)
  87. return nil if account.nil?
  88. end
  89. Status.new(account: account, uri: target_id(xml), text: target_content(xml), url: target_url(xml))
  90. end
  91. def published(xml)
  92. xml.at_xpath('./xmlns:published').content
  93. end
  94. def updated(xml)
  95. xml.at_xpath('./xmlns:updated').content
  96. end
  97. def content(xml)
  98. xml.at_xpath('./xmlns:content').content
  99. end
  100. def thread_id(xml)
  101. xml.at_xpath('./thr:in-reply-to').attribute('ref').value
  102. rescue
  103. nil
  104. end
  105. def target_id(xml)
  106. xml.at_xpath('.//activity:object/xmlns:id').content
  107. rescue
  108. nil
  109. end
  110. def activity_id(xml)
  111. xml.at_xpath('./xmlns:id').content
  112. end
  113. def activity_link(xml)
  114. xml.at_xpath('./xmlns:link[@rel="alternate"]').attribute('href').value
  115. rescue
  116. ''
  117. end
  118. def target_content(xml)
  119. xml.at_xpath('.//activity:object/xmlns:content').content
  120. end
  121. def target_url(xml)
  122. xml.at_xpath('.//activity:object/xmlns:link[@rel="alternate"]').attribute('href').value
  123. end
  124. def object_type(xml)
  125. xml.at_xpath('./activity:object-type').content.gsub('http://activitystrea.ms/schema/1.0/', '').to_sym
  126. rescue
  127. :note
  128. end
  129. def verb(xml)
  130. xml.at_xpath('./activity:verb').content.gsub('http://activitystrea.ms/schema/1.0/', '').to_sym
  131. rescue
  132. :post
  133. end
  134. def follow_remote_account_service
  135. @follow_remote_account_service ||= FollowRemoteAccountService.new
  136. end
  137. def update_remote_profile_service
  138. @update_remote_profile_service ||= UpdateRemoteProfileService.new
  139. end
  140. def fan_out_on_write_service
  141. @fan_out_on_write_service ||= FanOutOnWriteService.new
  142. end
  143. end