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.

182 lines
5.2 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. DistributionWorker.perform_async(status.id)
  34. end
  35. end
  36. def record_remote_mentions(status, links)
  37. # Here we have to do a reverse lookup of local accounts by their URL!
  38. # It's not pretty at all! I really wish all these protocols sticked to
  39. # using acct:username@domain only! It would make things so much easier
  40. # and tidier
  41. links.each do |mention_link|
  42. href = Addressable::URI.parse(mention_link.attribute('href').value)
  43. if href.host == Rails.configuration.x.local_domain
  44. # A local user is mentioned
  45. mentioned_account = Account.find_local(href.path.gsub('/users/', ''))
  46. unless mentioned_account.nil?
  47. mentioned_account.mentions.where(status: status).first_or_create(status: status)
  48. NotificationMailer.mention(mentioned_account, status).deliver_later
  49. end
  50. else
  51. # What to do about remote user?
  52. # Are we supposed to do a search in the database by URL?
  53. # We could technically open the URL, look for LRDD tags, get webfinger that way,
  54. # finally acquire the acct:username@domain form, and then check DB
  55. end
  56. end
  57. end
  58. def add_post!(_entry, status)
  59. status.save!
  60. end
  61. def add_reblog!(entry, status)
  62. status.reblog = find_original_status(entry, target_id(entry))
  63. if status.reblog.nil?
  64. status.reblog = fetch_remote_status(entry)
  65. end
  66. if !status.reblog.nil?
  67. status.save!
  68. NotificationMailer.reblog(status.reblog, status.account).deliver_later if status.reblog.local?
  69. end
  70. end
  71. def add_reply!(entry, status)
  72. status.thread = find_original_status(entry, thread_id(entry))
  73. status.save!
  74. end
  75. def delete_post!(status)
  76. RemoveStatusService.new.(status)
  77. end
  78. def find_original_status(_xml, id)
  79. return nil if id.nil?
  80. if local_id?(id)
  81. Status.find(unique_tag_to_local_id(id, 'Status'))
  82. else
  83. Status.find_by(uri: id)
  84. end
  85. end
  86. def fetch_remote_status(xml)
  87. username = xml.at_xpath('./activity:object/xmlns:author/xmlns:name').content
  88. url = xml.at_xpath('./activity:object/xmlns:author/xmlns:uri').content
  89. domain = Addressable::URI.parse(url).host
  90. account = Account.find_by(username: username, domain: domain)
  91. if account.nil?
  92. account = follow_remote_account_service.("#{username}@#{domain}", false)
  93. return nil if account.nil?
  94. end
  95. Status.new(account: account, uri: target_id(xml), text: target_content(xml), url: target_url(xml))
  96. end
  97. def published(xml)
  98. xml.at_xpath('./xmlns:published').content
  99. end
  100. def updated(xml)
  101. xml.at_xpath('./xmlns:updated').content
  102. end
  103. def content(xml)
  104. xml.at_xpath('./xmlns:content').content
  105. end
  106. def thread_id(xml)
  107. xml.at_xpath('./thr:in-reply-to').attribute('ref').value
  108. rescue
  109. nil
  110. end
  111. def target_id(xml)
  112. xml.at_xpath('.//activity:object/xmlns:id').content
  113. rescue
  114. nil
  115. end
  116. def activity_id(xml)
  117. xml.at_xpath('./xmlns:id').content
  118. end
  119. def activity_link(xml)
  120. xml.at_xpath('./xmlns:link[@rel="alternate"]').attribute('href').value
  121. rescue
  122. ''
  123. end
  124. def target_content(xml)
  125. xml.at_xpath('.//activity:object/xmlns:content').content
  126. end
  127. def target_url(xml)
  128. xml.at_xpath('.//activity:object/xmlns:link[@rel="alternate"]').attribute('href').value
  129. end
  130. def object_type(xml)
  131. xml.at_xpath('./activity:object-type').content.gsub('http://activitystrea.ms/schema/1.0/', '').to_sym
  132. rescue
  133. :note
  134. end
  135. def verb(xml)
  136. xml.at_xpath('./activity:verb').content.gsub('http://activitystrea.ms/schema/1.0/', '').to_sym
  137. rescue
  138. :post
  139. end
  140. def follow_remote_account_service
  141. @follow_remote_account_service ||= FollowRemoteAccountService.new
  142. end
  143. def update_remote_profile_service
  144. @update_remote_profile_service ||= UpdateRemoteProfileService.new
  145. end
  146. end