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.

110 lines
2.3 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. class ProcessFeedService
  2. include ApplicationHelper
  3. def call(body, account)
  4. xml = Nokogiri::XML(body)
  5. xml.xpath('//xmlns:entry').each do |entry|
  6. next unless [:note, :comment, :activity].includes? object_type(entry)
  7. status = Status.find_by(uri: activity_id(entry))
  8. next unless status.nil?
  9. status = Status.new(uri: activity_id(entry), account: account, text: content(entry), created_at: published(entry), updated_at: updated(entry))
  10. if object_type(entry) == :comment
  11. add_reply!(entry, status)
  12. elsif verb(entry) == :share
  13. add_reblog!(entry, status)
  14. else
  15. add_post!(entry, status)
  16. end
  17. end
  18. end
  19. private
  20. def add_post!(entry, status)
  21. status.save!
  22. end
  23. def add_reblog!(entry, status)
  24. status.reblog = find_original_status(entry, target_id(entry))
  25. status.save! unless status.reblog.nil?
  26. end
  27. def add_reply!(entry, status)
  28. status.thread = find_original_status(entry, thread_id(entry))
  29. status.save! unless status.thread.nil?
  30. end
  31. def find_original_status(xml, id)
  32. return nil if id.nil?
  33. if local_id?(id)
  34. Status.find(unique_tag_to_local_id(id, 'Status'))
  35. else
  36. status = Status.find_by(uri: id)
  37. if status.nil?
  38. status = fetch_remote_status(xml, id)
  39. end
  40. status
  41. end
  42. end
  43. def fetch_remote_status(xml, id)
  44. url = xml.at_xpath('./link[@rel="self"]').attribute('href').value
  45. nil
  46. end
  47. def local_id?(id)
  48. id.start_with?("tag:#{LOCAL_DOMAIN}")
  49. end
  50. def published(xml)
  51. xml.at_xpath('./xmlns:published').content
  52. end
  53. def updated(xml)
  54. xml.at_xpath('./xmlns:updated').content
  55. end
  56. def content(xml)
  57. xml.at_xpath('./xmlns:content').content
  58. end
  59. def thread_id(xml)
  60. xml.at_xpath('./thr:in-reply-to-id').attribute('ref').value
  61. rescue
  62. nil
  63. end
  64. def target_id(xml)
  65. xml.at_xpath('./activity:object/xmlns:id').content
  66. rescue
  67. nil
  68. end
  69. def activity_id(xml)
  70. entry.at_xpath('./xmlns:id').content
  71. end
  72. def object_type(xml)
  73. xml.at_xpath('./activity:object-type').content.gsub('http://activitystrea.ms/schema/1.0/', '').to_sym
  74. rescue
  75. :note
  76. end
  77. def verb(xml)
  78. xml.at_xpath('./activity:verb').content.gsub('http://activitystrea.ms/schema/1.0/', '').to_sym
  79. rescue
  80. :post
  81. end
  82. def follow_remote_account_service
  83. FollowRemoteAccountService.new
  84. end
  85. end