mirror of
https://github.com/discourse/discourse.git
synced 2024-11-22 11:23:25 +08:00
FIX: Add onebox support for x.com (#27140)
This commit is contained in:
parent
ebc3af90eb
commit
1ed1a1f96a
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user