From d6bcfb7131cd6f2481e6d1b9952b9de8a3a5a1d1 Mon Sep 17 00:00:00 2001 From: Christian Schmidt Date: Wed, 31 Jul 2024 21:25:24 +0200 Subject: [PATCH] Remove unrelated commit --- app/services/fetch_link_card_service.rb | 14 ++++------- app/services/fetch_oembed_service.rb | 8 ++----- spec/services/fetch_link_card_service_spec.rb | 24 ------------------- spec/services/fetch_oembed_service_spec.rb | 6 ----- 4 files changed, 6 insertions(+), 46 deletions(-) diff --git a/app/services/fetch_link_card_service.rb b/app/services/fetch_link_card_service.rb index 62a813fc815..adabb1096e8 100644 --- a/app/services/fetch_link_card_service.rb +++ b/app/services/fetch_link_card_service.rb @@ -45,13 +45,7 @@ class FetchLinkCardService < BaseService def html return @html if defined?(@html) - headers = { - '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| + @html = Request.new(:get, @url).add_headers('Accept' => 'text/html', 'User-Agent' => "#{Mastodon::Version.user_agent} Bot").perform do |res| next unless res.code == 200 && res.mime_type == 'text/html' # We follow redirects, and ideally we want to save the preview card for @@ -109,9 +103,9 @@ class FetchLinkCardService < BaseService service = FetchOEmbedService.new url_domain = Addressable::URI.parse(@url).normalized_host 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, language: @status.language.presence, html: html) unless html.nil? + + embed = service.call(@url, cached_endpoint: cached_endpoint) unless cached_endpoint.nil? + embed ||= service.call(@url, html: html) unless html.nil? return false if embed.nil? diff --git a/app/services/fetch_oembed_service.rb b/app/services/fetch_oembed_service.rb index 3322c98f8f1..dc84b16b684 100644 --- a/app/services/fetch_oembed_service.rb +++ b/app/services/fetch_oembed_service.rb @@ -81,7 +81,7 @@ class FetchOEmbedService def fetch! 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 end @@ -106,12 +106,8 @@ class FetchOEmbedService def 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 end end - - def accept_language - [@options[:language], "#{I18n.default_locale};q=0.5", '*'].compact.join(', ') - end end diff --git a/spec/services/fetch_link_card_service_spec.rb b/spec/services/fetch_link_card_service_spec.rb index 8d0fe485e4d..2f64f405583 100644 --- a/spec/services/fetch_link_card_service_spec.rb +++ b/spec/services/fetch_link_card_service_spec.rb @@ -321,28 +321,4 @@ RSpec.describe FetchLinkCardService do expect(a_request(:get, 'https://quitter.se/tag/wannacry')).to_not have_been_made 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 diff --git a/spec/services/fetch_oembed_service_spec.rb b/spec/services/fetch_oembed_service_spec.rb index 32363f67cd0..c9f84048b61 100644 --- a/spec/services/fetch_oembed_service_spec.rb +++ b/spec/services/fetch_oembed_service_spec.rb @@ -37,12 +37,6 @@ describe FetchOEmbedService do 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}' 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 context 'when both of JSON and XML provider are discoverable' do