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.

258 lines
7.7 KiB

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