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.

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