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

279 lines
8.1 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, = 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 status_from_xml(entry)
  74. # Return early if status already exists in db
  75. status = find_status(id(entry))
  76. return [status, false] unless status.nil?
  77. # If status embeds an author, find that author
  78. # If that author cannot be found, don't record the status (do not misattribute)
  79. if account?(entry)
  80. begin
  81. account = find_or_resolve_account(acct(entry))
  82. return [nil, false] if account.nil?
  83. rescue Goldfinger::Error
  84. return [nil, false]
  85. end
  86. else
  87. account = @account
  88. end
  89. return [nil, false] if account.suspended?
  90. status = Status.create!(
  91. uri: id(entry),
  92. url: url(entry),
  93. account: account,
  94. text: content(entry),
  95. spoiler_text: content_warning(entry),
  96. created_at: published(entry),
  97. reply: thread?(entry),
  98. language: content_language(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. mentioned_account = account_from_href(link['href'])
  130. next if mentioned_account.nil? || processed_account_ids.include?(mentioned_account.id)
  131. mentioned_account.mentions.where(status: parent).first_or_create(status: parent)
  132. # So we can skip duplicate mentions
  133. processed_account_ids << mentioned_account.id
  134. end
  135. end
  136. def account_from_href(href)
  137. url = Addressable::URI.parse(href)
  138. if TagManager.instance.web_domain?(url.host)
  139. Account.find_local(url.path.gsub('/users/', ''))
  140. else
  141. Account.find_by(uri: href) || Account.find_by(url: href) || FetchRemoteAccountService.new.call(href)
  142. end
  143. end
  144. def hashtags_from_xml(parent, xml)
  145. tags = xml.xpath('./xmlns:category', xmlns: TagManager::XMLNS).map { |category| category['term'] }.select(&:present?)
  146. ProcessHashtagsService.new.call(parent, tags)
  147. end
  148. def media_from_xml(parent, xml)
  149. do_not_download = DomainBlock.find_by(domain: parent.account.domain)&.reject_media?
  150. xml.xpath('./xmlns:link[@rel="enclosure"]', xmlns: TagManager::XMLNS).each do |link|
  151. next unless link['href']
  152. media = MediaAttachment.where(status: parent, remote_url: link['href']).first_or_initialize(account: parent.account, status: parent, remote_url: link['href'])
  153. parsed_url = URI.parse(link['href'])
  154. next if !%w[http https].include?(parsed_url.scheme) || parsed_url.host.empty?
  155. media.save
  156. next if do_not_download
  157. begin
  158. media.file_remote_url = link['href']
  159. media.save
  160. rescue OpenURI::HTTPError, Paperclip::Errors::NotIdentifiedByImageMagickError
  161. next
  162. end
  163. end
  164. end
  165. def id(xml = @xml)
  166. xml.at_xpath('./xmlns:id', xmlns: TagManager::XMLNS).content
  167. end
  168. def verb(xml = @xml)
  169. raw = xml.at_xpath('./activity:verb', activity: TagManager::AS_XMLNS).content
  170. TagManager::VERBS.key(raw)
  171. rescue
  172. :post
  173. end
  174. def type(xml = @xml)
  175. raw = xml.at_xpath('./activity:object-type', activity: TagManager::AS_XMLNS).content
  176. TagManager::TYPES.key(raw)
  177. rescue
  178. :activity
  179. end
  180. def url(xml = @xml)
  181. link = xml.at_xpath('./xmlns:link[@rel="alternate"]', xmlns: TagManager::XMLNS)
  182. link.nil? ? nil : link['href']
  183. end
  184. def content(xml = @xml)
  185. xml.at_xpath('./xmlns:content', xmlns: TagManager::XMLNS).content
  186. end
  187. def content_language(xml = @xml)
  188. xml.at_xpath('./xmlns:content', xmlns: TagManager::XMLNS)['xml:lang']&.presence || 'en'
  189. end
  190. def content_warning(xml = @xml)
  191. xml.at_xpath('./xmlns:summary', xmlns: TagManager::XMLNS)&.content || ''
  192. end
  193. def visibility_scope(xml = @xml)
  194. xml.at_xpath('./mastodon:scope', mastodon: TagManager::MTDN_XMLNS)&.content&.to_sym || :public
  195. end
  196. def published(xml = @xml)
  197. xml.at_xpath('./xmlns:published', xmlns: TagManager::XMLNS).content
  198. end
  199. def thread?(xml = @xml)
  200. !xml.at_xpath('./thr:in-reply-to', thr: TagManager::THR_XMLNS).nil?
  201. end
  202. def thread(xml = @xml)
  203. thr = xml.at_xpath('./thr:in-reply-to', thr: TagManager::THR_XMLNS)
  204. [thr['ref'], thr['href']]
  205. end
  206. def account?(xml = @xml)
  207. !xml.at_xpath('./xmlns:author', xmlns: TagManager::XMLNS).nil?
  208. end
  209. def acct(xml = @xml)
  210. username = xml.at_xpath('./xmlns:author/xmlns:name', xmlns: TagManager::XMLNS).content
  211. url = xml.at_xpath('./xmlns:author/xmlns:uri', xmlns: TagManager::XMLNS).content
  212. domain = Addressable::URI.parse(url).host
  213. "#{username}@#{domain}"
  214. end
  215. end
  216. end