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

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