discourse/lib/onebox/engine
sansnumero f0c6dd5682
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
2022-06-13 17:32:34 +02:00
..
allowlisted_generic_onebox.rb DEV: Correctly tag heredocs (#16061) 2022-02-28 20:50:55 +01:00
amazon_onebox.rb FIX: Only display the first listed price (#16138) 2022-03-08 15:24:45 -05:00
animated_image_onebox.rb FIX: Giphy oneboxing when the response is an image (#13199) 2021-05-28 15:10:32 -04:00
asciinema_onebox.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
audio_onebox.rb FIX: Disable previews if diffhtml is enabled (#14537) 2021-10-08 15:57:08 +03:00
audioboom_onebox.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
band_camp_onebox.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
cloud_app_onebox.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
coub_onebox.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
facebook_media_onebox.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
five_hundred_px_onebox.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
flickr_onebox.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
flickr_shortened_onebox.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
gfycat_onebox.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
github_actions_onebox.rb DEV: Fix rubocop issues (#14715) 2021-10-27 11:39:28 +03:00
github_blob_onebox.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
github_commit_onebox.rb UX: Use committed date for GitHub oneboxes (#16318) 2022-03-30 09:16:28 +08:00
github_folder_onebox.rb FIX: Broken GitHub folder onebox logic (#15612) 2022-01-17 18:32:07 +01:00
github_gist_onebox.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
github_issue_onebox.rb FEATURE: Render emojis on GitHub labels when oneboxing an issue. (#13531) 2021-06-25 14:48:36 -03:00
github_pull_request_onebox.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
gitlab_blob_onebox.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
google_calendar_onebox.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
google_docs_onebox.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
google_drive_onebox.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
google_maps_onebox.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
google_photos_onebox.rb FIX: Correct the play icon position (#14295) 2021-09-09 15:10:32 +02:00
google_play_app_onebox.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
hackernews_onebox.rb FEATURE: Onebox for news.ycombinator.com (#15781) 2022-02-03 13:39:21 -03:00
html.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
image_onebox.rb FEATURE: Onebox can match engines based on the content_type (#13876) 2021-07-30 13:36:30 -04:00
imgur_onebox.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
instagram_onebox.rb FIX: Display Instagram Oneboxes in an iframe (#14789) 2021-11-02 14:34:51 -04:00
json.rb DEV: Fix rubocop issues (#14715) 2021-10-27 11:39:28 +03:00
kaltura_onebox.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
mixcloud_onebox.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
opengraph_image.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
pastebin_onebox.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
pdf_onebox.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
pubmed_onebox.rb DEV: Fix rubocop issues (#14715) 2021-10-27 11:39:28 +03:00
reddit_media_onebox.rb FIX: Correct the play icon position (#14295) 2021-09-09 15:10:32 +02:00
replit_onebox.rb Update replit onebox to accept .com 2021-10-19 16:37:33 -04:00
simplecast_onebox.rb FIX: update iframe url for simplecast onebox (#13957) 2021-08-05 18:29:04 +05:30
sketch_fab_onebox.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
slides_onebox.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
sound_cloud_onebox.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
stack_exchange_onebox.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
standard_embed.rb Add support for JSON LD in Onebox (#17007) 2022-06-13 17:32:34 +02:00
steam_store_onebox.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
trello_onebox.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
twitch_clips_onebox.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
twitch_stream_onebox.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
twitch_video_onebox.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
twitter_status_onebox.rb FIX: Don’t use `user_generated` images as avatar images in Oneboxed Twitter content (#13712) 2021-07-13 14:54:28 -04:00
typeform_onebox.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
video_onebox.rb FIX: Disable previews if diffhtml is enabled (#14537) 2021-10-08 15:57:08 +03:00
vimeo_onebox.rb FIX: Vimeo private video oneboxes were broken (#14510) 2021-10-05 15:46:58 +05:30
wikimedia_onebox.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
wikipedia_onebox.rb FIX: Unescapes hash section with present to account for url-encoded chars 2021-08-12 10:43:50 -04:00
wistia_onebox.rb DEV: Add tests for wistia onebox. (#15860) 2022-02-08 13:04:32 +08:00
xkcd_onebox.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
youku_onebox.rb DEV: Absorb onebox gem into core (#12979) 2021-05-26 15:11:35 +05:30
youtube_onebox.rb FIX: escape youtube title when constructing onebox preview html (#16999) 2022-06-08 13:42:37 +08:00