resolve_url_service.rb 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. FetchRemoteAccountService.new.call(resource_url, body, protocol)
  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, protocol)
  20. authorize_with @on_behalf_of, status, :show? unless status.nil?
  21. status
  22. end
  23. end
  24. def fetched_resource
  25. @fetched_resource ||= FetchResourceService.new.call(@url)
  26. end
  27. def resource_url
  28. fetched_resource.first
  29. end
  30. def body
  31. fetched_resource.second[:prefetched_body]
  32. end
  33. def protocol
  34. fetched_resource.third
  35. end
  36. def type
  37. return json_data['type'] if protocol == :activitypub
  38. end
  39. def json_data
  40. @json_data ||= body_to_json(body)
  41. end
  42. def local_url?
  43. TagManager.instance.local_url?(@url)
  44. end
  45. def process_local_url
  46. recognized_params = Rails.application.routes.recognize_path(@url)
  47. return unless recognized_params[:action] == 'show'
  48. if recognized_params[:controller] == 'statuses'
  49. status = Status.find_by(id: recognized_params[:id])
  50. check_local_status(status)
  51. elsif recognized_params[:controller] == 'accounts'
  52. Account.find_local(recognized_params[:username])
  53. end
  54. end
  55. def check_local_status(status)
  56. return if status.nil?
  57. authorize_with @on_behalf_of, status, :show?
  58. status
  59. rescue Mastodon::NotPermittedError
  60. nil
  61. end
  62. end