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.

137 lines
3.4 KiB

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. unless xml.at_xpath('/xmlns:feed').nil?
  8. update_remote_profile_service.(xml.at_xpath('/xmlns:feed/xmlns:author'), account)
  9. end
  10. xml.xpath('//xmlns:entry').each do |entry|
  11. next unless [:note, :comment, :activity].include? object_type(entry)
  12. status = Status.find_by(uri: activity_id(entry))
  13. next unless status.nil?
  14. status = Status.new(uri: activity_id(entry), account: account, text: content(entry), created_at: published(entry), updated_at: updated(entry))
  15. if object_type(entry) == :comment
  16. add_reply!(entry, status)
  17. elsif verb(entry) == :share
  18. add_reblog!(entry, status)
  19. else
  20. add_post!(entry, status)
  21. end
  22. process_mentions_service.(status) unless status.new_record?
  23. end
  24. end
  25. private
  26. def add_post!(_entry, status)
  27. status.save!
  28. end
  29. def add_reblog!(entry, status)
  30. status.reblog = find_original_status(entry, target_id(entry))
  31. if status.reblog.nil?
  32. status.reblog = fetch_remote_status(entry)
  33. end
  34. status.save! unless status.reblog.nil?
  35. end
  36. def add_reply!(entry, status)
  37. status.thread = find_original_status(entry, thread_id(entry))
  38. status.save!
  39. end
  40. def find_original_status(_xml, id)
  41. return nil if id.nil?
  42. if local_id?(id)
  43. Status.find(unique_tag_to_local_id(id, 'Status'))
  44. else
  45. Status.find_by(uri: id)
  46. end
  47. end
  48. def fetch_remote_status(xml)
  49. username = xml.at_xpath('./activity:object/xmlns:author/xmlns:name').content
  50. url = xml.at_xpath('./activity:object/xmlns:author/xmlns:uri').content
  51. domain = Addressable::URI.parse(url).host
  52. account = Account.find_by(username: username, domain: domain)
  53. if account.nil?
  54. account = follow_remote_account_service.("acct:#{username}@#{domain}", false)
  55. return nil if account.nil?
  56. end
  57. Status.new(account: account, uri: target_id(xml), text: target_content(xml), url: target_url(xml))
  58. end
  59. def published(xml)
  60. xml.at_xpath('./xmlns:published').content
  61. end
  62. def updated(xml)
  63. xml.at_xpath('./xmlns:updated').content
  64. end
  65. def content(xml)
  66. xml.at_xpath('./xmlns:content').content
  67. end
  68. def thread_id(xml)
  69. xml.at_xpath('./thr:in-reply-to').attribute('ref').value
  70. rescue
  71. nil
  72. end
  73. def target_id(xml)
  74. xml.at_xpath('.//activity:object/xmlns:id').content
  75. rescue
  76. nil
  77. end
  78. def activity_id(xml)
  79. xml.at_xpath('./xmlns:id').content
  80. end
  81. def target_content(xml)
  82. xml.at_xpath('.//activity:object/xmlns:content').content
  83. end
  84. def target_url(xml)
  85. xml.at_xpath('.//activity:object/xmlns:link[@rel=alternate]').attribute('href').value
  86. end
  87. def object_type(xml)
  88. xml.at_xpath('./activity:object-type').content.gsub('http://activitystrea.ms/schema/1.0/', '').to_sym
  89. rescue
  90. :note
  91. end
  92. def verb(xml)
  93. xml.at_xpath('./activity:verb').content.gsub('http://activitystrea.ms/schema/1.0/', '').to_sym
  94. rescue
  95. :post
  96. end
  97. def follow_remote_account_service
  98. @follow_remote_account_service ||= FollowRemoteAccountService.new
  99. end
  100. def process_mentions_service
  101. @process_mentions_service ||= ProcessMentionsService.new
  102. end
  103. def update_remote_profile_service
  104. @update_remote_profile_service ||= UpdateRemoteProfileService.new
  105. end
  106. end