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.

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