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.

89 lines
1.9 KiB

  1. # frozen_string_literal: true
  2. class ResolveURLService < BaseService
  3. include JsonLdHelper
  4. attr_reader :url
  5. def call(url)
  6. @url = url
  7. return process_local_url if local_url?
  8. process_url unless fetched_atom_feed.nil?
  9. end
  10. private
  11. def process_url
  12. if equals_or_includes_any?(type, %w(Application Group Organization Person Service))
  13. FetchRemoteAccountService.new.call(atom_url, body, protocol)
  14. elsif equals_or_includes_any?(type, %w(Note Article Image Video))
  15. FetchRemoteStatusService.new.call(atom_url, body, protocol)
  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.second[:prefetched_body]
  26. end
  27. def protocol
  28. fetched_atom_feed.third
  29. end
  30. def type
  31. return json_data['type'] if protocol == :activitypub
  32. case xml_root
  33. when 'feed'
  34. 'Person'
  35. when 'entry'
  36. 'Note'
  37. end
  38. end
  39. def json_data
  40. @_json_data ||= body_to_json(body)
  41. end
  42. def xml_root
  43. xml_data.root.name
  44. end
  45. def xml_data
  46. @_xml_data ||= Nokogiri::XML(body, nil, 'utf-8')
  47. end
  48. def local_url?
  49. TagManager.instance.local_url?(@url)
  50. end
  51. def process_local_url
  52. recognized_params = Rails.application.routes.recognize_path(@url)
  53. return unless recognized_params[:action] == 'show'
  54. if recognized_params[:controller] == 'stream_entries'
  55. status = StreamEntry.find_by(id: recognized_params[:id])&.status
  56. check_local_status(status)
  57. elsif recognized_params[:controller] == 'statuses'
  58. status = Status.find_by(id: recognized_params[:id])
  59. check_local_status(status)
  60. elsif recognized_params[:controller] == 'accounts'
  61. Account.find_local(recognized_params[:username])
  62. end
  63. end
  64. def check_local_status(status)
  65. return if status.nil?
  66. status if status.public_visibility? || status.unlisted_visibility?
  67. end
  68. end