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.

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