1
0

jsonld_helper.rb 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. on_behalf_of ||= Account.representative
  63. build_request(uri, on_behalf_of).perform do |response|
  64. raise Mastodon::UnexpectedResponseError, response unless response_successful?(response) || response_error_unsalvageable?(response) || !raise_on_temporary_error
  65. body_to_json(response.body_with_limit) if response.code == 200
  66. end
  67. end
  68. def body_to_json(body, compare_id: nil)
  69. json = body.is_a?(String) ? Oj.load(body, mode: :strict) : body
  70. return if compare_id.present? && json['id'] != compare_id
  71. json
  72. rescue Oj::ParseError
  73. nil
  74. end
  75. def merge_context(context, new_context)
  76. if context.is_a?(Array)
  77. context << new_context
  78. else
  79. [context, new_context]
  80. end
  81. end
  82. def response_successful?(response)
  83. (200...300).cover?(response.code)
  84. end
  85. def response_error_unsalvageable?(response)
  86. response.code == 501 || ((400...500).cover?(response.code) && ![401, 408, 429].include?(response.code))
  87. end
  88. def build_request(uri, on_behalf_of = nil)
  89. Request.new(:get, uri).tap do |request|
  90. request.on_behalf_of(on_behalf_of) if on_behalf_of
  91. request.add_headers('Accept' => 'application/activity+json, application/ld+json')
  92. end
  93. end
  94. def load_jsonld_context(url, _options = {}, &_block)
  95. json = Rails.cache.fetch("jsonld:context:#{url}", expires_in: 30.days, raw: true) do
  96. request = Request.new(:get, url)
  97. request.add_headers('Accept' => 'application/ld+json')
  98. request.perform do |res|
  99. raise JSON::LD::JsonLdError::LoadingDocumentFailed unless res.code == 200 && res.mime_type == 'application/ld+json'
  100. res.body_with_limit
  101. end
  102. end
  103. doc = JSON::LD::API::RemoteDocument.new(url, json)
  104. block_given? ? yield(doc) : doc
  105. end
  106. end