Skip to content

Commit

Permalink
Use original URL in preview if it redirects to 4xx page (#26200)
Browse files Browse the repository at this point in the history
  • Loading branch information
c960657 committed Jul 28, 2023
1 parent 3554c52 commit f96e4b3
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 527 deletions.
14 changes: 6 additions & 8 deletions app/services/fetch_link_card_service.rb
Expand Up @@ -45,20 +45,18 @@ def process_url
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

Expand Down

0 comments on commit f96e4b3

Please sign in to comment.