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

289 lines
8.4 KiB

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. def call(xml, account)
  18. @account = account
  19. @xml = xml
  20. return if skip_unsupported_type?
  21. case verb
  22. when :post, :share
  23. return create_status
  24. when :delete
  25. return delete_status
  26. end
  27. rescue ActiveRecord::RecordInvalid => e
  28. Rails.logger.debug "Nothing was saved for #{id} because: #{e}"
  29. nil
  30. end
  31. private
  32. def create_status
  33. if redis.exists("delete_upon_arrival:#{@account.id}:#{id}")
  34. Rails.logger.debug "Delete for status #{id} was queued, ignoring"
  35. return
  36. end
  37. status, just_created = nil
  38. Rails.logger.debug "Creating remote status #{id}"
  39. if verb == :share
  40. original_status = shared_status_from_xml(@xml.at_xpath('.//activity:object', activity: TagManager::AS_XMLNS))
  41. return nil if original_status.nil?
  42. end
  43. ApplicationRecord.transaction do
  44. status, just_created = status_from_xml(@xml)
  45. return if status.nil?
  46. return status unless just_created
  47. if verb == :share
  48. status.reblog = original_status.reblog? ? original_status.reblog : original_status
  49. end
  50. status.save!
  51. end
  52. if thread?(@xml) && status.thread.nil?
  53. Rails.logger.debug "Trying to attach #{status.id} (#{id(@xml)}) to #{thread(@xml).first}"
  54. ThreadResolveWorker.perform_async(status.id, thread(@xml).second)
  55. end
  56. notify_about_mentions!(status) unless status.reblog?
  57. notify_about_reblog!(status) if status.reblog? && status.reblog.account.local?
  58. Rails.logger.debug "Queuing remote status #{status.id} (#{id}) for distribution"
  59. LinkCrawlWorker.perform_async(status.id) unless status.spoiler_text?
  60. DistributionWorker.perform_async(status.id)
  61. status
  62. end
  63. def notify_about_mentions!(status)
  64. status.mentions.includes(:account).each do |mention|
  65. mentioned_account = mention.account
  66. next unless mentioned_account.local?
  67. NotifyService.new.call(mentioned_account, mention)
  68. end
  69. end
  70. def notify_about_reblog!(status)
  71. NotifyService.new.call(status.reblog.account, status)
  72. end
  73. def delete_status
  74. Rails.logger.debug "Deleting remote status #{id}"
  75. status = Status.find_by(uri: id, account: @account)
  76. if status.nil?
  77. redis.setex("delete_upon_arrival:#{@account.id}:#{id}", 6 * 3_600, id)
  78. else
  79. RemoveStatusService.new.call(status)
  80. end
  81. end
  82. def skip_unsupported_type?
  83. !([:post, :share, :delete].include?(verb) && [:activity, :note, :comment].include?(type))
  84. end
  85. def shared_status_from_xml(entry)
  86. status = find_status(id(entry))
  87. return status unless status.nil?
  88. FetchRemoteStatusService.new.call(url(entry))
  89. end
  90. def status_from_xml(entry)
  91. # Return early if status already exists in db
  92. status = find_status(id(entry))
  93. return [status, false] unless status.nil?
  94. account = @account
  95. return [nil, false] if account.suspended?
  96. status = Status.create!(
  97. uri: id(entry),
  98. url: url(entry),
  99. account: account,
  100. text: content(entry),
  101. spoiler_text: content_warning(entry),
  102. created_at: published(entry),
  103. reply: thread?(entry),
  104. language: content_language(entry),
  105. visibility: visibility_scope(entry),
  106. conversation: find_or_create_conversation(entry),
  107. thread: thread?(entry) ? find_status(thread(entry).first) : nil
  108. )
  109. mentions_from_xml(status, entry)
  110. hashtags_from_xml(status, entry)
  111. media_from_xml(status, entry)
  112. [status, true]
  113. end
  114. def find_or_create_conversation(xml)
  115. uri = xml.at_xpath('./ostatus:conversation', ostatus: TagManager::OS_XMLNS)&.attribute('ref')&.content
  116. return if uri.nil?
  117. if TagManager.instance.local_id?(uri)
  118. local_id = TagManager.instance.unique_tag_to_local_id(uri, 'Conversation')
  119. return Conversation.find_by(id: local_id)
  120. end
  121. Conversation.find_by(uri: uri) || Conversation.create!(uri: uri)
  122. end
  123. def find_status(uri)
  124. if TagManager.instance.local_id?(uri)
  125. local_id = TagManager.instance.unique_tag_to_local_id(uri, 'Status')
  126. return Status.find_by(id: local_id)
  127. end
  128. Status.find_by(uri: uri)
  129. end
  130. def mentions_from_xml(parent, xml)
  131. processed_account_ids = []
  132. xml.xpath('./xmlns:link[@rel="mentioned"]', xmlns: TagManager::XMLNS).each do |link|
  133. next if [TagManager::TYPES[:group], TagManager::TYPES[:collection]].include? link['ostatus:object-type']
  134. mentioned_account = account_from_href(link['href'])
  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 account_from_href(href)
  142. url = Addressable::URI.parse(href).normalize
  143. if TagManager.instance.web_domain?(url.host)
  144. Account.find_local(url.path.gsub('/users/', ''))
  145. else
  146. Account.where(uri: href).or(Account.where(url: href)).first || FetchRemoteAccountService.new.call(href)
  147. end
  148. end
  149. def hashtags_from_xml(parent, xml)
  150. tags = xml.xpath('./xmlns:category', xmlns: TagManager::XMLNS).map { |category| category['term'] }.select(&:present?)
  151. ProcessHashtagsService.new.call(parent, tags)
  152. end
  153. def media_from_xml(parent, xml)
  154. do_not_download = DomainBlock.find_by(domain: parent.account.domain)&.reject_media?
  155. xml.xpath('./xmlns:link[@rel="enclosure"]', xmlns: TagManager::XMLNS).each do |link|
  156. next unless link['href']
  157. media = MediaAttachment.where(status: parent, remote_url: link['href']).first_or_initialize(account: parent.account, status: parent, remote_url: link['href'])
  158. parsed_url = Addressable::URI.parse(link['href']).normalize
  159. next if !%w(http https).include?(parsed_url.scheme) || parsed_url.host.empty?
  160. media.save
  161. next if do_not_download
  162. begin
  163. media.file_remote_url = link['href']
  164. media.save!
  165. rescue ActiveRecord::RecordInvalid
  166. next
  167. end
  168. end
  169. end
  170. def id(xml = @xml)
  171. xml.at_xpath('./xmlns:id', xmlns: TagManager::XMLNS).content
  172. end
  173. def verb(xml = @xml)
  174. raw = xml.at_xpath('./activity:verb', activity: TagManager::AS_XMLNS).content
  175. TagManager::VERBS.key(raw)
  176. rescue
  177. :post
  178. end
  179. def type(xml = @xml)
  180. raw = xml.at_xpath('./activity:object-type', activity: TagManager::AS_XMLNS).content
  181. TagManager::TYPES.key(raw)
  182. rescue
  183. :activity
  184. end
  185. def url(xml = @xml)
  186. link = xml.at_xpath('./xmlns:link[@rel="alternate"]', xmlns: TagManager::XMLNS)
  187. link.nil? ? nil : link['href']
  188. end
  189. def content(xml = @xml)
  190. xml.at_xpath('./xmlns:content', xmlns: TagManager::XMLNS).content
  191. end
  192. def content_language(xml = @xml)
  193. xml.at_xpath('./xmlns:content', xmlns: TagManager::XMLNS)['xml:lang']&.presence || 'en'
  194. end
  195. def content_warning(xml = @xml)
  196. xml.at_xpath('./xmlns:summary', xmlns: TagManager::XMLNS)&.content || ''
  197. end
  198. def visibility_scope(xml = @xml)
  199. xml.at_xpath('./mastodon:scope', mastodon: TagManager::MTDN_XMLNS)&.content&.to_sym || :public
  200. end
  201. def published(xml = @xml)
  202. xml.at_xpath('./xmlns:published', xmlns: TagManager::XMLNS).content
  203. end
  204. def thread?(xml = @xml)
  205. !xml.at_xpath('./thr:in-reply-to', thr: TagManager::THR_XMLNS).nil?
  206. end
  207. def thread(xml = @xml)
  208. thr = xml.at_xpath('./thr:in-reply-to', thr: TagManager::THR_XMLNS)
  209. [thr['ref'], thr['href']]
  210. end
  211. def account?(xml = @xml)
  212. !xml.at_xpath('./xmlns:author', xmlns: TagManager::XMLNS).nil?
  213. end
  214. def redis
  215. Redis.current
  216. end
  217. end
  218. end