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.

197 lines
5.8 KiB

8 years ago
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. update_remote_profile_service.(xml.at_xpath('/xmlns:feed/xmlns:author'), account) unless xml.at_xpath('/xmlns:feed').nil?
  8. xml.xpath('//xmlns:entry').each { |entry| process_entry(account, entry) }
  9. end
  10. private
  11. def process_entry(account, entry)
  12. return unless [:note, :comment, :activity].include? object_type(entry)
  13. status = Status.find_by(uri: activity_id(entry))
  14. # If we already have a post and the verb is now "delete", we gotta delete it and move on!
  15. if !status.nil? && verb(entry) == :delete
  16. delete_post!(status)
  17. return
  18. end
  19. return unless status.nil?
  20. status = Status.new(uri: activity_id(entry), url: activity_link(entry), account: account, text: content(entry), created_at: published(entry), updated_at: updated(entry))
  21. if verb(entry) == :share
  22. add_reblog!(entry, status)
  23. elsif verb(entry) == :post
  24. if thread_id(entry).nil?
  25. add_post!(entry, status)
  26. else
  27. add_reply!(entry, status)
  28. end
  29. end
  30. # If we added a status, go through accounts it mentions and create respective relations
  31. # Also record all media attachments for the status and for the reblogged status if present
  32. unless status.new_record?
  33. record_remote_mentions(status, entry.xpath('./xmlns:link[@rel="mentioned"]'))
  34. process_attachments(entry, status)
  35. process_attachments(entry.xpath('./activity:object'), status.reblog) if status.reblog?
  36. DistributionWorker.perform_async(status.id)
  37. end
  38. end
  39. def record_remote_mentions(status, links)
  40. # Here we have to do a reverse lookup of local accounts by their URL!
  41. # It's not pretty at all! I really wish all these protocols sticked to
  42. # using acct:username@domain only! It would make things so much easier
  43. # and tidier
  44. links.each do |mention_link|
  45. href = Addressable::URI.parse(mention_link.attribute('href').value)
  46. if href.host == Rails.configuration.x.local_domain
  47. # A local user is mentioned
  48. mentioned_account = Account.find_local(href.path.gsub('/users/', ''))
  49. unless mentioned_account.nil?
  50. mentioned_account.mentions.where(status: status).first_or_create(status: status)
  51. NotificationMailer.mention(mentioned_account, status).deliver_later
  52. end
  53. else
  54. # What to do about remote user?
  55. # Are we supposed to do a search in the database by URL?
  56. # We could technically open the URL, look for LRDD tags, get webfinger that way,
  57. # finally acquire the acct:username@domain form, and then check DB
  58. end
  59. end
  60. end
  61. def process_attachments(entry, status)
  62. entry.xpath('./xmlns:link[@rel="enclosure"]').each do |enclosure_link|
  63. next if enclosure_link.attribute('href').nil?
  64. media = MediaAttachment.new(account: status.account, status: status, remote_url: enclosure_link.attribute('href').value)
  65. media.file_remote_url = enclosure_link.attribute('href').value
  66. media.save
  67. end
  68. end
  69. def add_post!(_entry, status)
  70. status.save!
  71. end
  72. def add_reblog!(entry, status)
  73. status.reblog = find_original_status(entry, target_id(entry))
  74. if status.reblog.nil?
  75. status.reblog = fetch_remote_status(entry)
  76. end
  77. if !status.reblog.nil?
  78. status.save!
  79. NotificationMailer.reblog(status.reblog, status.account).deliver_later if status.reblog.local?
  80. end
  81. end
  82. def add_reply!(entry, status)
  83. status.thread = find_original_status(entry, thread_id(entry))
  84. status.save!
  85. end
  86. def delete_post!(status)
  87. RemoveStatusService.new.(status)
  88. end
  89. def find_original_status(_xml, id)
  90. return nil if id.nil?
  91. if TagManager.instance.local_id?(id)
  92. Status.find(TagManager.instance.unique_tag_to_local_id(id, 'Status'))
  93. else
  94. Status.find_by(uri: id)
  95. end
  96. end
  97. def fetch_remote_status(xml)
  98. username = xml.at_xpath('./activity:object/xmlns:author/xmlns:name').content
  99. url = xml.at_xpath('./activity:object/xmlns:author/xmlns:uri').content
  100. domain = Addressable::URI.parse(url).host
  101. account = Account.find_by(username: username, domain: domain)
  102. if account.nil?
  103. account = follow_remote_account_service.("#{username}@#{domain}", false)
  104. return nil if account.nil?
  105. end
  106. Status.new(account: account, uri: target_id(xml), text: target_content(xml), url: target_url(xml))
  107. end
  108. def published(xml)
  109. xml.at_xpath('./xmlns:published').content
  110. end
  111. def updated(xml)
  112. xml.at_xpath('./xmlns:updated').content
  113. end
  114. def content(xml)
  115. xml.at_xpath('./xmlns:content').content
  116. end
  117. def thread_id(xml)
  118. xml.at_xpath('./thr:in-reply-to').attribute('ref').value
  119. rescue
  120. nil
  121. end
  122. def target_id(xml)
  123. xml.at_xpath('.//activity:object/xmlns:id').content
  124. rescue
  125. nil
  126. end
  127. def activity_id(xml)
  128. xml.at_xpath('./xmlns:id').content
  129. end
  130. def activity_link(xml)
  131. xml.at_xpath('./xmlns:link[@rel="alternate"]').attribute('href').value
  132. rescue
  133. ''
  134. end
  135. def target_content(xml)
  136. xml.at_xpath('.//activity:object/xmlns:content').content
  137. end
  138. def target_url(xml)
  139. xml.at_xpath('.//activity:object/xmlns:link[@rel="alternate"]').attribute('href').value
  140. end
  141. def object_type(xml)
  142. xml.at_xpath('./activity:object-type').content.gsub('http://activitystrea.ms/schema/1.0/', '').to_sym
  143. rescue
  144. :note
  145. end
  146. def verb(xml)
  147. xml.at_xpath('./activity:verb').content.gsub('http://activitystrea.ms/schema/1.0/', '').to_sym
  148. rescue
  149. :post
  150. end
  151. def follow_remote_account_service
  152. @follow_remote_account_service ||= FollowRemoteAccountService.new
  153. end
  154. def update_remote_profile_service
  155. @update_remote_profile_service ||= UpdateRemoteProfileService.new
  156. end
  157. end