FIX: Inline oneboxes should obey the locale. (#30664)

Following on from f369db5ae9, we need to apply a similar fix to inline oneboxes, since they use a different code path to retrieve the onebox provider data.

This change ensures the Accept-Language header is sent by inline onebox requests, too.
This commit is contained in:
Gary Pendergast 2025-01-09 17:22:22 +11:00 committed by GitHub
parent f53c734ba6
commit ec30b6f6c6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 46 additions and 11 deletions

View File

@ -77,6 +77,9 @@ class InlineOneboxer
url,
max_redirects: max_redirects,
initial_https_redirect_ignore_limit: SiteSetting.block_onebox_on_redirect,
headers: {
"Accept-Language" => Oneboxer.accept_language,
},
)
title = nil if title && title.length < MIN_TITLE_LENGTH
return onebox_for(url, title, opts)
@ -115,7 +118,7 @@ class InlineOneboxer
end
def self.cache_key(url)
"inline_onebox:#{url}"
"inline_onebox:#{Oneboxer.onebox_locale}:#{url}"
end
def self.post_author_for_title(topic, post_number)

View File

@ -8,11 +8,12 @@ module RetrieveTitle
FinalDestination::UrlEncodingError,
]
def self.crawl(url, max_redirects: nil, initial_https_redirect_ignore_limit: false)
def self.crawl(url, max_redirects: nil, initial_https_redirect_ignore_limit: false, headers: {})
fetch_title(
url,
max_redirects: max_redirects,
initial_https_redirect_ignore_limit: initial_https_redirect_ignore_limit,
headers: headers,
)
rescue *UNRECOVERABLE_ERRORS
# ¯\_(ツ)_/¯
@ -70,7 +71,12 @@ module RetrieveTitle
end
# Fetch the beginning of a HTML document at a url
def self.fetch_title(url, max_redirects: nil, initial_https_redirect_ignore_limit: false)
def self.fetch_title(
url,
max_redirects: nil,
initial_https_redirect_ignore_limit: false,
headers: {}
)
fd =
FinalDestination.new(
url,
@ -78,9 +84,7 @@ module RetrieveTitle
stop_at_blocked_pages: true,
max_redirects: max_redirects,
initial_https_redirect_ignore_limit: initial_https_redirect_ignore_limit,
headers: {
Accept: "text/html,*/*",
},
headers: headers.merge({ Accept: "text/html,*/*" }),
)
current = nil

View File

@ -20,19 +20,19 @@ RSpec.describe InlineOneboxer do
end
describe "caching" do
fab!(:topic)
url = "https://example.com/good-url"
before { InlineOneboxer.invalidate(topic.url) }
it "puts an entry in the cache" do
before do
SiteSetting.enable_inline_onebox_on_all_domains = true
url = "https://example.com/good-url"
stub_request(:get, url).to_return(
status: 200,
body: "<html><head><title>a blog</title></head></html>",
)
InlineOneboxer.invalidate(url)
end
it "puts an entry in the cache" do
expect(InlineOneboxer.cache_lookup(url)).to be_blank
result = InlineOneboxer.lookup(url)
@ -42,6 +42,34 @@ RSpec.describe InlineOneboxer do
expect(cached[:url]).to eq(url)
expect(cached[:title]).to eq("a blog")
end
it "separates cache by default_locale" do
expect(InlineOneboxer.cache_lookup(url)).to be_blank
result = InlineOneboxer.lookup(url)
expect(result[:title]).to be_present
cached = InlineOneboxer.cache_lookup(url)
expect(cached[:title]).to eq("a blog")
SiteSetting.default_locale = "fr"
expect(InlineOneboxer.cache_lookup(url)).to be_blank
end
it "separates cache by onebox_locale, when set" do
expect(InlineOneboxer.cache_lookup(url)).to be_blank
result = InlineOneboxer.lookup(url)
expect(result[:title]).to be_present
cached = InlineOneboxer.cache_lookup(url)
expect(cached[:title]).to eq("a blog")
SiteSetting.onebox_locale = "fr"
expect(InlineOneboxer.cache_lookup(url)).to be_blank
end
end
describe ".lookup" do