Skip to content

A platform for community discussion. Free, open, simple.

FIX: youtu.be URLs with parameters not using lazy-video in Chat (#37473)

Martin Brennan45776f7f1c8a
diff --git a/lib/oneboxer.rb b/lib/oneboxer.rb
index a33541c7225..3bebb780f6c 100644
--- a/lib/oneboxer.rb
+++ b/lib/oneboxer.rb
@@ -29,6 +29,7 @@ module Oneboxer
"http://store.steampowered.com",
"http://vimeo.com",
"https://www.youtube.com",
+ "https://youtu.be",
"https://twitter.com",
"https://x.com",
Discourse.base_url,
diff --git a/plugins/chat/spec/system/chat_lazy_youtube_spec.rb b/plugins/chat/spec/system/chat_lazy_youtube_spec.rb
new file mode 100644
index 00000000000..a8c9dfd4444
--- /dev/null
+++ b/plugins/chat/spec/system/chat_lazy_youtube_spec.rb
@@ -0,0 +1,134 @@
+# frozen_string_literal: true
+
+RSpec.describe "Chat lazy YouTube videos", type: :system do
+ fab!(:current_user, :user)
+ fab!(:channel, :category_channel)
+
+ let(:chat_page) { PageObjects::Pages::Chat.new }
+ let(:channel_page) { PageObjects::Pages::ChatChannel.new }
+
+ let(:video_id) { "dQw4w9WgXcQ" }
+ let(:video_title) { "Rick Astley - Never Gonna Give You Up" }
+ let(:thumbnail_url) { "https://i.ytimg.com/vi/#{video_id}/hqdefault.jpg" }
+
+ let(:oembed_response) do
+ {
+ title: video_title,
+ author_name: "RickAstleyVEVO",
+ type: "video",
+ thumbnail_url: thumbnail_url,
+ }.to_json
+ end
+
+ let(:youtube_html) { <<~HTML }
+ <html>
+ <head>
+ <meta property="og:title" content="#{video_title}">
+ <meta property="og:image" content="#{thumbnail_url}">
+ </head>
+ <body></body>
+ </html>
+ HTML
+
+ before do
+ chat_system_bootstrap
+ channel.add(current_user)
+ sign_in(current_user)
+
+ # NOTE: This spec is testing integration with the core discourse-lazy-videos plugin.
+ SiteSetting.lazy_videos_enabled = true
+ SiteSetting.lazy_youtube_enabled = true
+
+ stub_request(:get, "https://img.youtube.com/vi/#{video_id}/maxresdefault.jpg").to_return(
+ status: 200,
+ body: "",
+ )
+ stub_request(:head, "https://img.youtube.com/vi/#{video_id}/maxresdefault.jpg").to_return(
+ status: 200,
+ body: "",
+ )
+ end
+
+ def stub_youtube_requests(url)
+ stub_request(:get, url).to_return(status: 200, body: youtube_html)
+ stub_request(:head, url).to_return(status: 200, body: "")
+ end
+
+ def stub_oembed_request(url)
+ stub_request(:get, "https://www.youtube.com/oembed?url=#{url}").to_return(
+ status: 200,
+ body: oembed_response,
+ )
+ end
+
+ context "with youtube.com URLs" do
+ let(:youtube_url) { "https://www.youtube.com/watch?v=#{video_id}" }
+
+ before do
+ stub_youtube_requests(youtube_url)
+ stub_oembed_request(youtube_url)
+ end
+
+ it "renders lazy-video-container for standard youtube.com URL" do
+ chat_page.visit_channel(channel)
+ channel_page.send_message(youtube_url)
+
+ expect(page).to have_css(".lazy-video-container[data-video-id='#{video_id}']")
+ end
+
+ it "renders lazy-video-container for youtube.com URL with timestamp" do
+ url_with_time = "#{youtube_url}&t=42"
+ stub_youtube_requests(url_with_time)
+ stub_oembed_request(url_with_time)
+
+ chat_page.visit_channel(channel)
+ channel_page.send_message(url_with_time)
+
+ expect(page).to have_css(
+ ".lazy-video-container[data-video-id='#{video_id}'][data-video-start-time='42']",
+ )
+ end
+ end
+
+ context "with youtu.be URLs" do
+ let(:youtu_be_url) { "https://youtu.be/#{video_id}" }
+
+ before do
+ stub_youtube_requests(youtu_be_url)
+ stub_oembed_request(youtu_be_url)
+ end
+
+ it "renders lazy-video-container for youtu.be URL" do
+ chat_page.visit_channel(channel)
+ channel_page.send_message(youtu_be_url)
+
+ expect(page).to have_css(".lazy-video-container[data-video-id='#{video_id}']")
+ end
+
+ it "renders lazy-video-container for youtu.be URL with timestamp" do
+ url_with_time = "#{youtu_be_url}?t=42"
+ stub_youtube_requests(url_with_time)
+ stub_oembed_request(url_with_time)
+
+ chat_page.visit_channel(channel)
+ channel_page.send_message(url_with_time)
+
+ expect(page).to have_css(
+ ".lazy-video-container[data-video-id='#{video_id}'][data-video-start-time='42']",
+ )
+ end
+
+ it "renders lazy-video-container for youtu.be URL with si and timestamp parameters" do
+ url_with_params = "#{youtu_be_url}?si=abc123&t=42"
+ stub_youtube_requests(url_with_params)
+ stub_oembed_request(url_with_params)
+
+ chat_page.visit_channel(channel)
+ channel_page.send_message(url_with_params)
+
+ expect(page).to have_css(
+ ".lazy-video-container[data-video-id='#{video_id}'][data-video-start-time='42']",
+ )
+ end
+ end
+end
diff --git a/spec/lib/oneboxer_spec.rb b/spec/lib/oneboxer_spec.rb
index 496a6fd0ae2..11bef8dda2a 100644
--- a/spec/lib/oneboxer_spec.rb
+++ b/spec/lib/oneboxer_spec.rb
@@ -643,6 +643,126 @@ RSpec.describe Oneboxer do
expect(Oneboxer.external_onebox("https://its.me")[:onebox]).to be_present
end
+
+ context "with ignore_redirects for YouTube URLs" do
+ let(:youtube_html) { <<~HTML }
+ <html>
+ <head>
+ <meta property="og:title" content="Test YouTube Video">
+ <meta property="og:description" content="A test video description">
+ <meta property="og:image" content="https://i.ytimg.com/vi/abc123/maxresdefault.jpg">
+ </head>
+ <body>
+ <p>body</p>
+ </body>
+ <html>
+ HTML
+
+ let(:youtube_oembed) do
+ {
+ title: "Test YouTube Video",
+ author_name: "Test Channel",
+ thumbnail_url: "https://i.ytimg.com/vi/abc123/hqdefault.jpg",
+ }.to_json
+ end
+
+ before { Discourse.cache.clear }
+
+ it "resolves youtu.be URLs without following redirects to youtube.com" do
+ youtu_be_url = "https://youtu.be/abc123"
+ youtube_full_url = "https://www.youtube.com/watch?v=abc123"
+
+ head_stub =
+ stub_request(:head, youtu_be_url).to_return(
+ status: 301,
+ body: "",
+ headers: {
+ "location" => youtube_full_url,
+ },
+ )
+
+ stub_request(:get, youtu_be_url).to_return(status: 200, body: youtube_html)
+
+ stub_request(:get, "https://www.youtube.com/oembed?url=#{youtu_be_url}").to_return(
+ status: 200,
+ body: youtube_oembed,
+ )
+
+ stub_request(:any, "https://youtu.be/embed/abc123").to_return(status: 403, body: nil)
+
+ redirect_stub = stub_request(:any, youtube_full_url)
+
+ result = Oneboxer.external_onebox(youtu_be_url)
+
+ expect(result[:onebox]).to be_present
+ expect(result[:onebox]).to include("abc123")
+ expect(result[:onebox]).to include("youtube.com/embed/abc123")
+
+ expect(head_stub).not_to have_been_requested
+ expect(redirect_stub).not_to have_been_requested
+ end
+
+ it "resolves youtube.com URLs without following redirects" do
+ youtube_url = "https://www.youtube.com/watch?v=xyz789"
+ redirect_url = "https://www.youtube.com/watch?v=xyz789&feature=share"
+
+ head_stub =
+ stub_request(:head, youtube_url).to_return(
+ status: 301,
+ body: "",
+ headers: {
+ "location" => redirect_url,
+ },
+ )
+
+ stub_request(:get, youtube_url).to_return(status: 200, body: youtube_html)
+
+ stub_request(:get, "https://www.youtube.com/oembed?url=#{youtube_url}").to_return(
+ status: 200,
+ body: youtube_oembed,
+ )
+
+ stub_request(:any, "https://www.youtube.com/embed/xyz789").to_return(status: 403, body: nil)
+
+ redirect_stub = stub_request(:any, redirect_url)
+
+ result = Oneboxer.external_onebox(youtube_url)
+
+ expect(result[:onebox]).to be_present
+ expect(result[:onebox]).to include("xyz789")
+ expect(result[:onebox]).to include("youtube.com/embed/xyz789")
+
+ expect(head_stub).not_to have_been_requested
+ expect(redirect_stub).not_to have_been_requested
+ end
+
+ it "follows redirects for domains not in ignore_redirects list" do
+ other_url = "https://other-site.com/video"
+ redirect_url = "https://other-site.com/video/actual"
+
+ head_stub =
+ stub_request(:head, other_url).to_return(
+ status: 301,
+ body: "",
+ headers: {
+ "location" => redirect_url,
+ },
+ )
+
+ redirect_head_stub =
+ stub_request(:head, redirect_url).to_return(status: 200, body: "", headers: {})
+
+ stub_request(:get, redirect_url).to_return(status: 200, body: html)
+
+ result = Oneboxer.external_onebox(other_url)
+
+ expect(result[:onebox]).to be_present
+ expect(result[:onebox]).to include("Cats")
+
+ expect(head_stub).to have_been_requested
+ expect(redirect_head_stub).to have_been_requested
+ end
+ end
end
describe "onebox custom user agent" do