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.

246 lines
7.2 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 = status_from_xml(@xml)
  36. if verb == :share
  37. original_status = status_from_xml(@xml.at_xpath('.//activity:object', activity: TagManager::AS_XMLNS))
  38. status.reblog = original_status
  39. if original_status.nil?
  40. status.destroy
  41. return nil
  42. elsif original_status.reblog?
  43. status.reblog = original_status.reblog
  44. end
  45. end
  46. status.save!
  47. NotifyService.new.call(status.reblog.account, status) if status.reblog? && status.reblog.account.local?
  48. Rails.logger.debug "Queuing remote status #{status.id} (#{id}) for distribution"
  49. DistributionWorker.perform_async(status.id)
  50. status
  51. end
  52. def delete_status
  53. Rails.logger.debug "Deleting remote status #{id}"
  54. status = Status.find_by(uri: id)
  55. RemoveStatusService.new.call(status) unless status.nil?
  56. nil
  57. end
  58. def skip_unsupported_type?
  59. !([:post, :share, :delete].include?(verb) && [:activity, :note, :comment].include?(type))
  60. end
  61. def status_from_xml(entry)
  62. # Return early if status already exists in db
  63. status = find_status(id(entry))
  64. return status unless status.nil?
  65. # If status embeds an author, find that author
  66. # If that author cannot be found, don't record the status (do not misattribute)
  67. if account?(entry)
  68. begin
  69. account = find_or_resolve_account(acct(entry))
  70. return nil if account.nil?
  71. rescue Goldfinger::Error
  72. return nil
  73. end
  74. else
  75. account = @account
  76. end
  77. return if account.suspended?
  78. status = Status.create!(
  79. uri: id(entry),
  80. url: url(entry),
  81. account: account,
  82. text: content(entry),
  83. created_at: published(entry)
  84. )
  85. if thread?(entry)
  86. Rails.logger.debug "Trying to attach #{status.id} (#{id(entry)}) to #{thread(entry).first}"
  87. status.thread = find_or_resolve_status(status, *thread(entry))
  88. end
  89. mentions_from_xml(status, entry)
  90. hashtags_from_xml(status, entry)
  91. media_from_xml(status, entry)
  92. status
  93. end
  94. def find_or_resolve_account(acct)
  95. FollowRemoteAccountService.new.call(acct)
  96. end
  97. def find_or_resolve_status(parent, uri, url)
  98. status = find_status(uri)
  99. ThreadResolveWorker.perform_async(parent.id, url) if status.nil?
  100. status
  101. end
  102. def find_status(uri)
  103. if TagManager.instance.local_id?(uri)
  104. local_id = TagManager.instance.unique_tag_to_local_id(uri, 'Status')
  105. return Status.find(local_id)
  106. end
  107. Status.find_by(uri: uri)
  108. end
  109. def mentions_from_xml(parent, xml)
  110. processed_account_ids = []
  111. public_visibility = false
  112. xml.xpath('./xmlns:link[@rel="mentioned"]', xmlns: TagManager::XMLNS).each do |link|
  113. if link['ostatus:object-type'] == TagManager::TYPES[:collection] && link['href'] == TagManager::COLLECTIONS[:public]
  114. public_visibility = true
  115. next
  116. elsif link['ostatus:object-type'] == TagManager::TYPES[:group]
  117. next
  118. end
  119. url = Addressable::URI.parse(link['href'])
  120. mentioned_account = if TagManager.instance.local_domain?(url.host)
  121. Account.find_local(url.path.gsub('/users/', ''))
  122. else
  123. Account.find_by(url: link['href']) || FetchRemoteAccountService.new.call(link['href'])
  124. end
  125. next if mentioned_account.nil? || processed_account_ids.include?(mentioned_account.id)
  126. mention = mentioned_account.mentions.where(status: parent).first_or_create(status: parent)
  127. # Notify local user
  128. NotifyService.new.call(mentioned_account, mention) if mentioned_account.local?
  129. # So we can skip duplicate mentions
  130. processed_account_ids << mentioned_account.id
  131. end
  132. parent.visibility = public_visibility ? :public : :unlisted
  133. parent.save!
  134. end
  135. def hashtags_from_xml(parent, xml)
  136. tags = xml.xpath('./xmlns:category', xmlns: TagManager::XMLNS).map { |category| category['term'] }.select { |t| !t.blank? }
  137. ProcessHashtagsService.new.call(parent, tags)
  138. end
  139. def media_from_xml(parent, xml)
  140. xml.xpath('./xmlns:link[@rel="enclosure"]', xmlns: TagManager::XMLNS).each do |link|
  141. next unless link['href']
  142. media = MediaAttachment.where(status: parent, remote_url: link['href']).first_or_initialize(account: parent.account, status: parent, remote_url: link['href'])
  143. begin
  144. media.file_remote_url = link['href']
  145. media.save
  146. rescue OpenURI::HTTPError, Paperclip::Errors::NotIdentifiedByImageMagickError
  147. next
  148. end
  149. end
  150. end
  151. def id(xml = @xml)
  152. xml.at_xpath('./xmlns:id', xmlns: TagManager::XMLNS).content
  153. end
  154. def verb(xml = @xml)
  155. raw = xml.at_xpath('./activity:verb', activity: TagManager::AS_XMLNS).content
  156. TagManager::VERBS.key(raw)
  157. rescue
  158. :post
  159. end
  160. def type(xml = @xml)
  161. raw = xml.at_xpath('./activity:object-type', activity: TagManager::AS_XMLNS).content
  162. TagManager::TYPES.key(raw)
  163. rescue
  164. :activity
  165. end
  166. def url(xml = @xml)
  167. link = xml.at_xpath('./xmlns:link[@rel="alternate"]', xmlns: TagManager::XMLNS)
  168. link.nil? ? nil : link['href']
  169. end
  170. def content(xml = @xml)
  171. xml.at_xpath('./xmlns:content', xmlns: TagManager::XMLNS).content
  172. end
  173. def published(xml = @xml)
  174. xml.at_xpath('./xmlns:published', xmlns: TagManager::XMLNS).content
  175. end
  176. def thread?(xml = @xml)
  177. !xml.at_xpath('./thr:in-reply-to', thr: TagManager::THR_XMLNS).nil?
  178. end
  179. def thread(xml = @xml)
  180. thr = xml.at_xpath('./thr:in-reply-to', thr: TagManager::THR_XMLNS)
  181. [thr['ref'], thr['href']]
  182. end
  183. def account?(xml = @xml)
  184. !xml.at_xpath('./xmlns:author', xmlns: TagManager::XMLNS).nil?
  185. end
  186. def acct(xml = @xml)
  187. username = xml.at_xpath('./xmlns:author/xmlns:name', xmlns: TagManager::XMLNS).content
  188. url = xml.at_xpath('./xmlns:author/xmlns:uri', xmlns: TagManager::XMLNS).content
  189. domain = Addressable::URI.parse(url).host
  190. "#{username}@#{domain}"
  191. end
  192. end
  193. end