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

287 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
  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. Rails.logger.debug "Creating remote status #{id}"
  34. status, just_created = status_from_xml(@xml)
  35. return if status.nil?
  36. return status unless just_created
  37. if verb == :share
  38. original_status = shared_status_from_xml(@xml.at_xpath('.//activity:object', activity: TagManager::AS_XMLNS))
  39. status.reblog = original_status
  40. if original_status.nil?
  41. status.destroy
  42. return nil
  43. elsif original_status.reblog?
  44. status.reblog = original_status.reblog
  45. end
  46. end
  47. status.save!
  48. notify_about_mentions!(status) unless status.reblog?
  49. notify_about_reblog!(status) if status.reblog? && status.reblog.account.local?
  50. Rails.logger.debug "Queuing remote status #{status.id} (#{id}) for distribution"
  51. DistributionWorker.perform_async(status.id)
  52. status
  53. end
  54. def notify_about_mentions!(status)
  55. status.mentions.includes(:account).each do |mention|
  56. mentioned_account = mention.account
  57. next unless mentioned_account.local?
  58. NotifyService.new.call(mentioned_account, mention)
  59. end
  60. end
  61. def notify_about_reblog!(status)
  62. NotifyService.new.call(status.reblog.account, status)
  63. end
  64. def delete_status
  65. Rails.logger.debug "Deleting remote status #{id}"
  66. status = Status.find_by(uri: id)
  67. RemoveStatusService.new.call(status) unless status.nil?
  68. nil
  69. end
  70. def skip_unsupported_type?
  71. !([:post, :share, :delete].include?(verb) && [:activity, :note, :comment].include?(type))
  72. end
  73. def shared_status_from_xml(entry)
  74. status = find_status(id(entry))
  75. return status unless status.nil?
  76. FetchRemoteStatusService.new.call(url(entry))
  77. end
  78. def status_from_xml(entry)
  79. # Return early if status already exists in db
  80. status = find_status(id(entry))
  81. return [status, false] unless status.nil?
  82. # If status embeds an author, find that author
  83. # If that author cannot be found, don't record the status (do not misattribute)
  84. if account?(entry)
  85. begin
  86. account = find_or_resolve_account(acct(entry))
  87. return [nil, false] if account.nil?
  88. rescue Goldfinger::Error
  89. return [nil, false]
  90. end
  91. else
  92. account = @account
  93. end
  94. return [nil, false] if account.suspended?
  95. status = Status.create!(
  96. uri: id(entry),
  97. url: url(entry),
  98. account: account,
  99. text: content(entry),
  100. spoiler_text: content_warning(entry),
  101. created_at: published(entry),
  102. reply: thread?(entry),
  103. language: content_language(entry),
  104. visibility: visibility_scope(entry)
  105. )
  106. if thread?(entry)
  107. Rails.logger.debug "Trying to attach #{status.id} (#{id(entry)}) to #{thread(entry).first}"
  108. status.thread = find_or_resolve_status(status, *thread(entry))
  109. end
  110. mentions_from_xml(status, entry)
  111. hashtags_from_xml(status, entry)
  112. media_from_xml(status, entry)
  113. [status, true]
  114. end
  115. def find_or_resolve_account(acct)
  116. FollowRemoteAccountService.new.call(acct)
  117. end
  118. def find_or_resolve_status(parent, uri, url)
  119. status = find_status(uri)
  120. ThreadResolveWorker.perform_async(parent.id, url) if status.nil?
  121. status
  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(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.find_by(uri: href) || Account.find_by(url: href) || 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 OpenURI::HTTPError, Paperclip::Errors::NotIdentifiedByImageMagickError
  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 acct(xml = @xml)
  215. username = xml.at_xpath('./xmlns:author/xmlns:name', xmlns: TagManager::XMLNS).content
  216. url = xml.at_xpath('./xmlns:author/xmlns:uri', xmlns: TagManager::XMLNS).content
  217. domain = Addressable::URI.parse(url).normalize.host
  218. "#{username}@#{domain}"
  219. end
  220. end
  221. end