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.

139 lines
4.3 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 = if value.is_a?(Array) && !value.first.is_a?(String)
  16. value.find { |link| preferred_type.nil? || ((link['mimeType'].presence || 'text/html') == preferred_type) }
  17. elsif value.is_a?(Array)
  18. value.first
  19. else
  20. value
  21. end
  22. if single_value.nil? || single_value.is_a?(String)
  23. single_value
  24. else
  25. single_value['href']
  26. end
  27. end
  28. def as_array(value)
  29. value.is_a?(Array) ? value : [value]
  30. end
  31. def value_or_id(value)
  32. value.is_a?(String) || value.nil? ? value : value['id']
  33. end
  34. def supported_context?(json)
  35. !json.nil? && equals_or_includes?(json['@context'], ActivityPub::TagManager::CONTEXT)
  36. end
  37. def unsupported_uri_scheme?(uri)
  38. !uri.start_with?('http://', 'https://')
  39. end
  40. def invalid_origin?(url)
  41. return true if unsupported_uri_scheme?(url)
  42. needle = Addressable::URI.parse(url).host
  43. haystack = Addressable::URI.parse(@account.uri).host
  44. !haystack.casecmp(needle).zero?
  45. end
  46. def canonicalize(json)
  47. graph = RDF::Graph.new << JSON::LD::API.toRdf(json, documentLoader: method(:load_jsonld_context))
  48. graph.dump(:normalize)
  49. end
  50. def fetch_resource(uri, id, on_behalf_of = nil)
  51. unless id
  52. json = fetch_resource_without_id_validation(uri, on_behalf_of)
  53. return unless json
  54. uri = json['id']
  55. end
  56. json = fetch_resource_without_id_validation(uri, on_behalf_of)
  57. json.present? && json['id'] == uri ? json : nil
  58. end
  59. def fetch_resource_without_id_validation(uri, on_behalf_of = nil, raise_on_temporary_error = false)
  60. build_request(uri, on_behalf_of).perform do |response|
  61. unless response_successful?(response) || response_error_unsalvageable?(response) || !raise_on_temporary_error
  62. raise Mastodon::UnexpectedResponseError, response
  63. end
  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. unless response_successful?(response) || response_error_unsalvageable?(response) || !raise_on_temporary_error
  70. raise Mastodon::UnexpectedResponseError, response
  71. end
  72. response.code == 200 ? body_to_json(response.body_with_limit) : nil
  73. end
  74. end
  75. def body_to_json(body, compare_id: nil)
  76. json = body.is_a?(String) ? Oj.load(body, mode: :strict) : body
  77. return if compare_id.present? && json['id'] != compare_id
  78. json
  79. rescue Oj::ParseError
  80. nil
  81. end
  82. def merge_context(context, new_context)
  83. if context.is_a?(Array)
  84. context << new_context
  85. else
  86. [context, new_context]
  87. end
  88. end
  89. private
  90. def response_successful?(response)
  91. (200...300).cover?(response.code)
  92. end
  93. def response_error_unsalvageable?(response)
  94. (400...500).cover?(response.code) && response.code != 429
  95. end
  96. def build_request(uri, on_behalf_of = nil)
  97. request = Request.new(:get, uri)
  98. request.on_behalf_of(on_behalf_of) if on_behalf_of
  99. request.add_headers('Accept' => 'application/activity+json, application/ld+json')
  100. request
  101. end
  102. def load_jsonld_context(url, _options = {}, &_block)
  103. json = Rails.cache.fetch("jsonld:context:#{url}", expires_in: 30.days, raw: true) do
  104. request = Request.new(:get, url)
  105. request.add_headers('Accept' => 'application/ld+json')
  106. request.perform do |res|
  107. raise JSON::LD::JsonLdError::LoadingDocumentFailed unless res.code == 200 && res.mime_type == 'application/ld+json'
  108. res.body_with_limit
  109. end
  110. end
  111. doc = JSON::LD::API::RemoteDocument.new(url, json)
  112. block_given? ? yield(doc) : doc
  113. end
  114. end