1
0
Fork 0
mirror of https://github.com/mastodon/mastodon.git synced 2024-08-20 21:08:15 -07:00

Remove unrelated commit

This commit is contained in:
Christian Schmidt 2024-07-31 21:25:24 +02:00
parent c79301fc07
commit d6bcfb7131
4 changed files with 6 additions and 46 deletions

View file

@ -45,13 +45,7 @@ class FetchLinkCardService < BaseService
def html def html
return @html if defined?(@html) return @html if defined?(@html)
headers = { @html = Request.new(:get, @url).add_headers('Accept' => 'text/html', 'User-Agent' => "#{Mastodon::Version.user_agent} Bot").perform do |res|
'Accept' => 'text/html',
'Accept-Language' => [@status.language.presence, "#{I18n.default_locale};q=0.5", '*'].compact.join(', '),
'User-Agent' => "#{Mastodon::Version.user_agent} Bot",
}
@html = Request.new(:get, @url).add_headers(headers).perform do |res|
next unless res.code == 200 && res.mime_type == 'text/html' next unless res.code == 200 && res.mime_type == 'text/html'
# We follow redirects, and ideally we want to save the preview card for # We follow redirects, and ideally we want to save the preview card for
@ -109,9 +103,9 @@ class FetchLinkCardService < BaseService
service = FetchOEmbedService.new service = FetchOEmbedService.new
url_domain = Addressable::URI.parse(@url).normalized_host url_domain = Addressable::URI.parse(@url).normalized_host
cached_endpoint = Rails.cache.read("oembed_endpoint:#{url_domain}") cached_endpoint = Rails.cache.read("oembed_endpoint:#{url_domain}")
p 'cached_endpoint', cached_endpoint, @url
embed = service.call(@url, language: @status.language.presence, cached_endpoint: cached_endpoint) unless cached_endpoint.nil? embed = service.call(@url, cached_endpoint: cached_endpoint) unless cached_endpoint.nil?
embed ||= service.call(@url, language: @status.language.presence, html: html) unless html.nil? embed ||= service.call(@url, html: html) unless html.nil?
return false if embed.nil? return false if embed.nil?

View file

@ -81,7 +81,7 @@ class FetchOEmbedService
def fetch! def fetch!
return if @endpoint_url.blank? return if @endpoint_url.blank?
body = Request.new(:get, @endpoint_url).add_headers('Accept-Language' => accept_language).perform do |res| body = Request.new(:get, @endpoint_url).perform do |res|
res.code == 200 ? res.body_with_limit : nil res.code == 200 ? res.body_with_limit : nil
end end
@ -106,12 +106,8 @@ class FetchOEmbedService
def html def html
return @html if defined?(@html) return @html if defined?(@html)
@html = @options[:html] || Request.new(:get, @url).add_headers('Accept' => 'text/html', 'Accept-Language' => accept_language).perform do |res| @html = @options[:html] || Request.new(:get, @url).add_headers('Accept' => 'text/html').perform do |res|
res.code != 200 || res.mime_type != 'text/html' ? nil : res.body_with_limit res.code != 200 || res.mime_type != 'text/html' ? nil : res.body_with_limit
end end
end end
def accept_language
[@options[:language], "#{I18n.default_locale};q=0.5", '*'].compact.join(', ')
end
end end

View file

@ -321,28 +321,4 @@ RSpec.describe FetchLinkCardService do
expect(a_request(:get, 'https://quitter.se/tag/wannacry')).to_not have_been_made expect(a_request(:get, 'https://quitter.se/tag/wannacry')).to_not have_been_made
end end
end end
describe 'Accept-Language' do
let(:status) { Fabricate(:status, language: language, text: 'http://example.com/html') }
context 'when status language is defined' do
let(:language) { 'da' }
it 'includes the status language' do
expect(a_request(:get, 'http://example.com/html').
with(headers: { 'Accept-Language' => 'da, en;q=0.5, *' })).
to have_been_made.once
end
end
context 'when status language is not defined' do
let(:language) { nil }
it 'includes the status language' do
expect(a_request(:get, 'http://example.com/html').
with(headers: { 'Accept-Language' => 'en;q=0.5, *' })).
to have_been_made.once
end
end
end
end end

View file

@ -37,12 +37,6 @@ describe FetchOEmbedService do
subject.call('https://www.youtube.com/watch?v=IPSbNdBmWKE') subject.call('https://www.youtube.com/watch?v=IPSbNdBmWKE')
expect(Rails.cache.read('oembed_endpoint:www.youtube.com')[:endpoint]).to eq 'https://www.youtube.com/oembed?format=json&url={url}' expect(Rails.cache.read('oembed_endpoint:www.youtube.com')[:endpoint]).to eq 'https://www.youtube.com/oembed?format=json&url={url}'
end end
it 'sends Accept-Language header' do
subject.call('https://www.youtube.com/watch?v=IPSbNdBmWKE', language: 'fi')
expect(a_request(:get, 'https://www.youtube.com/watch?v=IPSbNdBmWKE').with(headers: { 'Accept-Language' => 'fi, en;q=0.5, *' })).to have_been_made
expect(a_request(:get, 'https://www.youtube.com/oembed?format=json&url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DIPSbNdBmWKE').with(headers: { 'Accept-Language' => 'fi, en;q=0.5, *' })).to have_been_made
end
end end
context 'when both of JSON and XML provider are discoverable' do context 'when both of JSON and XML provider are discoverable' do