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.

103 lines
2.7 KiB

  1. # frozen_string_literal: true
  2. class ResolveURLService < BaseService
  3. include JsonLdHelper
  4. include Authorization
  5. def call(url, on_behalf_of: nil)
  6. @url = url
  7. @on_behalf_of = on_behalf_of
  8. if local_url?
  9. process_local_url
  10. elsif !fetched_resource.nil?
  11. process_url
  12. else
  13. process_url_from_db
  14. end
  15. end
  16. private
  17. def process_url
  18. if equals_or_includes_any?(type, ActivityPub::FetchRemoteAccountService::SUPPORTED_TYPES)
  19. ActivityPub::FetchRemoteAccountService.new.call(resource_url, prefetched_body: body)
  20. elsif equals_or_includes_any?(type, ActivityPub::Activity::Create::SUPPORTED_TYPES + ActivityPub::Activity::Create::CONVERTED_TYPES)
  21. status = FetchRemoteStatusService.new.call(resource_url, body)
  22. authorize_with @on_behalf_of, status, :show? unless status.nil?
  23. status
  24. end
  25. end
  26. def process_url_from_db
  27. return unless @on_behalf_of.present? && [401, 403, 404].include?(fetch_resource_service.response_code)
  28. # It may happen that the resource is a private toot, and thus not fetchable,
  29. # but we can return the toot if we already know about it.
  30. scope = Status.where(uri: @url)
  31. # We don't have an index on `url`, so try guessing the `uri` from `url`
  32. parsed_url = Addressable::URI.parse(@url)
  33. parsed_url.path.match(%r{/@(?<username>#{Account::USERNAME_RE})/(?<status_id>[0-9]+)\Z}) do |matched|
  34. parsed_url.path = "/users/#{matched[:username]}/statuses/#{matched[:status_id]}"
  35. scope = scope.or(Status.where(uri: parsed_url.to_s, url: @url))
  36. end
  37. status = scope.first
  38. authorize_with @on_behalf_of, status, :show? unless status.nil?
  39. status
  40. rescue Mastodon::NotPermittedError
  41. nil
  42. end
  43. def fetched_resource
  44. @fetched_resource ||= fetch_resource_service.call(@url)
  45. end
  46. def fetch_resource_service
  47. @_fetch_resource_service ||= FetchResourceService.new
  48. end
  49. def resource_url
  50. fetched_resource.first
  51. end
  52. def body
  53. fetched_resource.second[:prefetched_body]
  54. end
  55. def type
  56. json_data['type']
  57. end
  58. def json_data
  59. @json_data ||= body_to_json(body)
  60. end
  61. def local_url?
  62. TagManager.instance.local_url?(@url)
  63. end
  64. def process_local_url
  65. recognized_params = Rails.application.routes.recognize_path(@url)
  66. return unless recognized_params[:action] == 'show'
  67. if recognized_params[:controller] == 'statuses'
  68. status = Status.find_by(id: recognized_params[:id])
  69. check_local_status(status)
  70. elsif recognized_params[:controller] == 'accounts'
  71. Account.find_local(recognized_params[:username])
  72. end
  73. end
  74. def check_local_status(status)
  75. return if status.nil?
  76. authorize_with @on_behalf_of, status, :show?
  77. status
  78. rescue Mastodon::NotPermittedError
  79. nil
  80. end
  81. end