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.

257 lines
7.6 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(xml, account)
  7. process_entries(xml, account)
  8. end
  9. private
  10. def update_author(xml, account)
  11. return if xml.at_xpath('/xmlns:feed', xmlns: TagManager::XMLNS).nil?
  12. UpdateRemoteProfileService.new.call(xml.at_xpath('/xmlns:feed', xmlns: TagManager::XMLNS), account, true)
  13. end
  14. def process_entries(xml, account)
  15. xml.xpath('//xmlns:entry', xmlns: TagManager::XMLNS).reverse_each.map { |entry| ProcessEntry.new.call(entry, account) }.compact
  16. end
  17. class ProcessEntry
  18. def call(xml, account)
  19. @account = account
  20. @xml = xml
  21. return if skip_unsupported_type?
  22. case verb
  23. when :post, :share
  24. return create_status
  25. when :delete
  26. return delete_status
  27. end
  28. rescue ActiveRecord::RecordInvalid => e
  29. Rails.logger.debug "Nothing was saved for #{id} because: #{e}"
  30. nil
  31. end
  32. private
  33. def create_status
  34. Rails.logger.debug "Creating remote status #{id}"
  35. status, just_created = status_from_xml(@xml)
  36. return if status.nil?
  37. return status unless just_created
  38. if verb == :share
  39. original_status, = status_from_xml(@xml.at_xpath('.//activity:object', activity: TagManager::AS_XMLNS))
  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, false] 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, false] if account.nil?
  73. rescue Goldfinger::Error
  74. return [nil, false]
  75. end
  76. else
  77. account = @account
  78. end
  79. return [nil, false] if account.suspended?
  80. status = Status.create!(
  81. uri: id(entry),
  82. url: url(entry),
  83. account: account,
  84. text: content(entry),
  85. spoiler_text: content_warning(entry),
  86. created_at: published(entry)
  87. )
  88. if thread?(entry)
  89. Rails.logger.debug "Trying to attach #{status.id} (#{id(entry)}) to #{thread(entry).first}"
  90. status.thread = find_or_resolve_status(status, *thread(entry))
  91. end
  92. mentions_from_xml(status, entry)
  93. hashtags_from_xml(status, entry)
  94. media_from_xml(status, entry)
  95. [status, true]
  96. end
  97. def find_or_resolve_account(acct)
  98. FollowRemoteAccountService.new.call(acct)
  99. end
  100. def find_or_resolve_status(parent, uri, url)
  101. status = find_status(uri)
  102. ThreadResolveWorker.perform_async(parent.id, url) if status.nil?
  103. status
  104. end
  105. def find_status(uri)
  106. if TagManager.instance.local_id?(uri)
  107. local_id = TagManager.instance.unique_tag_to_local_id(uri, 'Status')
  108. return Status.find(local_id)
  109. end
  110. Status.find_by(uri: uri)
  111. end
  112. def mentions_from_xml(parent, xml)
  113. processed_account_ids = []
  114. public_visibility = false
  115. xml.xpath('./xmlns:link[@rel="mentioned"]', xmlns: TagManager::XMLNS).each do |link|
  116. if link['ostatus:object-type'] == TagManager::TYPES[:collection] && link['href'] == TagManager::COLLECTIONS[:public]
  117. public_visibility = true
  118. next
  119. elsif link['ostatus:object-type'] == TagManager::TYPES[:group]
  120. next
  121. end
  122. url = Addressable::URI.parse(link['href'])
  123. mentioned_account = if TagManager.instance.local_domain?(url.host)
  124. Account.find_local(url.path.gsub('/users/', ''))
  125. else
  126. Account.find_by(url: link['href']) || FetchRemoteAccountService.new.call(link['href'])
  127. end
  128. next if mentioned_account.nil? || processed_account_ids.include?(mentioned_account.id)
  129. mention = mentioned_account.mentions.where(status: parent).first_or_create(status: parent)
  130. # Notify local user
  131. NotifyService.new.call(mentioned_account, mention) if mentioned_account.local?
  132. # So we can skip duplicate mentions
  133. processed_account_ids << mentioned_account.id
  134. end
  135. parent.visibility = public_visibility ? :public : :unlisted
  136. parent.save!
  137. end
  138. def hashtags_from_xml(parent, xml)
  139. tags = xml.xpath('./xmlns:category', xmlns: TagManager::XMLNS).map { |category| category['term'] }.select { |t| !t.blank? }
  140. ProcessHashtagsService.new.call(parent, tags)
  141. end
  142. def media_from_xml(parent, xml)
  143. return if DomainBlock.find_by(domain: parent.account.domain)&.reject_media?
  144. xml.xpath('./xmlns:link[@rel="enclosure"]', xmlns: TagManager::XMLNS).each do |link|
  145. next unless link['href']
  146. media = MediaAttachment.where(status: parent, remote_url: link['href']).first_or_initialize(account: parent.account, status: parent, remote_url: link['href'])
  147. begin
  148. media.file_remote_url = link['href']
  149. media.save
  150. rescue OpenURI::HTTPError, Paperclip::Errors::NotIdentifiedByImageMagickError
  151. next
  152. end
  153. end
  154. end
  155. def id(xml = @xml)
  156. xml.at_xpath('./xmlns:id', xmlns: TagManager::XMLNS).content
  157. end
  158. def verb(xml = @xml)
  159. raw = xml.at_xpath('./activity:verb', activity: TagManager::AS_XMLNS).content
  160. TagManager::VERBS.key(raw)
  161. rescue
  162. :post
  163. end
  164. def type(xml = @xml)
  165. raw = xml.at_xpath('./activity:object-type', activity: TagManager::AS_XMLNS).content
  166. TagManager::TYPES.key(raw)
  167. rescue
  168. :activity
  169. end
  170. def url(xml = @xml)
  171. link = xml.at_xpath('./xmlns:link[@rel="alternate"]', xmlns: TagManager::XMLNS)
  172. link.nil? ? nil : link['href']
  173. end
  174. def content(xml = @xml)
  175. xml.at_xpath('./xmlns:content', xmlns: TagManager::XMLNS).content
  176. end
  177. def content_warning(xml = @xml)
  178. xml.at_xpath('./xmlns:summary', xmlns: TagManager::XMLNS)&.content || ''
  179. end
  180. def published(xml = @xml)
  181. xml.at_xpath('./xmlns:published', xmlns: TagManager::XMLNS).content
  182. end
  183. def thread?(xml = @xml)
  184. !xml.at_xpath('./thr:in-reply-to', thr: TagManager::THR_XMLNS).nil?
  185. end
  186. def thread(xml = @xml)
  187. thr = xml.at_xpath('./thr:in-reply-to', thr: TagManager::THR_XMLNS)
  188. [thr['ref'], thr['href']]
  189. end
  190. def account?(xml = @xml)
  191. !xml.at_xpath('./xmlns:author', xmlns: TagManager::XMLNS).nil?
  192. end
  193. def acct(xml = @xml)
  194. username = xml.at_xpath('./xmlns:author/xmlns:name', xmlns: TagManager::XMLNS).content
  195. url = xml.at_xpath('./xmlns:author/xmlns:uri', xmlns: TagManager::XMLNS).content
  196. domain = Addressable::URI.parse(url).host
  197. "#{username}@#{domain}"
  198. end
  199. end
  200. end