The code powering m.abunchtell.com https://m.abunchtell.com
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

86 righe
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. 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. 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 protocol
  40. fetched_resource.third
  41. end
  42. def type
  43. return json_data['type'] if protocol == :activitypub
  44. end
  45. def json_data
  46. @json_data ||= body_to_json(body)
  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] == 'statuses'
  55. status = Status.find_by(id: recognized_params[:id])
  56. check_local_status(status)
  57. elsif recognized_params[:controller] == 'accounts'
  58. Account.find_local(recognized_params[:username])
  59. end
  60. end
  61. def check_local_status(status)
  62. return if status.nil?
  63. authorize_with @on_behalf_of, status, :show?
  64. status
  65. rescue Mastodon::NotPermittedError
  66. nil
  67. end
  68. end