From 331236d6d790dd41538668469f16f853c5699884 Mon Sep 17 00:00:00 2001 From: jbrw Date: Wed, 18 Nov 2020 12:55:16 -0500 Subject: [PATCH] Onebox improved error handling and support for Instagram Access Tokens (#11253) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * FEATURE: display error if Oneboxing fails due to HTTP error - display warning if onebox URL is unresolvable - display warning if attributes are missing * FEATURE: Use new Instagram oEmbed endpoint if access token is configured Instagram requires an Access Token to access their oEmbed endpoint. The requirements (from https://developers.facebook.com/docs/instagram/oembed/) are as follows: - a Facebook Developer account, which you can create at developers.facebook.com - a registered Facebook app - the oEmbed Product added to the app - an Access Token - The Facebook app must be in Live Mode The generated Access Token, once added to SiteSetting.facebook_app_access_token, will be passed to onebox. Onebox can then use this token to access the oEmbed endpoint to generate a onebox for Instagram. * DEV: update user agent string * DEV: don’t do HEAD requests against news.yahoo.com * DEV: Bump onebox version from 2.1.5 to 2.1.6 * DEV: Avoid re-reading templates * DEV: Tweaks to onebox mustache templates * DEV: simplified error message for missing onebox data * Apply suggestions from code review Co-authored-by: Gerhard Schlager --- Gemfile.lock | 2 +- .../stylesheets/common/base/onebox.scss | 10 +++ config/locales/server.en.yml | 11 +++ config/site_settings.yml | 3 + lib/final_destination.rb | 7 +- .../templates/discourse_user_onebox.mustache | 2 +- .../preview_error_fragment_onebox.mustache | 4 + .../templates/preview_error_onebox.mustache | 10 +++ lib/oneboxer.rb | 76 ++++++++++++++++--- spec/components/oneboxer_spec.rb | 54 ++++++++++++- spec/fixtures/onebox/instagram_new.response | 13 ++++ spec/fixtures/onebox/instagram_old.response | 15 ++++ .../onebox/missing_description.response | 64 ++++++++++++++++ .../missing_description_and_image.response | 59 ++++++++++++++ .../onebox/video_missing_description.response | 30 ++++++++ spec/requests/onebox_controller_spec.rb | 21 ++--- 16 files changed, 351 insertions(+), 30 deletions(-) create mode 100644 lib/onebox/templates/preview_error_fragment_onebox.mustache create mode 100644 lib/onebox/templates/preview_error_onebox.mustache create mode 100644 spec/fixtures/onebox/instagram_new.response create mode 100644 spec/fixtures/onebox/instagram_old.response create mode 100644 spec/fixtures/onebox/missing_description.response create mode 100644 spec/fixtures/onebox/missing_description_and_image.response create mode 100644 spec/fixtures/onebox/video_missing_description.response diff --git a/Gemfile.lock b/Gemfile.lock index 791aa9dc7ba..946b7245a12 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -235,7 +235,7 @@ GEM omniauth-twitter (1.4.0) omniauth-oauth (~> 1.1) rack - onebox (2.1.5) + onebox (2.1.6) addressable (~> 2.7.0) htmlentities (~> 4.3) multi_json (~> 1.11) diff --git a/app/assets/stylesheets/common/base/onebox.scss b/app/assets/stylesheets/common/base/onebox.scss index 61687ebabe3..dfcb4020ba9 100644 --- a/app/assets/stylesheets/common/base/onebox.scss +++ b/app/assets/stylesheets/common/base/onebox.scss @@ -379,6 +379,16 @@ pre.onebox code { white-space: normal; } +.onebox-warning-message { + margin-top: 5px; + color: var(--primary-med-or-secondary-med); + img.emoji { + width: 20px; + height: 20px; + float: none; + } +} + // Onebox - Github - PR, Commit & Issue .onebox.githubpullrequest, .onebox.githubcommit, diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index b3a7e92eb50..e519c0f7b92 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -206,6 +206,16 @@ en: cannot_enable_s3_uploads_when_s3_enabled_globally: "You cannot enable S3 uploads because S3 uploads are already globally enabled, and enabling this site-level could cause critical issues with uploads" cors_origins_should_not_have_trailing_slash: "You should not add the trailing slash (/) to CORS origins." conflicting_google_user_id: 'The Google Account ID for this account has changed; staff intervention is required for security reasons. Please contact staff and point them to
https://meta.discourse.org/t/76575' + onebox: + invalid_address: "Sorry, we were unable to generate a preview for this web page, because the server '%{hostname}' could not be found. Instead of a preview, only a link will appear in your post. :cry:" + error_response: "Sorry, we were unable to generate a preview for this web page, because the web server returned an error code of %{status_code}. Instead of a preview, only a link will appear in your post. :cry:" + missing_data: + one: "Sorry, we were unable to generate a preview for this web page, because the following oEmbed / OpenGraph tag could not be found: %{missing_attributes}" + other: "Sorry, we were unable to generate a preview for this web page, because the following oEmbed / OpenGraph tags could not be found: %{missing_attributes}" + + word_connector: + # Connects words with a comma. Example: "foo, bar" + comma: ", " activemodel: errors: @@ -1491,6 +1501,7 @@ en: enable_inline_onebox_on_all_domains: "Ignore inline_onebox_domain_allowlist site setting and allow inline onebox on all domains." force_custom_user_agent_hosts: "Hosts for which to use the custom onebox user agent on all requests. (Especially useful for hosts that limit access by user agent)." max_oneboxes_per_post: "Maximum number of oneboxes in a post." + facebook_app_access_token: "A token generated from your Facebook app ID and secret. Used to generate Instagram oneboxes." logo: "The logo image at the top left of your site. Use a wide rectangular image with a height of 120 and an aspect ratio greater than 3:1. If left blank, the site title text will be shown." logo_small: "The small logo image at the top left of your site, seen when scrolling down. Use a square 120 × 120 image. If left blank, a home glyph will be shown." diff --git a/config/site_settings.yml b/config/site_settings.yml index 0b274f9ee98..08f20834a1e 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -1599,6 +1599,9 @@ onebox: force_custom_user_agent_hosts: default: "http://codepen.io" type: list + facebook_app_access_token: + default: "" + secret: true spam: add_rel_nofollow_to_user_content: true hide_post_sensitivity: diff --git a/lib/final_destination.rb b/lib/final_destination.rb index b5a4dcc4749..8bf06ee6e0f 100644 --- a/lib/final_destination.rb +++ b/lib/final_destination.rb @@ -67,7 +67,7 @@ class FinalDestination @timeout = @opts[:timeout] || nil @preserve_fragment_url = @preserve_fragment_url_hosts.any? { |host| hostname_matches?(host) } @validate_uri = @opts.fetch(:validate_uri) { true } - @user_agent = @force_custom_user_agent_hosts.any? { |host| hostname_matches?(host) } ? Onebox.options.user_agent : "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36" + @user_agent = @force_custom_user_agent_hosts.any? { |host| hostname_matches?(host) } ? Onebox.options.user_agent : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Safari/605.1.15" end def self.connection_timeout @@ -170,6 +170,7 @@ class FinalDestination end unless validate_uri + @status = :invalid_address log(:warn, "FinalDestination could not resolve URL (invalid URI): #{@uri}") if @verbose return nil end @@ -276,6 +277,10 @@ class FinalDestination (IPAddr.new(@uri.hostname) rescue nil).nil? end + def hostname + @uri.hostname + end + def hostname_matches?(url) url = uri(url) @uri && url.present? && @uri.hostname == url&.hostname diff --git a/lib/onebox/templates/discourse_user_onebox.mustache b/lib/onebox/templates/discourse_user_onebox.mustache index 60db7491c11..88a2cddf7fa 100644 --- a/lib/onebox/templates/discourse_user_onebox.mustache +++ b/lib/onebox/templates/discourse_user_onebox.mustache @@ -28,5 +28,5 @@ {{/bio}} {{joined}} {{created_at}} -
+
diff --git a/lib/onebox/templates/preview_error_fragment_onebox.mustache b/lib/onebox/templates/preview_error_fragment_onebox.mustache new file mode 100644 index 00000000000..9a36e1183e2 --- /dev/null +++ b/lib/onebox/templates/preview_error_fragment_onebox.mustache @@ -0,0 +1,4 @@ +
+ {{{error_message}}} +
+
diff --git a/lib/onebox/templates/preview_error_onebox.mustache b/lib/onebox/templates/preview_error_onebox.mustache new file mode 100644 index 00000000000..c51f8a34405 --- /dev/null +++ b/lib/onebox/templates/preview_error_onebox.mustache @@ -0,0 +1,10 @@ + diff --git a/lib/oneboxer.rb b/lib/oneboxer.rb index cb7c6945a85..b5caa926a69 100644 --- a/lib/oneboxer.rb +++ b/lib/oneboxer.rb @@ -27,7 +27,7 @@ module Oneboxer end def self.force_get_hosts - @force_get_hosts ||= ['http://us.battle.net'] + @force_get_hosts ||= ['http://us.battle.net', 'https://news.yahoo.com/'] end def self.force_custom_user_agent_hosts @@ -261,7 +261,7 @@ module Oneboxer quote: PrettyText.unescape_emoji(post.excerpt(SiteSetting.post_onebox_maxlength)), } - template = File.read("#{Rails.root}/lib/onebox/templates/discourse_topic_onebox.mustache") + template = template("discourse_topic_onebox") Mustache.render(template, args) end end @@ -287,8 +287,7 @@ module Oneboxer original_url: url } - template = File.read("#{Rails.root}/lib/onebox/templates/discourse_user_onebox.mustache") - Mustache.render(template, args) + Mustache.render(template("discourse_user_onebox"), args) else nil end @@ -313,12 +312,26 @@ module Oneboxer def self.external_onebox(url) Discourse.cache.fetch(onebox_cache_key(url), expires_in: 1.day) do fd = FinalDestination.new(url, - ignore_redirects: ignore_redirects, - ignore_hostnames: blocked_domains, - force_get_hosts: force_get_hosts, - force_custom_user_agent_hosts: force_custom_user_agent_hosts, - preserve_fragment_url_hosts: preserve_fragment_url_hosts) + ignore_redirects: ignore_redirects, + ignore_hostnames: blocked_domains, + force_get_hosts: force_get_hosts, + force_custom_user_agent_hosts: force_custom_user_agent_hosts, + preserve_fragment_url_hosts: preserve_fragment_url_hosts) uri = fd.resolve + + if fd.status != :resolved + args = { link: url } + if fd.status == :invalid_address + args[:error_message] = I18n.t("errors.onebox.invalid_address", hostname: fd.hostname) + elsif fd.status_code + args[:error_message] = I18n.t("errors.onebox.error_response", status_code: fd.status_code) + end + + error_box = blank_onebox + error_box[:preview] = preview_error_onebox(args) + return error_box + end + return blank_onebox if uri.blank? || blocked_domains.map { |hostname| uri.hostname.match?(hostname) }.any? options = { @@ -326,13 +339,56 @@ module Oneboxer sanitize_config: Onebox::DiscourseOneboxSanitizeConfig::Config::DISCOURSE_ONEBOX, allowed_iframe_origins: allowed_iframe_origins, hostname: GlobalSetting.hostname, + facebook_app_access_token: SiteSetting.facebook_app_access_token, } options[:cookie] = fd.cookie if fd.cookie r = Onebox.preview(uri.to_s, options) + result = { onebox: r.to_s, preview: r&.placeholder_html.to_s } - { onebox: r.to_s, preview: r&.placeholder_html.to_s } + # NOTE: Call r.errors after calling placeholder_html + if r.errors.any? + missing_attributes = r.errors.keys.map(&:to_s).sort.join(I18n.t("word_connector.comma")) + error_message = I18n.t("errors.onebox.missing_data", missing_attributes: missing_attributes, count: r.errors.keys.size) + args = r.data.merge(error_message: error_message) + + if result[:preview].blank? + result[:preview] = preview_error_onebox(args) + else + doc = Nokogiri::HTML5::fragment(result[:preview]) + aside = doc.at('aside') + + if aside + # Add an error message to the preview that was returned + error_fragment = preview_error_onebox_fragment(args) + aside.add_child(error_fragment) + result[:preview] = doc.to_html + end + end + end + + result + end + end + + def self.preview_error_onebox(args, is_fragment = false) + args[:title] ||= args[:link] if args[:link] + args[:error_message] = PrettyText.unescape_emoji(args[:error_message]) if args[:error_message] + + template_name = is_fragment ? "preview_error_fragment_onebox" : "preview_error_onebox" + Mustache.render(template(template_name), args) + end + + def self.preview_error_onebox_fragment(args) + preview_error_onebox(args, true) + end + + def self.template(template_name) + @template_cache ||= {} + @template_cache[template_name] ||= begin + full_path = "#{Rails.root}/lib/onebox/templates/#{template_name}.mustache" + File.read(full_path) end end diff --git a/spec/components/oneboxer_spec.rb b/spec/components/oneboxer_spec.rb index 5db8b83aaef..30e6b470a7b 100644 --- a/spec/components/oneboxer_spec.rb +++ b/spec/components/oneboxer_spec.rb @@ -3,11 +3,16 @@ require 'rails_helper' describe Oneboxer do + def response(file) + file = File.join("spec", "fixtures", "onebox", "#{file}.response") + File.exists?(file) ? File.read(file) : "" + end + it "returns blank string for an invalid onebox" do stub_request(:head, "http://boom.com") stub_request(:get, "http://boom.com").to_return(body: "") - expect(Oneboxer.preview("http://boom.com")).to eq("") + expect(Oneboxer.preview("http://boom.com", invalidate_oneboxes: true)).to include("Sorry, we were unable to generate a preview for this web page") expect(Oneboxer.onebox("http://boom.com")).to eq("") end @@ -191,6 +196,7 @@ describe Oneboxer do +

body

@@ -210,6 +216,7 @@ describe Oneboxer do # Disable all onebox iframes: SiteSetting.allowed_onebox_iframes = "" output = Oneboxer.onebox("https://www.youtube.com/watch?v=dQw4w9WgXcQ", invalidate_oneboxes: true) + expect(output).not_to include("\u003Cdiv style=\"padding:16px;\"> \u003Ca href=\"https://www.instagram.com/p/CHLkBERAiLa/?utm_source=ig_embed&utm_campaign=loading\" style=\" background:#FFFFFF; line-height:0; padding:0 0; text-align:center; text-decoration:none; width:100\u0025;\" target=\"_blank\"> \u003Cdiv style=\" display: flex; flex-direction: row; align-items: center;\"> \u003Cdiv style=\"background-color: #F4F4F4; border-radius: 50\u0025; flex-grow: 0; height: 40px; margin-right: 14px; width: 40px;\">\u003C/div> \u003Cdiv style=\"display: flex; flex-direction: column; flex-grow: 1; justify-content: center;\"> \u003Cdiv style=\" background-color: #F4F4F4; border-radius: 4px; flex-grow: 0; height: 14px; margin-bottom: 6px; width: 100px;\">\u003C/div> \u003Cdiv style=\" background-color: #F4F4F4; border-radius: 4px; flex-grow: 0; height: 14px; width: 60px;\">\u003C/div>\u003C/div>\u003C/div>\u003Cdiv style=\"padding: 19\u0025 0;\">\u003C/div> \u003Cdiv style=\"display:block; height:50px; margin:0 auto 12px; width:50px;\">\u003Csvg width=\"50px\" height=\"50px\" viewBox=\"0 0 60 60\" version=\"1.1\" xmlns=\"https://www.w3.org/2000/svg\" xmlns:xlink=\"https://www.w3.org/1999/xlink\">\u003Cg stroke=\"none\" stroke-width=\"1\" fill=\"none\" fill-rule=\"evenodd\">\u003Cg transform=\"translate(-511.000000, -20.000000)\" fill=\"#000000\">\u003Cg>\u003Cpath d=\"M556.869,30.41 C554.814,30.41 553.148,32.076 553.148,34.131 C553.148,36.186 554.814,37.852 556.869,37.852 C558.924,37.852 560.59,36.186 560.59,34.131 C560.59,32.076 558.924,30.41 556.869,30.41 M541,60.657 C535.114,60.657 530.342,55.887 530.342,50 C530.342,44.114 535.114,39.342 541,39.342 C546.887,39.342 551.658,44.114 551.658,50 C551.658,55.887 546.887,60.657 541,60.657 M541,33.886 C532.1,33.886 524.886,41.1 524.886,50 C524.886,58.899 532.1,66.113 541,66.113 C549.9,66.113 557.115,58.899 557.115,50 C557.115,41.1 549.9,33.886 541,33.886 M565.378,62.101 C565.244,65.022 564.756,66.606 564.346,67.663 C563.803,69.06 563.154,70.057 562.106,71.106 C561.058,72.155 560.06,72.803 558.662,73.347 C557.607,73.757 556.021,74.244 553.102,74.378 C549.944,74.521 548.997,74.552 541,74.552 C533.003,74.552 532.056,74.521 528.898,74.378 C525.979,74.244 524.393,73.757 523.338,73.347 C521.94,72.803 520.942,72.155 519.894,71.106 C518.846,70.057 518.197,69.06 517.654,67.663 C517.244,66.606 516.755,65.022 516.623,62.101 C516.479,58.943 516.448,57.996 516.448,50 C516.448,42.003 516.479,41.056 516.623,37.899 C516.755,34.978 517.244,33.391 517.654,32.338 C518.197,30.938 518.846,29.942 519.894,28.894 C520.942,27.846 521.94,27.196 523.338,26.654 C524.393,26.244 525.979,25.756 528.898,25.623 C532.057,25.479 533.004,25.448 541,25.448 C548.997,25.448 549.943,25.479 553.102,25.623 C556.021,25.756 557.607,26.244 558.662,26.654 C560.06,27.196 561.058,27.846 562.106,28.894 C563.154,29.942 563.803,30.938 564.346,32.338 C564.756,33.391 565.244,34.978 565.378,37.899 C565.522,41.056 565.552,42.003 565.552,50 C565.552,57.996 565.522,58.943 565.378,62.101 M570.82,37.631 C570.674,34.438 570.167,32.258 569.425,30.349 C568.659,28.377 567.633,26.702 565.965,25.035 C564.297,23.368 562.623,22.342 560.652,21.575 C558.743,20.834 556.562,20.326 553.369,20.18 C550.169,20.033 549.148,20 541,20 C532.853,20 531.831,20.033 528.631,20.18 C525.438,20.326 523.257,20.834 521.349,21.575 C519.376,22.342 517.703,23.368 516.035,25.035 C514.368,26.702 513.342,28.377 512.574,30.349 C511.834,32.258 511.326,34.438 511.181,37.631 C511.035,40.831 511,41.851 511,50 C511,58.147 511.035,59.17 511.181,62.369 C511.326,65.562 511.834,67.743 512.574,69.651 C513.342,71.625 514.368,73.296 516.035,74.965 C517.703,76.634 519.376,77.658 521.349,78.425 C523.257,79.167 525.438,79.673 528.631,79.82 C531.831,79.965 532.853,80.001 541,80.001 C549.148,80.001 550.169,79.965 553.369,79.82 C556.562,79.673 558.743,79.167 560.652,78.425 C562.623,77.658 564.297,76.634 565.965,74.965 C567.633,73.296 568.659,71.625 569.425,69.651 C570.167,67.743 570.674,65.562 570.82,62.369 C570.966,59.17 571,58.147 571,50 C571,41.851 570.966,40.831 570.82,37.631\">\u003C/path>\u003C/g>\u003C/g>\u003C/g>\u003C/svg>\u003C/div>\u003Cdiv style=\"padding-top: 8px;\"> \u003Cdiv style=\" color:#3897f0; font-family:Arial,sans-serif; font-size:14px; font-style:normal; font-weight:550; line-height:18px;\"> View this post on Instagram\u003C/div>\u003C/div>\u003Cdiv style=\"padding: 12.5\u0025 0;\">\u003C/div> \u003Cdiv style=\"display: flex; flex-direction: row; margin-bottom: 14px; align-items: center;\">\u003Cdiv> \u003Cdiv style=\"background-color: #F4F4F4; border-radius: 50\u0025; height: 12.5px; width: 12.5px; transform: translateX(0px) translateY(7px);\">\u003C/div> \u003Cdiv style=\"background-color: #F4F4F4; height: 12.5px; transform: rotate(-45deg) translateX(3px) translateY(1px); width: 12.5px; flex-grow: 0; margin-right: 14px; margin-left: 2px;\">\u003C/div> \u003Cdiv style=\"background-color: #F4F4F4; border-radius: 50\u0025; height: 12.5px; width: 12.5px; transform: translateX(9px) translateY(-18px);\">\u003C/div>\u003C/div>\u003Cdiv style=\"margin-left: 8px;\"> \u003Cdiv style=\" background-color: #F4F4F4; border-radius: 50\u0025; flex-grow: 0; height: 20px; width: 20px;\">\u003C/div> \u003Cdiv style=\" width: 0; height: 0; border-top: 2px solid transparent; border-left: 6px solid #f4f4f4; border-bottom: 2px solid transparent; transform: translateX(16px) translateY(-4px) rotate(30deg)\">\u003C/div>\u003C/div>\u003Cdiv style=\"margin-left: auto;\"> \u003Cdiv style=\" width: 0px; border-top: 8px solid #F4F4F4; border-right: 8px solid transparent; transform: translateY(16px);\">\u003C/div> \u003Cdiv style=\" background-color: #F4F4F4; flex-grow: 0; height: 12px; width: 16px; transform: translateY(-4px);\">\u003C/div> \u003Cdiv style=\" width: 0; height: 0; border-top: 8px solid #F4F4F4; border-left: 8px solid transparent; transform: translateY(-4px) translateX(8px);\">\u003C/div>\u003C/div>\u003C/div> \u003Cdiv style=\"display: flex; flex-direction: column; flex-grow: 1; justify-content: center; margin-bottom: 24px;\"> \u003Cdiv style=\" background-color: #F4F4F4; border-radius: 4px; flex-grow: 0; height: 14px; margin-bottom: 6px; width: 224px;\">\u003C/div> \u003Cdiv style=\" background-color: #F4F4F4; border-radius: 4px; flex-grow: 0; height: 14px; width: 144px;\">\u003C/div>\u003C/div>\u003C/a>\u003Cp style=\" color:#c9c8cd; font-family:Arial,sans-serif; font-size:14px; line-height:17px; margin-bottom:0; margin-top:8px; overflow:hidden; padding:8px 0 7px; text-align:center; text-overflow:ellipsis; white-space:nowrap;\">\u003Ca href=\"https://www.instagram.com/p/CHLkBERAiLa/?utm_source=ig_embed&utm_campaign=loading\" style=\" color:#c9c8cd; font-family:Arial,sans-serif; font-size:14px; font-style:normal; font-weight:normal; line-height:17px; text-decoration:none;\" target=\"_blank\">A post shared by Superette (\u0040superette_shop)\u003C/a>\u003C/p>\u003C/div>\u003C/blockquote>\n\u003Cscript async src=\"//platform.instagram.com/en_US/embeds.js\">\u003C/script>", + "thumbnail_url": "https://scontent.cdninstagram.com/v/t51.2885-15/sh0.08/e35/s640x640/123423691_164607455314708_8782231704527514521_n.jpg?_nc_ht=scontent.cdninstagram.com&_nc_cat=104&_nc_ohc=wDpj4bmnwCUAX-emRq7&_nc_tp=24&oh=e5c2a2fe1a38e11a394a9f78c217c63b&oe=5FD59C49", + "thumbnail_width": 640, + "thumbnail_height": 640 +} diff --git a/spec/fixtures/onebox/instagram_old.response b/spec/fixtures/onebox/instagram_old.response new file mode 100644 index 00000000000..e16fb6fd95e --- /dev/null +++ b/spec/fixtures/onebox/instagram_old.response @@ -0,0 +1,15 @@ + +{ + "version": "1.0", + "title": "Our current mood. Hbu?", + "author_name": "superette_shop", + "author_url": "https://www.instagram.com/superette_shop", + "author_id": 12501718236, "media_id": "2435198426498081498_12501718236", + "provider_name": "Instagram", + "provider_url": "https://www.instagram.com", + "type": "rich", + "width": 658, "height": null, "html": "\u003cblockquote class=\"instagram-media\" data-instgrm-captioned data-instgrm-permalink=\"https://www.instagram.com/p/CHLkBERAiLa/?utm_source=ig_embed\u0026amp;utm_campaign=loading\" data-instgrm-version=\"13\" style=\" background:#FFF; border:0; border-radius:3px; box-shadow:0 0 1px 0 rgba(0,0,0,0.5),0 1px 10px 0 rgba(0,0,0,0.15); margin: 1px; max-width:658px; min-width:326px; padding:0; width:99.375%; width:-webkit-calc(100% - 2px); width:calc(100% - 2px);\"\u003e\u003cdiv style=\"padding:16px;\"\u003e \u003ca href=\"https://www.instagram.com/p/CHLkBERAiLa/?utm_source=ig_embed\u0026amp;utm_campaign=loading\" style=\" background:#FFFFFF; line-height:0; padding:0 0; text-align:center; text-decoration:none; width:100%;\" target=\"_blank\"\u003e \u003cdiv style=\" display: flex; flex-direction: row; align-items: center;\"\u003e \u003cdiv style=\"background-color: #F4F4F4; border-radius: 50%; flex-grow: 0; height: 40px; margin-right: 14px; width: 40px;\"\u003e\u003c/div\u003e \u003cdiv style=\"display: flex; flex-direction: column; flex-grow: 1; justify-content: center;\"\u003e \u003cdiv style=\" background-color: #F4F4F4; border-radius: 4px; flex-grow: 0; height: 14px; margin-bottom: 6px; width: 100px;\"\u003e\u003c/div\u003e \u003cdiv style=\" background-color: #F4F4F4; border-radius: 4px; flex-grow: 0; height: 14px; width: 60px;\"\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv style=\"padding: 19% 0;\"\u003e\u003c/div\u003e \u003cdiv style=\"display:block; height:50px; margin:0 auto 12px; width:50px;\"\u003e\u003csvg width=\"50px\" height=\"50px\" viewBox=\"0 0 60 60\" version=\"1.1\" xmlns=\"https://www.w3.org/2000/svg\" xmlns:xlink=\"https://www.w3.org/1999/xlink\"\u003e\u003cg stroke=\"none\" stroke-width=\"1\" fill=\"none\" fill-rule=\"evenodd\"\u003e\u003cg transform=\"translate(-511.000000, -20.000000)\" fill=\"#000000\"\u003e\u003cg\u003e\u003cpath d=\"M556.869,30.41 C554.814,30.41 553.148,32.076 553.148,34.131 C553.148,36.186 554.814,37.852 556.869,37.852 C558.924,37.852 560.59,36.186 560.59,34.131 C560.59,32.076 558.924,30.41 556.869,30.41 M541,60.657 C535.114,60.657 530.342,55.887 530.342,50 C530.342,44.114 535.114,39.342 541,39.342 C546.887,39.342 551.658,44.114 551.658,50 C551.658,55.887 546.887,60.657 541,60.657 M541,33.886 C532.1,33.886 524.886,41.1 524.886,50 C524.886,58.899 532.1,66.113 541,66.113 C549.9,66.113 557.115,58.899 557.115,50 C557.115,41.1 549.9,33.886 541,33.886 M565.378,62.101 C565.244,65.022 564.756,66.606 564.346,67.663 C563.803,69.06 563.154,70.057 562.106,71.106 C561.058,72.155 560.06,72.803 558.662,73.347 C557.607,73.757 556.021,74.244 553.102,74.378 C549.944,74.521 548.997,74.552 541,74.552 C533.003,74.552 532.056,74.521 528.898,74.378 C525.979,74.244 524.393,73.757 523.338,73.347 C521.94,72.803 520.942,72.155 519.894,71.106 C518.846,70.057 518.197,69.06 517.654,67.663 C517.244,66.606 516.755,65.022 516.623,62.101 C516.479,58.943 516.448,57.996 516.448,50 C516.448,42.003 516.479,41.056 516.623,37.899 C516.755,34.978 517.244,33.391 517.654,32.338 C518.197,30.938 518.846,29.942 519.894,28.894 C520.942,27.846 521.94,27.196 523.338,26.654 C524.393,26.244 525.979,25.756 528.898,25.623 C532.057,25.479 533.004,25.448 541,25.448 C548.997,25.448 549.943,25.479 553.102,25.623 C556.021,25.756 557.607,26.244 558.662,26.654 C560.06,27.196 561.058,27.846 562.106,28.894 C563.154,29.942 563.803,30.938 564.346,32.338 C564.756,33.391 565.244,34.978 565.378,37.899 C565.522,41.056 565.552,42.003 565.552,50 C565.552,57.996 565.522,58.943 565.378,62.101 M570.82,37.631 C570.674,34.438 570.167,32.258 569.425,30.349 C568.659,28.377 567.633,26.702 565.965,25.035 C564.297,23.368 562.623,22.342 560.652,21.575 C558.743,20.834 556.562,20.326 553.369,20.18 C550.169,20.033 549.148,20 541,20 C532.853,20 531.831,20.033 528.631,20.18 C525.438,20.326 523.257,20.834 521.349,21.575 C519.376,22.342 517.703,23.368 516.035,25.035 C514.368,26.702 513.342,28.377 512.574,30.349 C511.834,32.258 511.326,34.438 511.181,37.631 C511.035,40.831 511,41.851 511,50 C511,58.147 511.035,59.17 511.181,62.369 C511.326,65.562 511.834,67.743 512.574,69.651 C513.342,71.625 514.368,73.296 516.035,74.965 C517.703,76.634 519.376,77.658 521.349,78.425 C523.257,79.167 525.438,79.673 528.631,79.82 C531.831,79.965 532.853,80.001 541,80.001 C549.148,80.001 550.169,79.965 553.369,79.82 C556.562,79.673 558.743,79.167 560.652,78.425 C562.623,77.658 564.297,76.634 565.965,74.965 C567.633,73.296 568.659,71.625 569.425,69.651 C570.167,67.743 570.674,65.562 570.82,62.369 C570.966,59.17 571,58.147 571,50 C571,41.851 570.966,40.831 570.82,37.631\"\u003e\u003c/path\u003e\u003c/g\u003e\u003c/g\u003e\u003c/g\u003e\u003c/svg\u003e\u003c/div\u003e\u003cdiv style=\"padding-top: 8px;\"\u003e \u003cdiv style=\" color:#3897f0; font-family:Arial,sans-serif; font-size:14px; font-style:normal; font-weight:550; line-height:18px;\"\u003e View this post on Instagram\u003c/div\u003e\u003c/div\u003e\u003cdiv style=\"padding: 12.5% 0;\"\u003e\u003c/div\u003e \u003cdiv style=\"display: flex; flex-direction: row; margin-bottom: 14px; align-items: center;\"\u003e\u003cdiv\u003e \u003cdiv style=\"background-color: #F4F4F4; border-radius: 50%; height: 12.5px; width: 12.5px; transform: translateX(0px) translateY(7px);\"\u003e\u003c/div\u003e \u003cdiv style=\"background-color: #F4F4F4; height: 12.5px; transform: rotate(-45deg) translateX(3px) translateY(1px); width: 12.5px; flex-grow: 0; margin-right: 14px; margin-left: 2px;\"\u003e\u003c/div\u003e \u003cdiv style=\"background-color: #F4F4F4; border-radius: 50%; height: 12.5px; width: 12.5px; transform: translateX(9px) translateY(-18px);\"\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv style=\"margin-left: 8px;\"\u003e \u003cdiv style=\" background-color: #F4F4F4; border-radius: 50%; flex-grow: 0; height: 20px; width: 20px;\"\u003e\u003c/div\u003e \u003cdiv style=\" width: 0; height: 0; border-top: 2px solid transparent; border-left: 6px solid #f4f4f4; border-bottom: 2px solid transparent; transform: translateX(16px) translateY(-4px) rotate(30deg)\"\u003e\u003c/div\u003e\u003c/div\u003e\u003cdiv style=\"margin-left: auto;\"\u003e \u003cdiv style=\" width: 0px; border-top: 8px solid #F4F4F4; border-right: 8px solid transparent; transform: translateY(16px);\"\u003e\u003c/div\u003e \u003cdiv style=\" background-color: #F4F4F4; flex-grow: 0; height: 12px; width: 16px; transform: translateY(-4px);\"\u003e\u003c/div\u003e \u003cdiv style=\" width: 0; height: 0; border-top: 8px solid #F4F4F4; border-left: 8px solid transparent; transform: translateY(-4px) translateX(8px);\"\u003e\u003c/div\u003e\u003c/div\u003e\u003c/div\u003e \u003cdiv style=\"display: flex; flex-direction: column; flex-grow: 1; justify-content: center; margin-bottom: 24px;\"\u003e \u003cdiv style=\" background-color: #F4F4F4; border-radius: 4px; flex-grow: 0; height: 14px; margin-bottom: 6px; width: 224px;\"\u003e\u003c/div\u003e \u003cdiv style=\" background-color: #F4F4F4; border-radius: 4px; flex-grow: 0; height: 14px; width: 144px;\"\u003e\u003c/div\u003e\u003c/div\u003e\u003c/a\u003e\u003cp style=\" color:#c9c8cd; font-family:Arial,sans-serif; font-size:14px; line-height:17px; margin-bottom:0; margin-top:8px; overflow:hidden; padding:8px 0 7px; text-align:center; text-overflow:ellipsis; white-space:nowrap;\"\u003e\u003ca href=\"https://www.instagram.com/p/CHLkBERAiLa/?utm_source=ig_embed\u0026amp;utm_campaign=loading\" style=\" color:#c9c8cd; font-family:Arial,sans-serif; font-size:14px; font-style:normal; font-weight:normal; line-height:17px; text-decoration:none;\" target=\"_blank\"\u003eA post shared by Superette (@superette_shop)\u003c/a\u003e\u003c/p\u003e\u003c/div\u003e\u003c/blockquote\u003e\n\u003cscript async src=\"//www.instagram.com/embed.js\"\u003e\u003c/script\u003e", + "thumbnail_url": "https://instagram.fykz2-1.fna.fbcdn.net/v/t51.2885-15/sh0.08/e35/s640x640/123423691_164607455314708_8782231704527514521_n.jpg?_nc_ht=instagram.fykz2-1.fna.fbcdn.net\u0026_nc_cat=104\u0026_nc_ohc=wDpj4bmnwCUAX_-P0oZ\u0026_nc_tp=24\u0026oh=9468477264d2ac246d84efc15ea2df79\u0026oe=5FD59C49", + "thumbnail_width": 640, + "thumbnail_height": 640 +} diff --git a/spec/fixtures/onebox/missing_description.response b/spec/fixtures/onebox/missing_description.response new file mode 100644 index 00000000000..bebdc46b39a --- /dev/null +++ b/spec/fixtures/onebox/missing_description.response @@ -0,0 +1,64 @@ + + + + + +Open Graph protocol examples + + + + + + + + + + + + + + + + +

Open Graph protocol examples

+
+

Examples of Open Graph protocol markup.

+ +
+

Open Graph namespace

+ +
+ +
+

Global object namespace

+ +
+
+ + + diff --git a/spec/fixtures/onebox/missing_description_and_image.response b/spec/fixtures/onebox/missing_description_and_image.response new file mode 100644 index 00000000000..523d576d969 --- /dev/null +++ b/spec/fixtures/onebox/missing_description_and_image.response @@ -0,0 +1,59 @@ + + + + + +Open Graph protocol examples + + + + + + + + + + + +

Open Graph protocol examples

+
+

Examples of Open Graph protocol markup.

+ +
+

Open Graph namespace

+ +
+ +
+

Global object namespace

+ +
+
+ + + diff --git a/spec/fixtures/onebox/video_missing_description.response b/spec/fixtures/onebox/video_missing_description.response new file mode 100644 index 00000000000..813c31fc129 --- /dev/null +++ b/spec/fixtures/onebox/video_missing_description.response @@ -0,0 +1,30 @@ + + + + +Arrival of a Train at La Ciotat + + + + + + + + + + + + + + + + + + + + + + +

A video.movie object.

+ + diff --git a/spec/requests/onebox_controller_spec.rb b/spec/requests/onebox_controller_spec.rb index babe3104760..122f7d330b2 100644 --- a/spec/requests/onebox_controller_spec.rb +++ b/spec/requests/onebox_controller_spec.rb @@ -121,28 +121,17 @@ describe OneboxController do stub_request(:get, url).to_return(body: response_body).then.to_raise end - it "returns 404 if the onebox is nil" do + it "returns preview-error if the onebox is nil" do stub_request_to_onebox_url(nil) get "/onebox.json", params: { url: url, refresh: "true" } - expect(response.response_code).to eq(404) + expect(response.body).to include("Sorry, we were unable to generate a preview for this web page") end - it "returns 404 if the onebox is an empty string" do + it "returns preview-error if the onebox is an empty string" do stub_request_to_onebox_url(" \t ") get "/onebox.json", params: { url: url, refresh: "true" } - expect(response.response_code).to eq(404) - end - - it "cases missing onebox URLs so we do not attempt to preview again" do - stub_request_to_onebox_url(nil) - get "/onebox.json", params: { url: url, refresh: "true" } - expect(response.response_code).to eq(404) - Oneboxer.expects(:preview_onebox!).never - get "/onebox.json", params: { url: url, refresh: "true" } - expect(response.response_code).to eq(404) - expect( - Discourse.cache.read(Oneboxer.onebox_failed_cache_key(url)) - ).not_to eq(nil) + expect(response.response_code).to eq(200) + expect(response.body).to include("Sorry, we were unable to generate a preview for this web page") end end