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

266 lines
7.9 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. visibility: visibility_scope(entry)
  99. )
  100. if thread?(entry)
  101. Rails.logger.debug "Trying to attach #{status.id} (#{id(entry)}) to #{thread(entry).first}"
  102. status.thread = find_or_resolve_status(status, *thread(entry))
  103. end
  104. mentions_from_xml(status, entry)
  105. hashtags_from_xml(status, entry)
  106. media_from_xml(status, entry)
  107. [status, true]
  108. end
  109. def find_or_resolve_account(acct)
  110. FollowRemoteAccountService.new.call(acct)
  111. end
  112. def find_or_resolve_status(parent, uri, url)
  113. status = find_status(uri)
  114. ThreadResolveWorker.perform_async(parent.id, url) if status.nil?
  115. status
  116. end
  117. def find_status(uri)
  118. if TagManager.instance.local_id?(uri)
  119. local_id = TagManager.instance.unique_tag_to_local_id(uri, 'Status')
  120. return Status.find(local_id)
  121. end
  122. Status.find_by(uri: uri)
  123. end
  124. def mentions_from_xml(parent, xml)
  125. processed_account_ids = []
  126. xml.xpath('./xmlns:link[@rel="mentioned"]', xmlns: TagManager::XMLNS).each do |link|
  127. next if [TagManager::TYPES[:group], TagManager::TYPES[:collection]].include? link['ostatus:object-type']
  128. url = Addressable::URI.parse(link['href'])
  129. mentioned_account = if TagManager.instance.web_domain?(url.host)
  130. Account.find_local(url.path.gsub('/users/', ''))
  131. else
  132. Account.find_by(url: link['href']) || FetchRemoteAccountService.new.call(link['href'])
  133. end
  134. next if mentioned_account.nil? || processed_account_ids.include?(mentioned_account.id)
  135. mentioned_account.mentions.where(status: parent).first_or_create(status: parent)
  136. # So we can skip duplicate mentions
  137. processed_account_ids << mentioned_account.id
  138. end
  139. end
  140. def hashtags_from_xml(parent, xml)
  141. tags = xml.xpath('./xmlns:category', xmlns: TagManager::XMLNS).map { |category| category['term'] }.select { |t| !t.blank? }
  142. ProcessHashtagsService.new.call(parent, tags)
  143. end
  144. def media_from_xml(parent, xml)
  145. return if DomainBlock.find_by(domain: parent.account.domain)&.reject_media?
  146. xml.xpath('./xmlns:link[@rel="enclosure"]', xmlns: TagManager::XMLNS).each do |link|
  147. next unless link['href']
  148. media = MediaAttachment.where(status: parent, remote_url: link['href']).first_or_initialize(account: parent.account, status: parent, remote_url: link['href'])
  149. parsed_url = URI.parse(link['href'])
  150. next if !%w(http https).include?(parsed_url.scheme) || parsed_url.host.empty?
  151. begin
  152. media.file_remote_url = link['href']
  153. media.save
  154. rescue OpenURI::HTTPError, Paperclip::Errors::NotIdentifiedByImageMagickError
  155. next
  156. end
  157. end
  158. end
  159. def id(xml = @xml)
  160. xml.at_xpath('./xmlns:id', xmlns: TagManager::XMLNS).content
  161. end
  162. def verb(xml = @xml)
  163. raw = xml.at_xpath('./activity:verb', activity: TagManager::AS_XMLNS).content
  164. TagManager::VERBS.key(raw)
  165. rescue
  166. :post
  167. end
  168. def type(xml = @xml)
  169. raw = xml.at_xpath('./activity:object-type', activity: TagManager::AS_XMLNS).content
  170. TagManager::TYPES.key(raw)
  171. rescue
  172. :activity
  173. end
  174. def url(xml = @xml)
  175. link = xml.at_xpath('./xmlns:link[@rel="alternate"]', xmlns: TagManager::XMLNS)
  176. link.nil? ? nil : link['href']
  177. end
  178. def content(xml = @xml)
  179. xml.at_xpath('./xmlns:content', xmlns: TagManager::XMLNS).content
  180. end
  181. def content_warning(xml = @xml)
  182. xml.at_xpath('./xmlns:summary', xmlns: TagManager::XMLNS)&.content || ''
  183. end
  184. def visibility_scope(xml = @xml)
  185. xml.at_xpath('./mastodon:scope', mastodon: TagManager::MTDN_XMLNS)&.content&.to_sym || :public
  186. end
  187. def published(xml = @xml)
  188. xml.at_xpath('./xmlns:published', xmlns: TagManager::XMLNS).content
  189. end
  190. def thread?(xml = @xml)
  191. !xml.at_xpath('./thr:in-reply-to', thr: TagManager::THR_XMLNS).nil?
  192. end
  193. def thread(xml = @xml)
  194. thr = xml.at_xpath('./thr:in-reply-to', thr: TagManager::THR_XMLNS)
  195. [thr['ref'], thr['href']]
  196. end
  197. def account?(xml = @xml)
  198. !xml.at_xpath('./xmlns:author', xmlns: TagManager::XMLNS).nil?
  199. end
  200. def acct(xml = @xml)
  201. username = xml.at_xpath('./xmlns:author/xmlns:name', xmlns: TagManager::XMLNS).content
  202. url = xml.at_xpath('./xmlns:author/xmlns:uri', xmlns: TagManager::XMLNS).content
  203. domain = Addressable::URI.parse(url).host
  204. "#{username}@#{domain}"
  205. end
  206. end
  207. end