Use original URL in preview if it redirects to 4xx page (#26200)
This commit is contained in:
parent
3554c52795
commit
f96e4b3047
3 changed files with 179 additions and 527 deletions
|
@ -45,20 +45,18 @@ class FetchLinkCardService < BaseService
|
|||
def html
|
||||
return @html if defined?(@html)
|
||||
|
||||
Request.new(:get, @url).add_headers('Accept' => 'text/html', 'User-Agent' => "#{Mastodon::Version.user_agent} Bot").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
|
||||
# the destination URL and not any link shortener in-between, so here
|
||||
# we set the URL to the one of the last response in the redirect chain
|
||||
@url = res.request.uri.to_s
|
||||
@card = PreviewCard.find_or_initialize_by(url: @url) if @card.url != @url
|
||||
|
||||
if res.code == 200 && res.mime_type == 'text/html'
|
||||
@html_charset = res.charset
|
||||
@html = res.body_with_limit
|
||||
else
|
||||
@html_charset = nil
|
||||
@html = nil
|
||||
end
|
||||
@html_charset = res.charset
|
||||
|
||||
res.body_with_limit
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue