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.

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