FIX: Add onebox support for x.com (#27140)

This commit is contained in:
Jan Cernik 2024-05-23 10:25:42 -03:00 committed by GitHub
parent ebc3af90eb
commit 1ed1a1f96a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 14 additions and 5 deletions

View File

@ -9,7 +9,7 @@ module Onebox
include ActionView::Helpers::NumberHelper
matches_regexp(
%r{^https?://(mobile\.|www\.)?twitter\.com/.+?/status(es)?/\d+(/(video|photo)/\d?+)?+(/?\?.*)?/?$},
%r{^https?://(mobile\.|www\.)?(twitter\.com|x\.com)/.+?/status(es)?/\d+(/(video|photo)/\d?+)?+(/?\?.*)?/?$},
)
always_https
@ -26,7 +26,13 @@ module Onebox
def get_twitter_data
response =
begin
Onebox::Helpers.fetch_response(url, headers: http_params)
# We need to allow cross domain cookies to prevent an
# infinite redirect loop between twitter.com and x.com
Onebox::Helpers.fetch_response(
url,
headers: http_params,
allow_cross_domain_cookies: true,
)
rescue StandardError
return nil
end
@ -45,7 +51,7 @@ module Onebox
end
def match
@match ||= @url.match(%r{twitter\.com/.+?/status(es)?/(?<id>\d+)})
@match ||= @url.match(%r{(twitter\.com|x\.com)/.+?/status(es)?/(?<id>\d+)})
end
def twitter_data

View File

@ -74,7 +74,8 @@ module Onebox
domain: nil,
headers: nil,
body_cacher: nil,
raise_error_when_response_too_large: true
raise_error_when_response_too_large: true,
allow_cross_domain_cookies: false
)
redirect_limit = Onebox.options.redirect_limit if redirect_limit >
Onebox.options.redirect_limit
@ -113,6 +114,7 @@ module Onebox
size_bytes = Onebox.options.max_download_kb * 1024
http.request(request) do |response|
if cookie = response.get_fields("set-cookie")
headers["Cookie"] = cookie.join("; ") if allow_cross_domain_cookies
# HACK: If this breaks again in the future, use HTTP::CookieJar from gem 'http-cookie'
# See test: it "does not send cookies to the wrong domain"
redir_header = { "Cookie" => cookie.join("; ") }
@ -129,7 +131,8 @@ module Onebox
response["location"],
redirect_limit: redirect_limit - 1,
domain: "#{uri.scheme}://#{uri.host}",
headers: redir_header,
headers: allow_cross_domain_cookies ? headers : redir_header,
allow_cross_domain_cookies: allow_cross_domain_cookies,
)
)
end