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.

240 lines
7.2 KiB

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