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.

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