mirror of
https://github.com/discourse/discourse.git
synced 2025-01-16 03:52:41 +08:00
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:
parent
f53c734ba6
commit
ec30b6f6c6
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user