The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

88 lines
2.2 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. elsif @on_behalf_of.present?
  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. # It may happen that the resource is a private toot, and thus not fetchable,
  28. # but we can return the toot if we already know about it.
  29. status = Status.find_by(uri: @url) || Status.find_by(url: @url)
  30. authorize_with @on_behalf_of, status, :show? unless status.nil?
  31. status
  32. rescue Mastodon::NotPermittedError
  33. nil
  34. end
  35. def fetched_resource
  36. @fetched_resource ||= FetchResourceService.new.call(@url)
  37. end
  38. def resource_url
  39. fetched_resource.first
  40. end
  41. def body
  42. fetched_resource.second[:prefetched_body]
  43. end
  44. def type
  45. json_data['type']
  46. end
  47. def json_data
  48. @json_data ||= body_to_json(body)
  49. end
  50. def local_url?
  51. TagManager.instance.local_url?(@url)
  52. end
  53. def process_local_url
  54. recognized_params = Rails.application.routes.recognize_path(@url)
  55. return unless recognized_params[:action] == 'show'
  56. if recognized_params[:controller] == 'statuses'
  57. status = Status.find_by(id: recognized_params[:id])
  58. check_local_status(status)
  59. elsif recognized_params[:controller] == 'accounts'
  60. Account.find_local(recognized_params[:username])
  61. end
  62. end
  63. def check_local_status(status)
  64. return if status.nil?
  65. authorize_with @on_behalf_of, status, :show?
  66. status
  67. rescue Mastodon::NotPermittedError
  68. nil
  69. end
  70. end