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.

144 lines
4.2 KiB

  1. # frozen_string_literal: true
  2. module JsonLdHelper
  3. def equals_or_includes?(haystack, needle)
  4. haystack.is_a?(Array) ? haystack.include?(needle) : haystack == needle
  5. end
  6. def equals_or_includes_any?(haystack, needles)
  7. needles.any? { |needle| equals_or_includes?(haystack, needle) }
  8. end
  9. def first_of_value(value)
  10. value.is_a?(Array) ? value.first : value
  11. end
  12. # The url attribute can be a string, an array of strings, or an array of objects.
  13. # The objects could include a mimeType. Not-included mimeType means it's text/html.
  14. def url_to_href(value, preferred_type = nil)
  15. single_value = begin
  16. if value.is_a?(Array) && !value.first.is_a?(String)
  17. value.find { |link| preferred_type.nil? || ((link['mimeType'].presence || 'text/html') == preferred_type) }
  18. elsif value.is_a?(Array)
  19. value.first
  20. else
  21. value
  22. end
  23. end
  24. if single_value.nil? || single_value.is_a?(String)
  25. single_value
  26. else
  27. single_value['href']
  28. end
  29. end
  30. def as_array(value)
  31. value.is_a?(Array) ? value : [value]
  32. end
  33. def value_or_id(value)
  34. value.is_a?(String) || value.nil? ? value : value['id']
  35. end
  36. def supported_context?(json)
  37. !json.nil? && equals_or_includes?(json['@context'], ActivityPub::TagManager::CONTEXT)
  38. end
  39. def unsupported_uri_scheme?(uri)
  40. !uri.start_with?('http://', 'https://')
  41. end
  42. def invalid_origin?(url)
  43. return true if unsupported_uri_scheme?(url)
  44. needle = Addressable::URI.parse(url).host
  45. haystack = Addressable::URI.parse(@account.uri).host
  46. !haystack.casecmp(needle).zero?
  47. end
  48. def canonicalize(json)
  49. graph = RDF::Graph.new << JSON::LD::API.toRdf(json, documentLoader: method(:load_jsonld_context))
  50. graph.dump(:normalize)
  51. end
  52. def fetch_resource(uri, id, on_behalf_of = nil)
  53. unless id
  54. json = fetch_resource_without_id_validation(uri, on_behalf_of)
  55. return unless json
  56. uri = json['id']
  57. end
  58. json = fetch_resource_without_id_validation(uri, on_behalf_of)
  59. json.present? && json['id'] == uri ? json : nil
  60. end
  61. def fetch_resource_without_id_validation(uri, on_behalf_of = nil, raise_on_temporary_error = false)
  62. build_request(uri, on_behalf_of).perform do |response|
  63. raise Mastodon::UnexpectedResponseError, response unless response_successful?(response) || response_error_unsalvageable?(response) || !raise_on_temporary_error
  64. return body_to_json(response.body_with_limit) if response.code == 200
  65. end
  66. # If request failed, retry without doing it on behalf of a user
  67. return if on_behalf_of.nil?
  68. build_request(uri).perform do |response|
  69. raise Mastodon::UnexpectedResponseError, response unless response_successful?(response) || response_error_unsalvageable?(response) || !raise_on_temporary_error
  70. response.code == 200 ? body_to_json(response.body_with_limit) : nil
  71. end
  72. end
  73. def body_to_json(body, compare_id: nil)
  74. json = body.is_a?(String) ? Oj.load(body, mode: :strict) : body
  75. return if compare_id.present? && json['id'] != compare_id
  76. json
  77. rescue Oj::ParseError
  78. nil
  79. end
  80. def merge_context(context, new_context)
  81. if context.is_a?(Array)
  82. context << new_context
  83. else
  84. [context, new_context]
  85. end
  86. end
  87. def response_successful?(response)
  88. (200...300).cover?(response.code)
  89. end
  90. def response_error_unsalvageable?(response)
  91. response.code == 501 || ((400...500).cover?(response.code) && ![401, 408, 429].include?(response.code))
  92. end
  93. def build_request(uri, on_behalf_of = nil)
  94. Request.new(:get, uri).tap do |request|
  95. request.on_behalf_of(on_behalf_of) if on_behalf_of
  96. request.add_headers('Accept' => 'application/activity+json, application/ld+json')
  97. end
  98. end
  99. def load_jsonld_context(url, _options = {}, &_block)
  100. json = Rails.cache.fetch("jsonld:context:#{url}", expires_in: 30.days, raw: true) do
  101. request = Request.new(:get, url)
  102. request.add_headers('Accept' => 'application/ld+json')
  103. request.perform do |res|
  104. raise JSON::LD::JsonLdError::LoadingDocumentFailed unless res.code == 200 && res.mime_type == 'application/ld+json'
  105. res.body_with_limit
  106. end
  107. end
  108. doc = JSON::LD::API::RemoteDocument.new(url, json)
  109. block_given? ? yield(doc) : doc
  110. end
  111. end