闭社主体 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.

248 lines
7.3 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. # frozen_string_literal: true
  2. class ProcessFeedService < BaseService
  3. def call(body, account)
  4. xml = Nokogiri::XML(body)
  5. xml.encoding = 'utf-8'
  6. update_author(xml, account)
  7. process_entries(xml, account)
  8. end
  9. private
  10. def update_author(xml, account)
  11. return if xml.at_xpath('/xmlns:feed', xmlns: TagManager::XMLNS).nil?
  12. UpdateRemoteProfileService.new.call(xml.at_xpath('/xmlns:feed', xmlns: TagManager::XMLNS), account, true)
  13. end
  14. def process_entries(xml, account)
  15. xml.xpath('//xmlns:entry', xmlns: TagManager::XMLNS).reverse_each.map { |entry| ProcessEntry.new.call(entry, account) }.compact
  16. end
  17. class ProcessEntry
  18. def call(xml, account)
  19. @account = account
  20. @xml = xml
  21. return if skip_unsupported_type?
  22. case verb
  23. when :post, :share
  24. return create_status
  25. when :delete
  26. return delete_status
  27. end
  28. rescue ActiveRecord::RecordInvalid => e
  29. Rails.logger.debug "Nothing was saved for #{id} because: #{e}"
  30. nil
  31. end
  32. private
  33. def create_status
  34. Rails.logger.debug "Creating remote status #{id}"
  35. status = status_from_xml(@xml)
  36. if verb == :share
  37. original_status = status_from_xml(@xml.at_xpath('.//activity:object', activity: TagManager::AS_XMLNS))
  38. status.reblog = original_status
  39. if original_status.nil?
  40. status.destroy
  41. return nil
  42. elsif original_status.reblog?
  43. status.reblog = original_status.reblog
  44. end
  45. end
  46. return if status.nil?
  47. status.save!
  48. NotifyService.new.call(status.reblog.account, status) if status.reblog? && status.reblog.account.local?
  49. Rails.logger.debug "Queuing remote status #{status.id} (#{id}) for distribution"
  50. DistributionWorker.perform_async(status.id)
  51. status
  52. end
  53. def delete_status
  54. Rails.logger.debug "Deleting remote status #{id}"
  55. status = Status.find_by(uri: id)
  56. RemoveStatusService.new.call(status) unless status.nil?
  57. nil
  58. end
  59. def skip_unsupported_type?
  60. !([:post, :share, :delete].include?(verb) && [:activity, :note, :comment].include?(type))
  61. end
  62. def status_from_xml(entry)
  63. # Return early if status already exists in db
  64. status = find_status(id(entry))
  65. return status unless status.nil?
  66. # If status embeds an author, find that author
  67. # If that author cannot be found, don't record the status (do not misattribute)
  68. if account?(entry)
  69. begin
  70. account = find_or_resolve_account(acct(entry))
  71. return nil if account.nil?
  72. rescue Goldfinger::Error
  73. return nil
  74. end
  75. else
  76. account = @account
  77. end
  78. return if account.suspended?
  79. status = Status.create!(
  80. uri: id(entry),
  81. url: url(entry),
  82. account: account,
  83. text: content(entry),
  84. created_at: published(entry)
  85. )
  86. if thread?(entry)
  87. Rails.logger.debug "Trying to attach #{status.id} (#{id(entry)}) to #{thread(entry).first}"
  88. status.thread = find_or_resolve_status(status, *thread(entry))
  89. end
  90. mentions_from_xml(status, entry)
  91. hashtags_from_xml(status, entry)
  92. media_from_xml(status, entry)
  93. status
  94. end
  95. def find_or_resolve_account(acct)
  96. FollowRemoteAccountService.new.call(acct)
  97. end
  98. def find_or_resolve_status(parent, uri, url)
  99. status = find_status(uri)
  100. ThreadResolveWorker.perform_async(parent.id, url) if status.nil?
  101. status
  102. end
  103. def find_status(uri)
  104. if TagManager.instance.local_id?(uri)
  105. local_id = TagManager.instance.unique_tag_to_local_id(uri, 'Status')
  106. return Status.find(local_id)
  107. end
  108. Status.find_by(uri: uri)
  109. end
  110. def mentions_from_xml(parent, xml)
  111. processed_account_ids = []
  112. public_visibility = false
  113. xml.xpath('./xmlns:link[@rel="mentioned"]', xmlns: TagManager::XMLNS).each do |link|
  114. if link['ostatus:object-type'] == TagManager::TYPES[:collection] && link['href'] == TagManager::COLLECTIONS[:public]
  115. public_visibility = true
  116. next
  117. elsif link['ostatus:object-type'] == TagManager::TYPES[:group]
  118. next
  119. end
  120. url = Addressable::URI.parse(link['href'])
  121. mentioned_account = if TagManager.instance.local_domain?(url.host)
  122. Account.find_local(url.path.gsub('/users/', ''))
  123. else
  124. Account.find_by(url: link['href']) || FetchRemoteAccountService.new.call(link['href'])
  125. end
  126. next if mentioned_account.nil? || processed_account_ids.include?(mentioned_account.id)
  127. mention = mentioned_account.mentions.where(status: parent).first_or_create(status: parent)
  128. # Notify local user
  129. NotifyService.new.call(mentioned_account, mention) if mentioned_account.local?
  130. # So we can skip duplicate mentions
  131. processed_account_ids << mentioned_account.id
  132. end
  133. parent.visibility = public_visibility ? :public : :unlisted
  134. parent.save!
  135. end
  136. def hashtags_from_xml(parent, xml)
  137. tags = xml.xpath('./xmlns:category', xmlns: TagManager::XMLNS).map { |category| category['term'] }.select { |t| !t.blank? }
  138. ProcessHashtagsService.new.call(parent, tags)
  139. end
  140. def media_from_xml(parent, xml)
  141. xml.xpath('./xmlns:link[@rel="enclosure"]', xmlns: TagManager::XMLNS).each do |link|
  142. next unless link['href']
  143. media = MediaAttachment.where(status: parent, remote_url: link['href']).first_or_initialize(account: parent.account, status: parent, remote_url: link['href'])
  144. begin
  145. media.file_remote_url = link['href']
  146. media.save
  147. rescue OpenURI::HTTPError, Paperclip::Errors::NotIdentifiedByImageMagickError
  148. next
  149. end
  150. end
  151. end
  152. def id(xml = @xml)
  153. xml.at_xpath('./xmlns:id', xmlns: TagManager::XMLNS).content
  154. end
  155. def verb(xml = @xml)
  156. raw = xml.at_xpath('./activity:verb', activity: TagManager::AS_XMLNS).content
  157. TagManager::VERBS.key(raw)
  158. rescue
  159. :post
  160. end
  161. def type(xml = @xml)
  162. raw = xml.at_xpath('./activity:object-type', activity: TagManager::AS_XMLNS).content
  163. TagManager::TYPES.key(raw)
  164. rescue
  165. :activity
  166. end
  167. def url(xml = @xml)
  168. link = xml.at_xpath('./xmlns:link[@rel="alternate"]', xmlns: TagManager::XMLNS)
  169. link.nil? ? nil : link['href']
  170. end
  171. def content(xml = @xml)
  172. xml.at_xpath('./xmlns:content', xmlns: TagManager::XMLNS).content
  173. end
  174. def published(xml = @xml)
  175. xml.at_xpath('./xmlns:published', xmlns: TagManager::XMLNS).content
  176. end
  177. def thread?(xml = @xml)
  178. !xml.at_xpath('./thr:in-reply-to', thr: TagManager::THR_XMLNS).nil?
  179. end
  180. def thread(xml = @xml)
  181. thr = xml.at_xpath('./thr:in-reply-to', thr: TagManager::THR_XMLNS)
  182. [thr['ref'], thr['href']]
  183. end
  184. def account?(xml = @xml)
  185. !xml.at_xpath('./xmlns:author', xmlns: TagManager::XMLNS).nil?
  186. end
  187. def acct(xml = @xml)
  188. username = xml.at_xpath('./xmlns:author/xmlns:name', xmlns: TagManager::XMLNS).content
  189. url = xml.at_xpath('./xmlns:author/xmlns:uri', xmlns: TagManager::XMLNS).content
  190. domain = Addressable::URI.parse(url).host
  191. "#{username}@#{domain}"
  192. end
  193. end
  194. end