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.

111 lines
2.5 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. status.save! unless status.reblog.nil?
  28. end
  29. def add_reply!(entry, status)
  30. status.thread = find_original_status(entry, thread_id(entry))
  31. status.save! unless status.thread.nil?
  32. end
  33. def find_original_status(xml, id)
  34. return nil if id.nil?
  35. if local_id?(id)
  36. Status.find(unique_tag_to_local_id(id, 'Status'))
  37. else
  38. status = Status.find_by(uri: id)
  39. if status.nil?
  40. status = fetch_remote_status(xml, id)
  41. end
  42. status
  43. end
  44. end
  45. def fetch_remote_status(xml, id)
  46. url = xml.at_xpath('./link[@rel="self"]').attribute('href').value
  47. nil
  48. end
  49. def local_id?(id)
  50. id.start_with?("tag:#{LOCAL_DOMAIN}")
  51. end
  52. def published(xml)
  53. xml.at_xpath('./xmlns:published').content
  54. end
  55. def updated(xml)
  56. xml.at_xpath('./xmlns:updated').content
  57. end
  58. def content(xml)
  59. xml.at_xpath('./xmlns:content').content
  60. end
  61. def thread_id(xml)
  62. xml.at_xpath('./thr:in-reply-to-id').attribute('ref').value
  63. rescue
  64. nil
  65. end
  66. def target_id(xml)
  67. xml.at_xpath('./activity:object/xmlns:id').content
  68. rescue
  69. nil
  70. end
  71. def activity_id(xml)
  72. entry.at_xpath('./xmlns:id').content
  73. end
  74. def object_type(xml)
  75. xml.at_xpath('./activity:object-type').content.gsub('http://activitystrea.ms/schema/1.0/', '').to_sym
  76. rescue
  77. :note
  78. end
  79. def verb(xml)
  80. xml.at_xpath('./activity:verb').content.gsub('http://activitystrea.ms/schema/1.0/', '').to_sym
  81. rescue
  82. :post
  83. end
  84. def follow_remote_account_service
  85. @follow_remote_account_service ||= FollowRemoteAccountService.new
  86. end
  87. end