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

237 lines
6.8 KiB

8 years ago
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. ACTIVITY_NS = 'http://activitystrea.ms/spec/1.0/'
  4. THREAD_NS = 'http://purl.org/syndication/thread/1.0'
  5. def call(body, account)
  6. xml = Nokogiri::XML(body)
  7. xml.encoding = 'utf-8'
  8. update_author(xml, account)
  9. process_entries(xml, account)
  10. end
  11. private
  12. def update_author(xml, account)
  13. return if xml.at_xpath('/xmlns:feed').nil?
  14. UpdateRemoteProfileService.new.call(xml.at_xpath('/xmlns:feed/xmlns:author'), account)
  15. end
  16. def process_entries(xml, account)
  17. xml.xpath('//xmlns:entry').reverse_each.map { |entry| ProcessEntry.new.call(entry, account) }.compact
  18. end
  19. class ProcessEntry
  20. def call(xml, account)
  21. @account = account
  22. @xml = xml
  23. return if skip_unsupported_type?
  24. case verb
  25. when :post, :share
  26. return create_status
  27. when :delete
  28. return delete_status
  29. end
  30. rescue ActiveRecord::RecordInvalid => e
  31. Rails.logger.debug "Nothing was saved for #{id} because: #{e}"
  32. nil
  33. end
  34. private
  35. def create_status
  36. Rails.logger.debug "Creating remote status #{id}"
  37. status = status_from_xml(@xml)
  38. if verb == :share
  39. original_status = status_from_xml(@xml.at_xpath('.//activity:object', activity: ACTIVITY_NS))
  40. status.reblog = original_status
  41. if original_status.nil?
  42. status.destroy
  43. return nil
  44. elsif original_status.reblog?
  45. status.reblog = original_status.reblog
  46. end
  47. end
  48. status.save!
  49. NotifyService.new.call(status.reblog.account, 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 delete_status
  55. Rails.logger.debug "Deleting remote status #{id}"
  56. status = Status.find_by(uri: id)
  57. RemoveStatusService.new.call(status) unless status.nil?
  58. nil
  59. end
  60. def skip_unsupported_type?
  61. !([:post, :share, :delete].include?(verb) && [:activity, :note, :comment].include?(type))
  62. end
  63. def status_from_xml(entry)
  64. # Return early if status already exists in db
  65. status = find_status(id(entry))
  66. return status unless status.nil?
  67. # If status embeds an author, find that author
  68. # If that author cannot be found, don't record the status (do not misattribute)
  69. if account?(entry)
  70. begin
  71. account = find_or_resolve_account(acct(entry))
  72. return nil if account.nil?
  73. rescue Goldfinger::Error
  74. return nil
  75. end
  76. else
  77. account = @account
  78. end
  79. status = Status.create!(
  80. uri: id(entry),
  81. url: url(entry),
  82. account: account,
  83. text: content(entry),
  84. created_at: published(entry)
  85. )
  86. if thread?(entry)
  87. Rails.logger.debug "Trying to attach #{status.id} (#{id(entry)}) to #{thread(entry).first}"
  88. status.thread = find_or_resolve_status(status, *thread(entry))
  89. end
  90. mentions_from_xml(status, entry)
  91. hashtags_from_xml(status, entry)
  92. media_from_xml(status, entry)
  93. status
  94. end
  95. def find_or_resolve_account(acct)
  96. FollowRemoteAccountService.new.call(acct)
  97. end
  98. def find_or_resolve_status(parent, uri, url)
  99. status = find_status(uri)
  100. ThreadResolveWorker.perform_async(parent.id, url) if status.nil?
  101. status
  102. end
  103. def find_status(uri)
  104. if TagManager.instance.local_id?(uri)
  105. local_id = TagManager.instance.unique_tag_to_local_id(uri, 'Status')
  106. return Status.find(local_id)
  107. end
  108. Status.find_by(uri: uri)
  109. end
  110. def mentions_from_xml(parent, xml)
  111. processed_account_ids = []
  112. xml.xpath('./xmlns:link[@rel="mentioned"]').each do |link|
  113. next if link['href'] == 'http://activityschema.org/collection/public'
  114. url = Addressable::URI.parse(link['href'])
  115. mentioned_account = if TagManager.instance.local_domain?(url.host)
  116. Account.find_local(url.path.gsub('/users/', ''))
  117. else
  118. Account.find_by(url: link['href']) || FetchRemoteAccountService.new.call(link['href'])
  119. end
  120. next if mentioned_account.nil? || processed_account_ids.include?(mentioned_account.id)
  121. mention = mentioned_account.mentions.where(status: parent).first_or_create(status: parent)
  122. # Notify local user
  123. NotifyService.new.call(mentioned_account, mention) if mentioned_account.local?
  124. # So we can skip duplicate mentions
  125. processed_account_ids << mentioned_account.id
  126. end
  127. end
  128. def hashtags_from_xml(parent, xml)
  129. tags = xml.xpath('./xmlns:category').map { |category| category['term'] }.select { |t| !t.blank? }
  130. ProcessHashtagsService.new.call(parent, tags)
  131. end
  132. def media_from_xml(parent, xml)
  133. xml.xpath('./xmlns:link[@rel="enclosure"]').each do |link|
  134. next unless link['href']
  135. media = MediaAttachment.where(status: parent, remote_url: link['href']).first_or_initialize(account: parent.account, status: parent, remote_url: link['href'])
  136. begin
  137. media.file_remote_url = link['href']
  138. media.save
  139. rescue OpenURI::HTTPError, Paperclip::Errors::NotIdentifiedByImageMagickError
  140. next
  141. end
  142. end
  143. end
  144. def id(xml = @xml)
  145. xml.at_xpath('./xmlns:id').content
  146. end
  147. def verb(xml = @xml)
  148. raw = xml.at_xpath('./activity:verb', activity: ACTIVITY_NS).content
  149. raw.gsub('http://activitystrea.ms/schema/1.0/', '').gsub('http://ostatus.org/schema/1.0/', '').to_sym
  150. rescue
  151. :post
  152. end
  153. def type(xml = @xml)
  154. raw = xml.at_xpath('./activity:object-type', activity: ACTIVITY_NS).content
  155. raw.gsub('http://activitystrea.ms/schema/1.0/', '').gsub('http://ostatus.org/schema/1.0/', '').to_sym
  156. rescue
  157. :activity
  158. end
  159. def url(xml = @xml)
  160. link = xml.at_xpath('./xmlns:link[@rel="alternate"]')
  161. link.nil? ? nil : link['href']
  162. end
  163. def content(xml = @xml)
  164. xml.at_xpath('./xmlns:content').content
  165. end
  166. def published(xml = @xml)
  167. xml.at_xpath('./xmlns:published').content
  168. end
  169. def thread?(xml = @xml)
  170. !xml.at_xpath('./thr:in-reply-to', thr: THREAD_NS).nil?
  171. end
  172. def thread(xml = @xml)
  173. thr = xml.at_xpath('./thr:in-reply-to', thr: THREAD_NS)
  174. [thr['ref'], thr['href']]
  175. end
  176. def account?(xml = @xml)
  177. !xml.at_xpath('./xmlns:author').nil?
  178. end
  179. def acct(xml = @xml)
  180. username = xml.at_xpath('./xmlns:author/xmlns:name').content
  181. url = xml.at_xpath('./xmlns:author/xmlns:uri').content
  182. domain = Addressable::URI.parse(url).host
  183. "#{username}@#{domain}"
  184. end
  185. end
  186. end