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.

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