mirror of
https://github.com/discourse/discourse.git
synced 2026-08-08 04:04:59 -05:00
Add support for JSON LD in Onebox (#17007)
* FIX: Fix a bug that is accessing the values in a hash wrongly and write tests
I decided to write tests in order to be confident in my refactor that's in the next commit.
Meanwhile I have discovered a potential bug. The `title_attr` key was accessed as a string,
but all the keys are actually symbols so it was never evaluated to be true.
irb(main):025:0> d = {key: 'value'}
=> {:key=>"value"}
irb(main):026:0> d['key']
=> nil
irb(main):027:0> d[:key]
=> "value"
* DEV: Extract methods for readability
I will be adding a new method following the conventions in place for adding a new normalizer. And this will make the readability of the `raw` block even more difficult; so I am extracting self contained private methods beforehand.
* FEATURE: Parse JSON-LD and introduce Movie object
JSON LD data is very easily transferable to Ruby objects because they contain types. If these types are mapped to Ruby objects, it is also better to make all the parsed data very explicit and easily extendable.
JSON-LD has many more standardized item types, with a full list here: https://schema.org/docs/full.html
However in order to decrease the scope, I only adapted the movie type.
* DEV: Change inheritance between normalizers
Normalizers are not supposed to have an inheritance relationships amongst each other. They are all normalizers, but all normalizing separate protocols. This is why I chose to extract a parent class and relieve Open Graph off that responsibility. Removing the parent class altogether could also a possibility, but I am keeping the scope limited to having a more accurate representation of the normalizers while making it easier to add a new one.
* Lint changes
* Bring back the Oembed OpenGraph inheritance
There is one test that caught that this inheritance was necessary. I still think modelling wise this inheritance shouldn't exist, but this can be tackled separately.
* Return empty hash if the json received is invalid
Before this change if there was a parsing error with JSON it would throw an exception. The goal of this commit is to rescue that exception and then log a warning. I chose to use Discourse's logger wrapper `warn_exception` to have the backtrace and not just used Rails logger. I considered raising an `InvalidParameters` error however if the JSON here is invalid it should not block showing of the Onebox, so logging is enough.
* Prep to support more JSONLD schema types with case
* Extract mustache template object created from JSONLD
This commit is contained in:
@@ -217,6 +217,16 @@ describe Onebox::Engine::AllowlistedGenericOnebox do
|
||||
expect(onebox.to_html).to include("Rudy (1993) - IMDb")
|
||||
expect(onebox.to_html).to include("Rudy: Directed by David Anspaugh. With Sean Astin, Jon Favreau, Ned Beatty, Greta Lind. Rudy has always been told that he was too small to play college football.")
|
||||
end
|
||||
|
||||
it 'shows rating' do
|
||||
onebox = described_class.new("https://www.imdb.com/title/tt0108002/")
|
||||
expect(onebox.to_html).to include("7.5")
|
||||
end
|
||||
|
||||
it 'shows duration' do
|
||||
onebox = described_class.new("https://www.imdb.com/title/tt0108002/")
|
||||
expect(onebox.to_html).to include("01:54")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
describe Onebox::Engine::StandardEmbed do
|
||||
let(:host_class) do
|
||||
Class.new do
|
||||
include Onebox::Engine::StandardEmbed
|
||||
|
||||
def options
|
||||
{}
|
||||
end
|
||||
|
||||
def url
|
||||
''
|
||||
end
|
||||
end
|
||||
end
|
||||
let(:instance) { host_class.new }
|
||||
|
||||
describe '#raw' do
|
||||
it 'does not set title_attr from opengraph data' do
|
||||
Onebox::Helpers.stubs(fetch_html_doc: nil)
|
||||
Onebox::OpenGraph.any_instance.stubs(:data).returns({ description: "description", title_attr: "should not be returned" })
|
||||
Onebox::Oembed.any_instance.stubs(:data).returns({})
|
||||
|
||||
expect(instance.raw).to eq({ description: "description" })
|
||||
end
|
||||
|
||||
it 'sets twitter data' do
|
||||
html_doc = mocked_html_doc(twitter_data: { "name" => "twitter:url", "content" => "cool.url" })
|
||||
Onebox::Helpers.stubs(fetch_html_doc: html_doc)
|
||||
|
||||
expect(instance.raw).to eq({ url: "cool.url" })
|
||||
end
|
||||
|
||||
it 'does not override data with twitter data' do
|
||||
html_doc = mocked_html_doc(twitter_data: { "name" => "twitter:title", "content" => "i do not want to override" })
|
||||
Onebox::OpenGraph.any_instance.stubs(:data).returns({ description: "description", title: "do not override me" })
|
||||
Onebox::Helpers.stubs(fetch_html_doc: html_doc)
|
||||
|
||||
expect(instance.raw).to eq({ description: "description", title: "do not override me" })
|
||||
end
|
||||
|
||||
it 'does not override data with oembed data' do
|
||||
Onebox::Oembed.any_instance.stubs(:data).returns({ title: "i do not want to override" })
|
||||
html_doc = mocked_html_doc(twitter_data: { "name" => "twitter:title", "content" => "do not override me" })
|
||||
Onebox::Helpers.stubs(fetch_html_doc: html_doc)
|
||||
|
||||
expect(instance.raw).to eq({ title: "do not override me" })
|
||||
end
|
||||
|
||||
it 'sets oembed data' do
|
||||
Onebox::Helpers.stubs(fetch_html_doc: nil)
|
||||
Onebox::Oembed.any_instance.stubs(:data).returns({ description: "description" })
|
||||
|
||||
expect(instance.raw).to eq({ description: "description" })
|
||||
end
|
||||
|
||||
it 'does not override data with json_ld data' do
|
||||
Onebox::Helpers.stubs(fetch_html_doc: nil)
|
||||
Onebox::JsonLd.any_instance.stubs(:data).returns({ title: "i do not want to override" })
|
||||
Onebox::Oembed.any_instance.stubs(:data).returns({ title: "do not override me" })
|
||||
|
||||
expect(instance.raw).to eq({ title: "do not override me" })
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def mocked_html_doc(twitter_data: nil)
|
||||
html_doc = mock
|
||||
html_doc.stubs(at_css: nil, at: nil)
|
||||
stub_twitter(html_doc, twitter_data)
|
||||
stub_favicon(html_doc)
|
||||
stub_json_ld
|
||||
html_doc
|
||||
end
|
||||
|
||||
def stub_twitter(html_doc, twitter_data = [])
|
||||
html_doc.expects(:css).with('meta').at_least_once.returns([twitter_data])
|
||||
end
|
||||
|
||||
def stub_favicon(html_doc)
|
||||
html_doc.stubs(:css).with('link[rel="shortcut icon"], link[rel="icon shortcut"], link[rel="shortcut"], link[rel="icon"]').returns([])
|
||||
end
|
||||
|
||||
def stub_json_ld
|
||||
normalizer = mock
|
||||
normalizer.stubs(:data).returns([])
|
||||
Onebox::JsonLd.stubs(new: normalizer)
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,54 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'onebox/json_ld'
|
||||
|
||||
describe Onebox::JsonLd do
|
||||
it 'logs warning and returns an empty hash if received json is invalid' do
|
||||
invalid_json = "{\"@type\":invalid-json}"
|
||||
doc = Nokogiri::HTML("<script type=\"application/ld+json\">#{invalid_json}</script>")
|
||||
Discourse.expects(:warn_exception).with(
|
||||
instance_of(JSON::ParserError), { message: "Error parsing JSON-LD json: #{invalid_json}" }
|
||||
)
|
||||
|
||||
json_ld = described_class.new(doc)
|
||||
|
||||
expect(json_ld.data).to eq({})
|
||||
end
|
||||
|
||||
it 'returns an empty hash if there is no json_ld script tag' do
|
||||
doc = Nokogiri::HTML("<script type=\"something else\"></script>")
|
||||
json_ld = described_class.new(doc)
|
||||
expect(json_ld.data).to eq({})
|
||||
end
|
||||
|
||||
it 'returns an empty hash if there is no json_ld data' do
|
||||
doc = Nokogiri::HTML("<script type=\"application/ld+json\"></script>")
|
||||
json_ld = described_class.new(doc)
|
||||
expect(json_ld.data).to eq({})
|
||||
end
|
||||
|
||||
it 'returns an empty hash if the type of JSONLD data is not Movie' do
|
||||
doc = Nokogiri::HTML("<script type=\"application/ld+json\">{\"@type\":\"Something Else\",\"aggregateRating\":{\"@type\":\"AggregateRating\",\"ratingCount\":806928,\"bestRating\":10,\"worstRating\":1}}</script>")
|
||||
json_ld = described_class.new(doc)
|
||||
expect(json_ld.data).to eq({})
|
||||
end
|
||||
|
||||
it 'correctly normalizes the properties' do
|
||||
doc = Nokogiri::HTML(onebox_response('imdb'))
|
||||
json_ld = described_class.new(doc)
|
||||
expect(json_ld.data).to eq(expected_movie_hash)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def expected_movie_hash
|
||||
{
|
||||
name: 'Rudy',
|
||||
image: 'https://m.media-amazon.com/images/M/MV5BZGUzMDU1YmQtMzBkOS00MTNmLTg5ZDQtZjY5Njk4Njk2MmRlXkEyXkFqcGdeQXVyNjc1NTYyMjg@._V1_.jpg',
|
||||
description: 'Rudy has always been told that he was too small to play college football. But he is determined to overcome the odds and fulfill his dream of playing for Notre Dame.',
|
||||
rating: 7.5,
|
||||
genres: ['Biography', 'Drama', 'Sport'],
|
||||
duration: '01:54'
|
||||
}
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,52 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'onebox/movie'
|
||||
|
||||
describe Onebox::Movie do
|
||||
it 'returns a nil rating if there is no aggregateRating item in json_ld data' do
|
||||
json_ld_data = json_ld_data_from_doc(
|
||||
"<script type=\"application/ld+json\">{\"@type\":\"Movie\",\"someKey\":{}}</script>"
|
||||
)
|
||||
json_ld = described_class.new(json_ld_data)
|
||||
expect(json_ld.rating).to eq(nil)
|
||||
end
|
||||
|
||||
it 'returns a nil rating if there is no ratingValue item in json_ld data' do
|
||||
json_ld_data = json_ld_data_from_doc(
|
||||
"<script type=\"application/ld+json\">{\"@type\":\"Movie\",\"aggregateRating\":{\"@type\":\"AggregateRating\",\"ratingCount\":806928,\"bestRating\":10,\"worstRating\":1}}</script>"
|
||||
)
|
||||
json_ld = described_class.new(json_ld_data)
|
||||
expect(json_ld.rating).to eq(nil)
|
||||
end
|
||||
|
||||
it 'returns a nil if there is no duration in json_ld data' do
|
||||
json_ld_data = json_ld_data_from_doc(
|
||||
"<script type=\"application/ld+json\">{\"@type\":\"Movie\",\"aggregateRating\":{\"@type\":\"AggregateRating\",\"ratingCount\":806928,\"bestRating\":10,\"worstRating\":1}}</script>"
|
||||
)
|
||||
json_ld = described_class.new(json_ld_data)
|
||||
expect(json_ld.duration).to eq(nil)
|
||||
end
|
||||
|
||||
it 'to_h returns hash version of the object' do
|
||||
json_ld_data = json_ld_data_from_doc(onebox_response('imdb'))
|
||||
movie = described_class.new(json_ld_data)
|
||||
expect(movie.to_h).to eq(expected_movie_hash)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def json_ld_data_from_doc(html)
|
||||
JSON[Nokogiri::HTML(html).search('script[type="application/ld+json"]').text]
|
||||
end
|
||||
|
||||
def expected_movie_hash
|
||||
{
|
||||
name: 'Rudy',
|
||||
image: 'https://m.media-amazon.com/images/M/MV5BZGUzMDU1YmQtMzBkOS00MTNmLTg5ZDQtZjY5Njk4Njk2MmRlXkEyXkFqcGdeQXVyNjc1NTYyMjg@._V1_.jpg',
|
||||
description: 'Rudy has always been told that he was too small to play college football. But he is determined to overcome the odds and fulfill his dream of playing for Notre Dame.',
|
||||
rating: 7.5,
|
||||
genres: ['Biography', 'Drama', 'Sport'],
|
||||
duration: '01:54'
|
||||
}
|
||||
end
|
||||
end
|
||||
@@ -3,7 +3,7 @@
|
||||
require "onebox/oembed"
|
||||
|
||||
describe Onebox::Oembed do
|
||||
it "excludes html tags" do
|
||||
it "excludes text tags" do
|
||||
json = '{"text": "<iframe src=\'https://ifram.es/foo/bar\'></iframe>"}'
|
||||
oembed = described_class.new(json)
|
||||
expect(oembed.text).to be_nil
|
||||
|
||||
Reference in New Issue
Block a user