闭社主体 forked from https://github.com/tootsuite/mastodon
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.

69 lines
1.5 KiB

  1. # frozen_string_literal: true
  2. class FetchRemoteResourceService < BaseService
  3. attr_reader :url
  4. def call(url)
  5. @url = url
  6. return process_local_url if local_url?
  7. process_url unless fetched_atom_feed.nil?
  8. end
  9. private
  10. def process_url
  11. case xml_root
  12. when 'feed'
  13. FetchRemoteAccountService.new.call(atom_url, body)
  14. when 'entry'
  15. FetchRemoteStatusService.new.call(atom_url, body)
  16. end
  17. end
  18. def fetched_atom_feed
  19. @_fetched_atom_feed ||= FetchAtomService.new.call(url)
  20. end
  21. def atom_url
  22. fetched_atom_feed.first
  23. end
  24. def body
  25. fetched_atom_feed.last
  26. end
  27. def xml_root
  28. xml_data.root.name
  29. end
  30. def xml_data
  31. @_xml_data ||= Nokogiri::XML(body, nil, 'utf-8')
  32. end
  33. def local_url?
  34. TagManager.instance.local_url?(@url)
  35. end
  36. def process_local_url
  37. recognized_params = Rails.application.routes.recognize_path(@url)
  38. return unless recognized_params[:action] == 'show'
  39. if recognized_params[:controller] == 'stream_entries'
  40. status = StreamEntry.find_by(id: recognized_params[:id])&.status
  41. check_local_status(status)
  42. elsif recognized_params[:controller] == 'statuses'
  43. status = Status.find_by(id: recognized_params[:id])
  44. check_local_status(status)
  45. elsif recognized_params[:controller] == 'accounts'
  46. Account.find_local(recognized_params[:username])
  47. end
  48. end
  49. def check_local_status(status)
  50. return if status.nil?
  51. status if status.public_visibility? || status.unlisted_visibility?
  52. end
  53. end