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.

238 lines
6.9 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
7 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. # @return [Enumerable] created statuses
  6. def call(body, account)
  7. xml = Nokogiri::XML(body)
  8. update_remote_profile_service.call(xml.at_xpath('/xmlns:feed/xmlns:author'), account) unless xml.at_xpath('/xmlns:feed').nil?
  9. xml.xpath('//xmlns:entry').reverse_each.map { |entry| process_entry(account, entry) }.compact
  10. end
  11. private
  12. def process_entry(account, entry)
  13. return 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. return
  19. end
  20. return 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. # Also record all media attachments for the status and for the reblogged status if present
  33. unless status.new_record?
  34. record_remote_mentions(status, entry.xpath('./xmlns:link[@rel="mentioned"]'))
  35. process_attachments(entry, status)
  36. process_attachments(entry.xpath('./activity:object'), status.reblog) if status.reblog?
  37. DistributionWorker.perform_async(status.id)
  38. end
  39. return status
  40. end
  41. def record_remote_mentions(status, links)
  42. # Here we have to do a reverse lookup of local accounts by their URL!
  43. # It's not pretty at all! I really wish all these protocols sticked to
  44. # using acct:username@domain only! It would make things so much easier
  45. # and tidier
  46. links.each do |mention_link|
  47. href_val = mention_link.attribute('href').value
  48. next if href_val == 'http://activityschema.org/collection/public'
  49. href = Addressable::URI.parse(href_val)
  50. if href.host == Rails.configuration.x.local_domain
  51. # A local user is mentioned
  52. mentioned_account = Account.find_local(href.path.gsub('/users/', ''))
  53. unless mentioned_account.nil?
  54. mentioned_account.mentions.where(status: status).first_or_create(status: status)
  55. NotificationMailer.mention(mentioned_account, status).deliver_later
  56. end
  57. else
  58. # What to do about remote user?
  59. # This is kinda dodgy because URLs could change, we don't index them
  60. mentioned_account = Account.find_by(url: href.to_s)
  61. if mentioned_account.nil?
  62. mentioned_account = FetchRemoteAccountService.new.call(href)
  63. end
  64. unless mentioned_account.nil?
  65. mentioned_account.mentions.where(status: status).first_or_create(status: status)
  66. end
  67. end
  68. end
  69. end
  70. def process_attachments(entry, status)
  71. entry.xpath('./xmlns:link[@rel="enclosure"]').each do |enclosure_link|
  72. next if enclosure_link.attribute('href').nil?
  73. media = MediaAttachment.where(status: status, remote_url: enclosure_link.attribute('href').value).first
  74. next unless media.nil?
  75. media = MediaAttachment.new(account: status.account, status: status, remote_url: enclosure_link.attribute('href').value)
  76. media.file_remote_url = enclosure_link.attribute('href').value
  77. media.save
  78. end
  79. end
  80. def add_post!(_entry, status)
  81. status.save!
  82. end
  83. def add_reblog!(entry, status)
  84. status.reblog = find_original_status(entry, target_id(entry))
  85. if status.reblog.nil?
  86. status.reblog = fetch_remote_status(entry)
  87. end
  88. if !status.reblog.nil?
  89. status.save!
  90. NotificationMailer.reblog(status.reblog, status.account).deliver_later if status.reblog.local?
  91. end
  92. end
  93. def add_reply!(entry, status)
  94. status.thread = find_original_status(entry, thread_id(entry))
  95. status.save!
  96. if status.thread.nil? && !thread_href(entry).nil?
  97. ThreadResolveWorker.perform_async(status.id, thread_href(entry))
  98. end
  99. end
  100. def delete_post!(status)
  101. remove_status_service.call(status)
  102. end
  103. def find_original_status(_xml, id)
  104. return nil if id.nil?
  105. if TagManager.instance.local_id?(id)
  106. Status.find(TagManager.instance.unique_tag_to_local_id(id, 'Status'))
  107. else
  108. Status.find_by(uri: id)
  109. end
  110. end
  111. def fetch_remote_status(xml)
  112. username = xml.at_xpath('./activity:object/xmlns:author/xmlns:name').content
  113. url = xml.at_xpath('./activity:object/xmlns:author/xmlns:uri').content
  114. domain = Addressable::URI.parse(url).host
  115. account = Account.find_by(username: username, domain: domain)
  116. if account.nil?
  117. account = follow_remote_account_service.call("#{username}@#{domain}")
  118. end
  119. status = Status.new(account: account, uri: target_id(xml), text: target_content(xml), url: target_url(xml), created_at: published(xml), updated_at: updated(xml))
  120. status.thread = find_original_status(xml, thread_id(xml))
  121. status.save
  122. if status.saved? && status.thread.nil? && !thread_href(xml).nil?
  123. ThreadResolveWorker.perform_async(status.id, thread_href(xml))
  124. end
  125. status
  126. rescue Goldfinger::Error, HTTP::Error
  127. nil
  128. end
  129. def published(xml)
  130. xml.at_xpath('./xmlns:published').content
  131. end
  132. def updated(xml)
  133. xml.at_xpath('./xmlns:updated').content
  134. end
  135. def content(xml)
  136. xml.at_xpath('./xmlns:content').content
  137. end
  138. def thread_id(xml)
  139. xml.at_xpath('./thr:in-reply-to').attribute('ref').value
  140. rescue
  141. nil
  142. end
  143. def thread_href(xml)
  144. xml.at_xpath('./thr:in-reply-to').attribute('href').value
  145. rescue
  146. nil
  147. end
  148. def target_id(xml)
  149. xml.at_xpath('.//activity:object/xmlns:id').content
  150. rescue
  151. nil
  152. end
  153. def activity_id(xml)
  154. xml.at_xpath('./xmlns:id').content
  155. end
  156. def activity_link(xml)
  157. xml.at_xpath('./xmlns:link[@rel="alternate"]').attribute('href').value
  158. rescue
  159. ''
  160. end
  161. def target_content(xml)
  162. xml.at_xpath('.//activity:object/xmlns:content').content
  163. end
  164. def target_url(xml)
  165. xml.at_xpath('.//activity:object/xmlns:link[@rel="alternate"]').attribute('href').value
  166. end
  167. def object_type(xml)
  168. xml.at_xpath('./activity:object-type').content.gsub('http://activitystrea.ms/schema/1.0/', '').to_sym
  169. rescue
  170. :note
  171. end
  172. def verb(xml)
  173. xml.at_xpath('./activity:verb').content.gsub('http://activitystrea.ms/schema/1.0/', '').to_sym
  174. rescue
  175. :post
  176. end
  177. def follow_remote_account_service
  178. @follow_remote_account_service ||= FollowRemoteAccountService.new
  179. end
  180. def update_remote_profile_service
  181. @update_remote_profile_service ||= UpdateRemoteProfileService.new
  182. end
  183. def remove_status_service
  184. @remove_status_service ||= RemoveStatusService.new
  185. end
  186. end