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

267 lines
8.0 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, just_created = status_from_xml(@xml)
  36. return if status.nil?
  37. return status unless just_created
  38. if verb == :share
  39. original_status, = status_from_xml(@xml.at_xpath('.//activity:object', activity: TagManager::AS_XMLNS))
  40. status.reblog = original_status
  41. if original_status.nil?
  42. status.destroy
  43. return nil
  44. elsif original_status.reblog?
  45. status.reblog = original_status.reblog
  46. end
  47. end
  48. status.save!
  49. notify_about_mentions!(status) unless status.reblog?
  50. notify_about_reblog!(status) if status.reblog? && status.reblog.account.local?
  51. Rails.logger.debug "Queuing remote status #{status.id} (#{id}) for distribution"
  52. DistributionWorker.perform_async(status.id)
  53. status
  54. end
  55. def notify_about_mentions!(status)
  56. status.mentions.includes(:account).each do |mention|
  57. mentioned_account = mention.account
  58. next unless mentioned_account.local?
  59. NotifyService.new.call(mentioned_account, mention)
  60. end
  61. end
  62. def notify_about_reblog!(status)
  63. NotifyService.new.call(status.reblog.account, status)
  64. end
  65. def delete_status
  66. Rails.logger.debug "Deleting remote status #{id}"
  67. status = Status.find_by(uri: id)
  68. RemoveStatusService.new.call(status) unless status.nil?
  69. nil
  70. end
  71. def skip_unsupported_type?
  72. !([:post, :share, :delete].include?(verb) && [:activity, :note, :comment].include?(type))
  73. end
  74. def status_from_xml(entry)
  75. # Return early if status already exists in db
  76. status = find_status(id(entry))
  77. return [status, false] unless status.nil?
  78. # If status embeds an author, find that author
  79. # If that author cannot be found, don't record the status (do not misattribute)
  80. if account?(entry)
  81. begin
  82. account = find_or_resolve_account(acct(entry))
  83. return [nil, false] if account.nil?
  84. rescue Goldfinger::Error
  85. return [nil, false]
  86. end
  87. else
  88. account = @account
  89. end
  90. return [nil, false] if account.suspended?
  91. status = Status.create!(
  92. uri: id(entry),
  93. url: url(entry),
  94. account: account,
  95. text: content(entry),
  96. spoiler_text: content_warning(entry),
  97. created_at: published(entry),
  98. reply: thread?(entry),
  99. visibility: visibility_scope(entry)
  100. )
  101. if thread?(entry)
  102. Rails.logger.debug "Trying to attach #{status.id} (#{id(entry)}) to #{thread(entry).first}"
  103. status.thread = find_or_resolve_status(status, *thread(entry))
  104. end
  105. mentions_from_xml(status, entry)
  106. hashtags_from_xml(status, entry)
  107. media_from_xml(status, entry)
  108. [status, true]
  109. end
  110. def find_or_resolve_account(acct)
  111. FollowRemoteAccountService.new.call(acct)
  112. end
  113. def find_or_resolve_status(parent, uri, url)
  114. status = find_status(uri)
  115. ThreadResolveWorker.perform_async(parent.id, url) if status.nil?
  116. status
  117. end
  118. def find_status(uri)
  119. if TagManager.instance.local_id?(uri)
  120. local_id = TagManager.instance.unique_tag_to_local_id(uri, 'Status')
  121. return Status.find(local_id)
  122. end
  123. Status.find_by(uri: uri)
  124. end
  125. def mentions_from_xml(parent, xml)
  126. processed_account_ids = []
  127. xml.xpath('./xmlns:link[@rel="mentioned"]', xmlns: TagManager::XMLNS).each do |link|
  128. next if [TagManager::TYPES[:group], TagManager::TYPES[:collection]].include? link['ostatus:object-type']
  129. url = Addressable::URI.parse(link['href'])
  130. mentioned_account = if TagManager.instance.local_domain?(url.host)
  131. Account.find_local(url.path.gsub('/users/', ''))
  132. else
  133. Account.find_by(url: link['href']) || FetchRemoteAccountService.new.call(link['href'])
  134. end
  135. next if mentioned_account.nil? || processed_account_ids.include?(mentioned_account.id)
  136. mentioned_account.mentions.where(status: parent).first_or_create(status: parent)
  137. # So we can skip duplicate mentions
  138. processed_account_ids << mentioned_account.id
  139. end
  140. end
  141. def hashtags_from_xml(parent, xml)
  142. tags = xml.xpath('./xmlns:category', xmlns: TagManager::XMLNS).map { |category| category['term'] }.select { |t| !t.blank? }
  143. ProcessHashtagsService.new.call(parent, tags)
  144. end
  145. def media_from_xml(parent, xml)
  146. return if DomainBlock.find_by(domain: parent.account.domain)&.reject_media?
  147. xml.xpath('./xmlns:link[@rel="enclosure"]', xmlns: TagManager::XMLNS).each do |link|
  148. next unless link['href']
  149. media = MediaAttachment.where(status: parent, remote_url: link['href']).first_or_initialize(account: parent.account, status: parent, remote_url: link['href'])
  150. parsed_url = URI.parse(link['href'])
  151. next if !%w(http https).include?(parsed_url.scheme) || parsed_url.host.empty?
  152. begin
  153. media.file_remote_url = link['href']
  154. media.save
  155. rescue OpenURI::HTTPError, Paperclip::Errors::NotIdentifiedByImageMagickError
  156. next
  157. end
  158. end
  159. end
  160. def id(xml = @xml)
  161. xml.at_xpath('./xmlns:id', xmlns: TagManager::XMLNS).content
  162. end
  163. def verb(xml = @xml)
  164. raw = xml.at_xpath('./activity:verb', activity: TagManager::AS_XMLNS).content
  165. TagManager::VERBS.key(raw)
  166. rescue
  167. :post
  168. end
  169. def type(xml = @xml)
  170. raw = xml.at_xpath('./activity:object-type', activity: TagManager::AS_XMLNS).content
  171. TagManager::TYPES.key(raw)
  172. rescue
  173. :activity
  174. end
  175. def url(xml = @xml)
  176. link = xml.at_xpath('./xmlns:link[@rel="alternate"]', xmlns: TagManager::XMLNS)
  177. link.nil? ? nil : link['href']
  178. end
  179. def content(xml = @xml)
  180. xml.at_xpath('./xmlns:content', xmlns: TagManager::XMLNS).content
  181. end
  182. def content_warning(xml = @xml)
  183. xml.at_xpath('./xmlns:summary', xmlns: TagManager::XMLNS)&.content || ''
  184. end
  185. def visibility_scope(xml = @xml)
  186. xml.at_xpath('./mastodon:scope', mastodon: TagManager::MTDN_XMLNS)&.content&.to_sym || :public
  187. end
  188. def published(xml = @xml)
  189. xml.at_xpath('./xmlns:published', xmlns: TagManager::XMLNS).content
  190. end
  191. def thread?(xml = @xml)
  192. !xml.at_xpath('./thr:in-reply-to', thr: TagManager::THR_XMLNS).nil?
  193. end
  194. def thread(xml = @xml)
  195. thr = xml.at_xpath('./thr:in-reply-to', thr: TagManager::THR_XMLNS)
  196. [thr['ref'], thr['href']]
  197. end
  198. def account?(xml = @xml)
  199. !xml.at_xpath('./xmlns:author', xmlns: TagManager::XMLNS).nil?
  200. end
  201. def acct(xml = @xml)
  202. username = xml.at_xpath('./xmlns:author/xmlns:name', xmlns: TagManager::XMLNS).content
  203. url = xml.at_xpath('./xmlns:author/xmlns:uri', xmlns: TagManager::XMLNS).content
  204. domain = Addressable::URI.parse(url).host
  205. "#{username}@#{domain}"
  206. end
  207. end
  208. end