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.

234 lines
6.6 KiB

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