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.

193 lines
5.6 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. class ProcessFeedService < BaseService
  2. # Create local statuses from an Atom feed
  3. # @param [String] body Atom feed
  4. # @param [Account] account Account this feed belongs to
  5. def call(body, account)
  6. xml = Nokogiri::XML(body)
  7. update_remote_profile_service.(xml.at_xpath('/xmlns:feed/xmlns:author'), account) unless xml.at_xpath('/xmlns:feed').nil?
  8. xml.xpath('//xmlns:entry').each { |entry| process_entry(account, entry) }
  9. end
  10. private
  11. def process_entry(account, entry)
  12. return unless [:note, :comment, :activity].include? object_type(entry)
  13. status = Status.find_by(uri: activity_id(entry))
  14. # If we already have a post and the verb is now "delete", we gotta delete it and move on!
  15. if !status.nil? && verb(entry) == :delete
  16. delete_post!(status)
  17. return
  18. end
  19. return unless status.nil?
  20. status = Status.new(uri: activity_id(entry), url: activity_link(entry), account: account, text: content(entry), created_at: published(entry), updated_at: updated(entry))
  21. if verb(entry) == :share
  22. add_reblog!(entry, status)
  23. elsif verb(entry) == :post
  24. if thread_id(entry).nil?
  25. add_post!(entry, status)
  26. else
  27. add_reply!(entry, status)
  28. end
  29. end
  30. # If we added a status, go through accounts it mentions and create respective relations
  31. unless status.new_record?
  32. record_remote_mentions(status, entry.xpath('./xmlns:link[@rel="mentioned"]'))
  33. process_attachments(entry, status)
  34. DistributionWorker.perform_async(status.id)
  35. end
  36. end
  37. def record_remote_mentions(status, links)
  38. # Here we have to do a reverse lookup of local accounts by their URL!
  39. # It's not pretty at all! I really wish all these protocols sticked to
  40. # using acct:username@domain only! It would make things so much easier
  41. # and tidier
  42. links.each do |mention_link|
  43. href = Addressable::URI.parse(mention_link.attribute('href').value)
  44. if href.host == Rails.configuration.x.local_domain
  45. # A local user is mentioned
  46. mentioned_account = Account.find_local(href.path.gsub('/users/', ''))
  47. unless mentioned_account.nil?
  48. mentioned_account.mentions.where(status: status).first_or_create(status: status)
  49. NotificationMailer.mention(mentioned_account, status).deliver_later
  50. end
  51. else
  52. # What to do about remote user?
  53. # Are we supposed to do a search in the database by URL?
  54. # We could technically open the URL, look for LRDD tags, get webfinger that way,
  55. # finally acquire the acct:username@domain form, and then check DB
  56. end
  57. end
  58. end
  59. def process_attachments(entry, status)
  60. entry.xpath('./xmlns:link[@rel="enclosure"]').each do |enclosure_link|
  61. next if enclosure_link.attribute('href').nil?
  62. media = MediaAttachment.new(account: status.account, status: status, remote_url: enclosure_link.attribute('href').value)
  63. media.file_remote_url = enclosure_link.attribute('href').value
  64. media.save
  65. end
  66. end
  67. def add_post!(_entry, status)
  68. status.save!
  69. end
  70. def add_reblog!(entry, status)
  71. status.reblog = find_original_status(entry, target_id(entry))
  72. if status.reblog.nil?
  73. status.reblog = fetch_remote_status(entry)
  74. end
  75. if !status.reblog.nil?
  76. status.save!
  77. NotificationMailer.reblog(status.reblog, status.account).deliver_later if status.reblog.local?
  78. end
  79. end
  80. def add_reply!(entry, status)
  81. status.thread = find_original_status(entry, thread_id(entry))
  82. status.save!
  83. end
  84. def delete_post!(status)
  85. RemoveStatusService.new.(status)
  86. end
  87. def find_original_status(_xml, id)
  88. return nil if id.nil?
  89. if local_id?(id)
  90. Status.find(unique_tag_to_local_id(id, 'Status'))
  91. else
  92. Status.find_by(uri: id)
  93. end
  94. end
  95. def fetch_remote_status(xml)
  96. username = xml.at_xpath('./activity:object/xmlns:author/xmlns:name').content
  97. url = xml.at_xpath('./activity:object/xmlns:author/xmlns:uri').content
  98. domain = Addressable::URI.parse(url).host
  99. account = Account.find_by(username: username, domain: domain)
  100. if account.nil?
  101. account = follow_remote_account_service.("#{username}@#{domain}", false)
  102. return nil if account.nil?
  103. end
  104. Status.new(account: account, uri: target_id(xml), text: target_content(xml), url: target_url(xml))
  105. end
  106. def published(xml)
  107. xml.at_xpath('./xmlns:published').content
  108. end
  109. def updated(xml)
  110. xml.at_xpath('./xmlns:updated').content
  111. end
  112. def content(xml)
  113. xml.at_xpath('./xmlns:content').content
  114. end
  115. def thread_id(xml)
  116. xml.at_xpath('./thr:in-reply-to').attribute('ref').value
  117. rescue
  118. nil
  119. end
  120. def target_id(xml)
  121. xml.at_xpath('.//activity:object/xmlns:id').content
  122. rescue
  123. nil
  124. end
  125. def activity_id(xml)
  126. xml.at_xpath('./xmlns:id').content
  127. end
  128. def activity_link(xml)
  129. xml.at_xpath('./xmlns:link[@rel="alternate"]').attribute('href').value
  130. rescue
  131. ''
  132. end
  133. def target_content(xml)
  134. xml.at_xpath('.//activity:object/xmlns:content').content
  135. end
  136. def target_url(xml)
  137. xml.at_xpath('.//activity:object/xmlns:link[@rel="alternate"]').attribute('href').value
  138. end
  139. def object_type(xml)
  140. xml.at_xpath('./activity:object-type').content.gsub('http://activitystrea.ms/schema/1.0/', '').to_sym
  141. rescue
  142. :note
  143. end
  144. def verb(xml)
  145. xml.at_xpath('./activity:verb').content.gsub('http://activitystrea.ms/schema/1.0/', '').to_sym
  146. rescue
  147. :post
  148. end
  149. def follow_remote_account_service
  150. @follow_remote_account_service ||= FollowRemoteAccountService.new
  151. end
  152. def update_remote_profile_service
  153. @update_remote_profile_service ||= UpdateRemoteProfileService.new
  154. end
  155. end