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.

81 lines
2.1 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. end
  13. end
  14. private
  15. def process_url
  16. if equals_or_includes_any?(type, ActivityPub::FetchRemoteAccountService::SUPPORTED_TYPES)
  17. ActivityPub::FetchRemoteAccountService.new.call(resource_url, prefetched_body: body)
  18. elsif equals_or_includes_any?(type, ActivityPub::Activity::Create::SUPPORTED_TYPES + ActivityPub::Activity::Create::CONVERTED_TYPES)
  19. status = FetchRemoteStatusService.new.call(resource_url, body)
  20. authorize_with @on_behalf_of, status, :show? unless status.nil?
  21. status
  22. elsif fetched_resource.nil? && @on_behalf_of.present?
  23. # It may happen that the resource is a private toot, and thus not fetchable,
  24. # but we can return the toot if we already know about it.
  25. status = Status.find_by(uri: @url) || Status.find_by(url: @url)
  26. authorize_with @on_behalf_of, status, :show? unless status.nil?
  27. status
  28. end
  29. end
  30. def fetched_resource
  31. @fetched_resource ||= FetchResourceService.new.call(@url)
  32. end
  33. def resource_url
  34. fetched_resource.first
  35. end
  36. def body
  37. fetched_resource.second[:prefetched_body]
  38. end
  39. def type
  40. json_data['type']
  41. end
  42. def json_data
  43. @json_data ||= body_to_json(body)
  44. end
  45. def local_url?
  46. TagManager.instance.local_url?(@url)
  47. end
  48. def process_local_url
  49. recognized_params = Rails.application.routes.recognize_path(@url)
  50. return unless recognized_params[:action] == 'show'
  51. if recognized_params[:controller] == 'statuses'
  52. status = Status.find_by(id: recognized_params[:id])
  53. check_local_status(status)
  54. elsif recognized_params[:controller] == 'accounts'
  55. Account.find_local(recognized_params[:username])
  56. end
  57. end
  58. def check_local_status(status)
  59. return if status.nil?
  60. authorize_with @on_behalf_of, status, :show?
  61. status
  62. rescue Mastodon::NotPermittedError
  63. nil
  64. end
  65. end