A lightly modified copy of Mastodon, the canonical repository for Chinwag Social's live code. https://social.chinwag.org/
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.

process_feed_service.rb 3.2KB

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