闭社主体 forked from https://github.com/tootsuite/mastodon
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.

257 lines
8.3 KiB

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