mirror of
https://github.com/discourse/discourse.git
synced 2025-01-16 06:02: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,
|
url,
|
||||||
max_redirects: max_redirects,
|
max_redirects: max_redirects,
|
||||||
initial_https_redirect_ignore_limit: SiteSetting.block_onebox_on_redirect,
|
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
|
title = nil if title && title.length < MIN_TITLE_LENGTH
|
||||||
return onebox_for(url, title, opts)
|
return onebox_for(url, title, opts)
|
||||||
|
@ -115,7 +118,7 @@ class InlineOneboxer
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.cache_key(url)
|
def self.cache_key(url)
|
||||||
"inline_onebox:#{url}"
|
"inline_onebox:#{Oneboxer.onebox_locale}:#{url}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.post_author_for_title(topic, post_number)
|
def self.post_author_for_title(topic, post_number)
|
||||||
|
|
|
@ -8,11 +8,12 @@ module RetrieveTitle
|
||||||
FinalDestination::UrlEncodingError,
|
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(
|
fetch_title(
|
||||||
url,
|
url,
|
||||||
max_redirects: max_redirects,
|
max_redirects: max_redirects,
|
||||||
initial_https_redirect_ignore_limit: initial_https_redirect_ignore_limit,
|
initial_https_redirect_ignore_limit: initial_https_redirect_ignore_limit,
|
||||||
|
headers: headers,
|
||||||
)
|
)
|
||||||
rescue *UNRECOVERABLE_ERRORS
|
rescue *UNRECOVERABLE_ERRORS
|
||||||
# ¯\_(ツ)_/¯
|
# ¯\_(ツ)_/¯
|
||||||
|
@ -70,7 +71,12 @@ module RetrieveTitle
|
||||||
end
|
end
|
||||||
|
|
||||||
# Fetch the beginning of a HTML document at a url
|
# 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 =
|
fd =
|
||||||
FinalDestination.new(
|
FinalDestination.new(
|
||||||
url,
|
url,
|
||||||
|
@ -78,9 +84,7 @@ module RetrieveTitle
|
||||||
stop_at_blocked_pages: true,
|
stop_at_blocked_pages: true,
|
||||||
max_redirects: max_redirects,
|
max_redirects: max_redirects,
|
||||||
initial_https_redirect_ignore_limit: initial_https_redirect_ignore_limit,
|
initial_https_redirect_ignore_limit: initial_https_redirect_ignore_limit,
|
||||||
headers: {
|
headers: headers.merge({ Accept: "text/html,*/*" }),
|
||||||
Accept: "text/html,*/*",
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
|
|
||||||
current = nil
|
current = nil
|
||||||
|
|
|
@ -20,19 +20,19 @@ RSpec.describe InlineOneboxer do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "caching" do
|
describe "caching" do
|
||||||
fab!(:topic)
|
|
||||||
|
|
||||||
before { InlineOneboxer.invalidate(topic.url) }
|
|
||||||
|
|
||||||
it "puts an entry in the cache" do
|
|
||||||
SiteSetting.enable_inline_onebox_on_all_domains = true
|
|
||||||
url = "https://example.com/good-url"
|
url = "https://example.com/good-url"
|
||||||
|
|
||||||
|
before do
|
||||||
|
SiteSetting.enable_inline_onebox_on_all_domains = true
|
||||||
stub_request(:get, url).to_return(
|
stub_request(:get, url).to_return(
|
||||||
status: 200,
|
status: 200,
|
||||||
body: "<html><head><title>a blog</title></head></html>",
|
body: "<html><head><title>a blog</title></head></html>",
|
||||||
)
|
)
|
||||||
|
|
||||||
InlineOneboxer.invalidate(url)
|
InlineOneboxer.invalidate(url)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "puts an entry in the cache" do
|
||||||
expect(InlineOneboxer.cache_lookup(url)).to be_blank
|
expect(InlineOneboxer.cache_lookup(url)).to be_blank
|
||||||
|
|
||||||
result = InlineOneboxer.lookup(url)
|
result = InlineOneboxer.lookup(url)
|
||||||
|
@ -42,6 +42,34 @@ RSpec.describe InlineOneboxer do
|
||||||
expect(cached[:url]).to eq(url)
|
expect(cached[:url]).to eq(url)
|
||||||
expect(cached[:title]).to eq("a blog")
|
expect(cached[:title]).to eq("a blog")
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
describe ".lookup" do
|
describe ".lookup" do
|
||||||
|
|
Loading…
Reference in New Issue
Block a user